Pagini recente » Cod sursa (job #762487) | Cod sursa (job #2370814) | Cod sursa (job #1476645) | Cod sursa (job #2924856) | Cod sursa (job #1706814)
#include <fstream>
using namespace std;
ifstream cin ("disjoint.in");
ofstream cout ("disjoint.out");
int n,m,t[100001],rang[100001];
void citire ()
{
cin>>n>>m;
}
void init ()
{
for(int i=1;i<=n;i++)
t[i]=i;
}
int cauta (int x)
{
if(t[x]!=x)
t[x]=cauta(t[x]);
return t[x];
}
void reunire (int i,int j)
{
if(rang[i]>rang[j])
t[j]=i;
else t[i]=j;
if(rang[i]==rang[j]) rang[j]++;
}
void parcurge ()
{ int x,y,tip;
for(int i=1;i<=m;i++)
{
cin>>tip>>x>>y;
if(tip==1)
{
reunire(cauta(x),cauta(y));
}
else
{
if(cauta(x)==cauta(y)) cout<<"DA\n"; else cout<<"NU\n";
}
}
}
int main()
{
citire();
init();
parcurge();
return 0;
}