Pagini recente » Cod sursa (job #398262) | Cod sursa (job #283332) | Cod sursa (job #233926) | Cod sursa (job #1971694) | Cod sursa (job #2028650)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int rang[100000], TT[100000];
int root(int nod){
int R=nod,next;
while(TT[R]!=R)R=TT[R];
while(TT[nod]!=nod){
next=TT[nod];
TT[nod]=R;
nod=next;
}
return R;
}
void unite(int x,int y)
{
TT[y]=x;
rang[x]+=rang[y];
}
int main()
{
int N,M,op,x,y;
in>>N>>M;
int i;
for (i=1 ;i<=N ;++i){
TT[i]=i;
rang[i]=1;
}
for(i=1 ; i<=M ; ++i)
{
in>>op>>x>>y;
if(op==1){
int rootX,rootY;
rootX=root(x);
rootY=root(y);
if(rootX!=rootY)unite(x,y);
}
else {
if(root(x)!=root(y))out<<"NU\n";
else out<<"DA\n";
}
}
return 0;
}