Pagini recente » Borderou de evaluare (job #1594257) | Cod sursa (job #628273) | Cod sursa (job #202068) | Cod sursa (job #202069) | Cod sursa (job #462885)
Cod sursa(job #462885)
#include <cstdio>
#define file_in "disjoint.in"
#define file_out "disjoint.out"
#define nmax 101000
int n,m;
int tata[nmax];
int nr[nmax];
void citire()
{
freopen(file_in,"r",stdin);
freopen(file_out,"w",stdout);
scanf("%d %d", &n, &m);
}
int find(int x)
{
if (tata[x]!=x)
tata[x]=find(tata[x]);
return tata[x];
}
void unite(int x, int y)
{
if (nr[x]>nr[y])
{
nr[x]+=nr[y];
tata[y]=x;
}
else
{
nr[y]+=nr[x];
tata[x]=y;
}
}
void solve()
{
int i,tip,x,y,t1,t2;;
for (i=1;i<=n;++i)
{
tata[i]=i;
nr[i]=1;
}
while(m--)
{
scanf("%d %d %d", &tip, &x, &y);
t1=find(x);
t2=find(y);
if (tip==1)
unite(t1,t2);
else
{
if (t1==t2)
printf("DA\n");
else
printf("NU\n");
}
}
}
int main()
{
citire();
solve();
fclose(stdin);
fclose(stdout);
return 0;
}