Pagini recente » Cod sursa (job #2117905) | Cod sursa (job #3214859) | Cod sursa (job #2504121) | Borderou de evaluare (job #2006251) | Cod sursa (job #2472410)
//#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int Max=100005;
int n,m,tata[Max],rang[Max];
ifstream in("disjoint.in");
ofstream out("disjoint.out");
void reuniune(int x,int y)
{
while(tata[x]!=x || tata[y]!=y)
{
x=tata[x];
y=tata[y];
}
if(rang[x]<rang[y])
tata[x]=y;
else
tata[y]=x;
if(rang[x]==rang[y])
rang[x]++;
}
int find(int x,int y)
{
while(tata[x]!=x || tata[y]!=y)
{
x=tata[x];
y=tata[y];
}
if(x==y)
return 1;
return 0;
}
void citire()
{
in>>n>>m;
for(int i=1;i<=n;i++)
tata[i]=i;
for(int i=1;i<=m;i++)
{
int q,x,y; in>>q>>x>>y;
if(q==1)
reuniune(x,y);
else
{
if(find(x,y))
out<<"DA"<<"\n";
else
out<<"NU"<<"\n";
}
}
}
int main()
{
citire();
return 0;
}