Cod sursa(job #2675153)
Utilizator | Hantig Lorena LorenaMaria | Data | 21 noiembrie 2020 10:41:48 |
---|---|---|---|
Problema | Paduri de multimi disjuncte | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.59 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n,q,t,x,y,p[100001],v[100001];
int main()
{ in>>n>>q;
while(q--)
{ in>>t>>x>>y;
if(t==1)
{ while(p[x]!=0)
x=p[x];
while(p[y]!=0)
y=p[y];
if(v[x]<v[y])
swap(x,y);
p[y]=x;
v[x]+=v[y]+1;
}
else
{ while(p[x]!=0)
x=p[x];
while(p[y]!=0)
y=p[y];
if(x==y)
out<<"DA\n";
else
out<<"NU\n";
}
}
in.close();
out.close();
return 0;
}