Pagini recente » Borderou de evaluare (job #3325574) | Borderou de evaluare (job #794966) | Borderou de evaluare (job #3319420) | Monitorul de evaluare | Cod sursa (job #1168027)
#include<cstdio>
using namespace std;
const int NMAX = 100000+5;
int N,M;
int Root[NMAX];
int Rang[NMAX];
void Read(),Solve();
int Find(int x)
{
if(Root[x] != x) Root[x] = Find(Root[x]);
return Root[x];
}
void Union(int x,int y)
{
int tx = Find(x),ty = Find(y);
if(Rang[tx] < Rang[ty])
{
Root[x] = ty;
return;
}
if(Rang[tx] > Rang[ty])
{
Root[y] = tx;
return;
}
Root[x] = ty;
Rang[tx]++;
}
int main()
{
Read();
Solve();
return 0;
}
void Read()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d%d",&N,&M);
}
void Solve()
{
int t,x,y;
for(; N; --N)
Root[N] = N;
for(; M; --M)
{
scanf("%d%d%d",&t,&x,&y);
if(t == 1) Union(x,y);
else printf("%s\n",Find(x) == Find(y)?"DA":"NU");
}
}