Pagini recente » Cod sursa (job #2761592) | Cod sursa (job #5796) | Cod sursa (job #3126555) | Cod sursa (job #2481860) | Cod sursa (job #2335494)
#include <iostream>
#include <fstream>
using namespace std;
int father[100001];
int temp[100001];
int main()
{
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n,m;
fin>>n>>m;
int p,a,b;
int i,j;
for(i=0;i<m;i++){
fin>>p>>a>>b;
if(p==1){
if(father[a]==0 && father[b]==0){
father[b]=a;
}
else{
while(father[a]!=0)
a=father[a];
while(father[b]!=0)
b=father[b];
father[b]=a;
}
}
else{
j=0;
while(father[a]!=0){
temp[j]=a;
a=father[a];
j++;
}
j--;
while(j>=0){
father[temp[j]]=a;
j--;
}
j=0;
while(father[b]!=0){
temp[j]=b;
b=father[b];
j++;
}
j--;
while(j>=0){
father[temp[j]]=b;
j--;
}
if(a==b) fout<<"DA"<<endl;
else fout<<"NU"<<endl;
}
}
fin.close();
fout.close();
return 0;
}