Pagini recente » Cod sursa (job #228347) | Cod sursa (job #2928093)
#include<iostream>
using namespace std;
const int NMAX = 1e5 + 1;
int t[NMAX],rang[NMAX];//t[i] = tatal lui i,daca t[i] = 0,i e fatherless
void unite(int a,int b)
{
t[a] = b;
}
int find(int a)
{
int root = a,c = a,cc;
while(t[root]) root = t[root];
while(c != root)
{
cc = c;
c = t[c];
t[cc] = root;
}
return root;
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,q,a,b,t;
cin >> n >> q;
while(q--)
{
cin >> t >> a >> b;
if(t & 1)
{
unite(find(a),find(b));
}
else
{
if(find(a) == find(b) && find(a))
{
cout << "DA\n";
}
else
{
cout << "NU\n";
}
}
}
}