Pagini recente » Cod sursa (job #1181388) | Cod sursa (job #164693) | Cod sursa (job #1800619) | Cod sursa (job #2377442) | Cod sursa (job #1477257)
#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXNM=100001;
int n,m,cod,x,y;
int pr[MAXNM], inaltime[MAXNM];
void preg(int n)
{
for(int i=1;i<=n;i++)
pr[i]=i;
}
int caut_pr(int x)
{
if(pr[x]==x) return x;
else return caut_pr(pr[x]);
}
void reun(int x, int y)
{
int pr_x=caut_pr(x), pr_y=caut_pr(y);
if(inaltime[x]<inaltime[y]) pr[x]=pr_y;
else
if(inaltime[x]>inaltime[y]) pr[y]=pr_x;
else
{
pr[y]=pr_x;
inaltime[x]++;
}
}
void solve()
{
scanf("%d %d", &n, &m);
preg(n);
for(int i=1;i<=m;i++)
{
scanf("%d %d %d", &cod, &x, &y);
if(cod==2)
if(caut_pr(x)==caut_pr(y)) printf("DA \n");
else printf("NU \n");
else
reun(x,y);
}
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
solve();
return 0;
}