Pagini recente » Cod sursa (job #2253657) | Cod sursa (job #1318430) | Cod sursa (job #713675) | Cod sursa (job #2479394) | Cod sursa (job #2416008)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int dad[MAXN], high[MAXN];
int getroot(int nod){
int root = nod;
while(root != dad[root])
root = dad[root];
while(nod != root){
int cp = nod;
nod = dad[nod];
dad[cp] = root;
}
return nod;
}
int main()
{
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m;
fin >> n >> m;
for(int i = 1; i <= n; ++i){
dad[i] = i;
high[i] = 1;
}
while(m){
int op, x, y;
fin >> op >> x >> y;
if(op == 1){
int rx = getroot(x), ry = getroot(y);
if(high[rx] > high[ry]) dad[ry] = rx;
else if(high[rx] < high[ry]) dad[rx] = ry;
else{
dad[ry] = rx;
high[rx]++;
}
}
else{
if(getroot(x) == getroot(y)) fout << "DA\n";
else fout << "NU\n";
}
m--;
}
return 0;
}