Pagini recente » Cod sursa (job #763246) | Cod sursa (job #1478803) | Cod sursa (job #2557185) | Cod sursa (job #1677817) | Cod sursa (job #3218483)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m;
int T[100008];
int H[100008];
int Find(int nod);
void Union(int t1, int t2);
int main(){
int x, y, c;
fin >> n >> m;
for(int i = 1; i <= n; i++)
H[i] = 1, T[i] = i;
for(int i = 1; i <= m; i++){
fin >> c >> x >> y;
int t1 = Find(x);
int t2 = Find(y);
if(c == 1){
if(t1 != t2)
Union(t1, t2);
}
else{
if(t1 == t2)
fout << "DA" << '\n';
else
fout << "NU" << '\n';
}
}
return 0;
}
int Find(int nod){
while(T[nod] != nod){
nod = T[nod];
}
return T[nod];
}
void Union(int t1, int t2){
if(H[t1] > H[t2])
T[t2] = t1;
else if(H[t1] < H[t2])
T[t1] = t2;
else{
T[t1] = t2;
H[t2]++;
}
}