Cod sursa(job #2304029)
| Utilizator | Data | 17 decembrie 2018 14:01:06 | |
|---|---|---|---|
| Problema | Paduri de multimi disjuncte | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.78 kb |
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, t[100001], m, x, y, a, b, cod, i;
int rad(int nod){
while(t[nod] > 0)
nod = t[nod];
return nod;
}
int main(){
fin>>n>>m;
for(i=1;i<=n;i++)
t[i] = -1;
while(m--){
fin>>cod>>x>>y;
a = rad(x);
b = rad(y);
if(cod == 1 && a != b){
if(t[a] < t[b]){
t[a] += t[b];
t[b] = a;
}
else{
t[b] += t[a];
t[a] = b;
}
}
if(cod == 2){
if(a == b)
fout<<"DA\n";
else
fout<<"NU\n";
}
}
return 0;
}
