Pagini recente » Cod sursa (job #2166127) | Cod sursa (job #1198877) | Cod sursa (job #2373910) | Cod sursa (job #39378) | Cod sursa (job #3195368)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int t[100001], r[100001], n, m, c, x, y, h, a, b;
int tatal(int x)
{
if(t[x])
{
h=tatal(t[x]);
t[x]=h;
r[x]=r[h]++;
return t[x];
}
return x;
}
void leaga(int x, int y)
{
a=tatal(x);
b=tatal(y);
if(r[x]>r[y])
{
t[b]=a;
}
else{
t[a]=b;
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>c>>x>>y;
if(c==1)
{
leaga(x, y);
}
else
{
if(tatal(x)==tatal(y))
cout<<"DA";
else
cout<<"NU";
cout<<endl;
}
}
}