Pagini recente » Arhiva de probleme | Cod sursa (job #2000362) | Cod sursa (job #2863987) | Cod sursa (job #3212431) | Cod sursa (job #1443714)
#include <stdio.h>
#include <cstring>
#include <vector>
#include <bitset>
#include <set>
#include <math.h>
#include <deque>
#include <queue>
#include <string>
#include <algorithm>
#define nmax 100010
using namespace std;
int n,m,i,x,y,tip,tata[nmax],sz[nmax];
int root(int x)
{
while (x!=tata[x]) x=tata[x];
return x;
}
inline bool is_in_one_component(int x,int y)
{
return (root(x)==root(y));
}
inline void union1(int x,int y)
{
x=root(x); y=root(y);
if (sz[x]>sz[y]) tata[y]=x; else tata[x]=y;
if (sz[x]==sz[y]) sz[y]++;
}
int main(){
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d%d",&n,&m);
for (i=1;i<=m;i++) tata[i]=i,sz[i]=1;
for (i=1;i<=m;i++){
scanf("%d%d%d",&tip,&x,&y);
switch (tip)
{
case 1:{ union1(x,y); break; }
case 2:{
if (is_in_one_component(x,y)) printf("DA\n"); else
printf("NU\n");
break;
}
}
}
return 0;
}