Pagini recente » Cod sursa (job #1784856) | Cod sursa (job #892280) | Cod sursa (job #1221373) | Cod sursa (job #3214971) | Cod sursa (job #361924)
Cod sursa(job #361924)
#include<stdio.h>
#include<stdlib.h>
#define NM 100001
struct set
{
int p;//parinte
int h;//inaltime
};
set s[NM];
void buildset(int x)
{
s[x].p=x;
s[x].h=0;
}
int find(int x)
{
if(x==s[x].p)return x;
else return find(s[x].p);
}
void reunion(int x,int y)
{
int xroot,yroot;
xroot=find(x);
yroot=find(y);
if(s[xroot].h<s[yroot].h) s[xroot].p=yroot;
else if(s[xroot].h>s[yroot].h) s[yroot].p=xroot;
else s[yroot].p=xroot,s[xroot].h++;
}
int main()
{
int n,m,x,y,c,i,xr,yr;
char sir[21],*p;
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d%d\n",&n,&m);
for(i=1;i<=n;i++) buildset(i);
while(m--)
{
fgets(sir,20,stdin);
p=sir;
c=atoi(p);
p++;p++;
x=atoi(p);
while(*p!=' ')p++;
p++;
y=atoi(p);
if(c==1)
{
reunion(x,y);
}
else
{
xr=find(x);
yr=find(y);
if(xr==yr)printf("DA\n");
else printf("NU\n");
}
}
return 0;
}