Pagini recente » Cod sursa (job #1181435) | Cod sursa (job #43940) | Cod sursa (job #1647940) | Cod sursa (job #1074740) | Cod sursa (job #2028652)
#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)
{
if(rang[x]<rang[y])TT[y]=x;
else TT[x]=y;
if(rang[x]==rang[y])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;
}