Pagini recente » Cod sursa (job #2167676) | Cod sursa (job #2098921) | Cod sursa (job #1187304) | Cod sursa (job #1352291) | Cod sursa (job #1590376)
#include <iostream>
#include <fstream>
#define maxn 100005
using namespace std;
int main()
{int n,m,i,j,gr[maxn],c,x,y,h[maxn];
fstream f("disjoint.in",ios::in);
fstream g("disjoint.out",ios::out);
f>>n>>m;
for(i=1;i<=n;i++)
{gr[i]=0;h[i]=0;}
for(i=1;i<=m;i++)
{f>>c>>x>>y;
if(c==1)
{while(gr[x]!=0)
x=gr[x];
while(gr[y]!=0)
y=gr[y];
if(h[x]<h[y])
{gr[x]=y;
h[y]=h[y]+1;}
else
{gr[y]=x;
h[x]=h[x]+1;
}
}
else
{while(gr[x]!=0)
x=gr[x];
while(gr[y]!=0)
y=gr[y];
if(x==y)
g<<"DA"<<'\n';
else
g<<"NU"<<'\n';
}
}
g.close();
}