Pagini recente » Cod sursa (job #1922503) | Cod sursa (job #213819) | Cod sursa (job #2677582) | Cod sursa (job #1317136) | Cod sursa (job #1917218)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n,m,father[100000],inaltime[100000];
void unire(int x,int y){
if(inaltime[x]>inaltime[y])
father[y]=x;
else
father[x]=y;
if(inaltime[x]==inaltime[y])
inaltime[y]++;
}
int getroot(int root){
while(root!=father[root])
root=father[root];
return root;
}
int main(){
int i,op,x,y,rootx,rooty;
rootx=rooty=1;
fin>>n>>m;
for(i=1;i<=n;i++){
father[i]=i;
inaltime[i]=1;}
for(i=1;i<=m;i++){
fin>>op>>x>>y;
rootx=getroot(x);
rooty=getroot(y);
if(op==1)
unire(rootx,rooty);
else{
if(rootx==rooty)
fout<<"DA"<<'\n';
else
fout<<"NU"<<'\n';}
}
return 0;}