Pagini recente » Cod sursa (job #466566) | Cod sursa (job #2633620) | Cod sursa (job #1623571) | Cod sursa (job #1590318) | Cod sursa (job #2772783)
#include <bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n,m,t[100005],nivel[100005];
int root(int p)
{
while (t[p] != 0)
p = t[p];
return p;
}
void unite(int x,int y)
{
if(nivel[x] > nivel[y])
t[y] = x;
if(nivel[x] < nivel[y])
t[x] = y;
if (nivel[x] == nivel[y])
{
t[x] = y;
nivel[y]++;
}
}
int main()
{
in >> n >> m;
for (int i = 1; i <= m; i++)
{
int t,x,y;
in >> t >> x >> y;
if (t == 1)
unite(root(x),root(y));
else
{
if (root(x) == root(y))
out << "DA\n";
else
out << "NU\n";
}
}
return 0;
}