Pagini recente » Borderou de evaluare (job #1927696) | Borderou de evaluare (job #3102529) | Borderou de evaluare (job #3102659) | Borderou de evaluare (job #3117928) | Cod sursa (job #1168028)
#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)
{
if(Rang[x] < Rang[y])
{
Root[x] = y;
return;
}
if(Rang[x] > Rang[y])
{
Root[y] = x;
return;
}
Root[x] = y;
Rang[x]++;
}
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(Find(x),Find(y));
else printf("%s\n",Find(x) == Find(y)?"DA":"NU");
}
}