Pagini recente » Cod sursa (job #2658952) | Cod sursa (job #3031259) | Cod sursa (job #1467916) | Cod sursa (job #2310907) | Cod sursa (job #2571075)
#include <iostream>
#include <fstream>
#define NMAX 100000
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, q, tata[NMAX+10], rang[NMAX+10];
int findDaddy(int x)
{ int y = x, r = x;
while(tata[r] != r) r = tata[r];
while(tata[y] != y)
{ y = tata[y];
tata[x] = r;
x = y;
}
return r;
}
void unite(int x, int y)
{ if(rang[x] < rang[y]) tata[x] = y;
else tata[y] = x;
if(rang[x] == rang[y]) rang[y]++;
}
int main()
{
f >> n >> q;
for(int i=1; i<=n; i++) tata[i] = i, rang[i] = 1;
while(q--)
{ int type, x, y;
f >> type >> x >> y;
int a = findDaddy(x), b = findDaddy(y);
if(type == 2)
{ if(a == b) g << "DA" << '\n';
else g << "NU" << '\n';
}
else unite(a, b);
}
return 0;
}