Pagini recente » Cod sursa (job #527579) | Cod sursa (job #2926823) | Cod sursa (job #2206351) | Cod sursa (job #1872481) | Cod sursa (job #2576218)
#include <bits/stdc++.h>
#define nmax 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m;
int t[nmax], height[nmax];
int get_root(int k)
{
while(t[k] != k)
k = t[k];
return k;
}
void unire(int a, int b)
{
if(height[a] > height[b])
t[b] = a;
else if(height[b] > height[a])
t[a] = b;
else
t[b] = a, height[a]++;
}
int main()
{
int i, op, x, y;
fin >> n >> m;
for(i = 1; i <= n; i++)
t[i] = i;
for(i = 0; i < m; i++)
{
fin >> op >> x >> y;
if(op == 1)
x = get_root(x), y = get_root(y), unire(x, y);
else
{
x = get_root(x), y = get_root(y);
if(x == y)
fout << "DA" << '\n';
else
fout << "NU" << '\n';
}
}
return 0;
}