Pagini recente » Cod sursa (job #1092507) | Cod sursa (job #991557) | Cod sursa (job #1111284) | Cod sursa (job #1949175) | Cod sursa (job #3150404)
#include<iostream>
#include<stdio.h>
using namespace std;
const int NMAX = 1e5 + 5;
int n, m, sz[NMAX], tata[NMAX];
void op1(int x, int y)
{
while(tata[x] != x)
x = tata[x];
while(tata[y] != y)
y = tata[y];
if(sz[x] < sz[y])
swap(x, y);
tata[y] = x;
sz[x] += sz[y];
}
string op2(int x, int y)
{
while(tata[x] != x)
x = tata[x];
while(tata[y] != y)
y = tata[y];
if(tata[x] == tata[y])
return "DA\n";
return "NU\n";
}
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
sz[i] = 1;
tata[i] = i;
}
for(int i = 1; i <= m; i++)
{
int x, y, c;
cin >> c >> x >> y;
if(c == 1)
op1(x, y);
else
cout << op2(x, y);
}
}