Pagini recente » Cod sursa (job #1872351) | Cod sursa (job #806486) | Cod sursa (job #3254509) | Cod sursa (job #2465702) | Cod sursa (job #1467324)
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#include <bitset>
#include <string>
#include <vector>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <limits.h>
#include <algorithm>
#include <deque>
#define nmax 100010
#define mod 1999999973
using namespace std;
int i,n,m,tata[nmax],sz[nmax],rang[nmax],x,y,tip;
int root(int x)
{
if (x==tata[x]) return x; else
return tata[x]=root(tata[x]);
}
void unite(int x,int y)
{
x=root(x); y=root(y);
if (x!=y) {
if (rang[x]>rang[y]) {
tata[y]=x; sz[y]=sz[y]+sz[x];
} else {
tata[x]=y; sz[x]=sz[x]+sz[y];
}
if (rang[x]==rang[y]) rang[y]++;
}
}
int main() {
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d %d",&n,&m);
for (i=1;i<=n;i++) {
tata[i]=i; sz[i]=1; rang[i]=0;
}
for (i=1;i<=m;i++) {
scanf("%d %d %d",&tip,&x,&y);
if (tip==1) unite(x,y); else
{
if (root(x)==root(y)) puts("DA"); else
puts("NU");
}
}
return 0;
}