Pagini recente » Monitorul de evaluare | Cod sursa (job #369523) | Cod sursa (job #1776558) | Cod sursa (job #254030) | Cod sursa (job #3344111)
#include <bits/stdc++.h>
#define eb emplace_back
#define cin fin
#define cout fout
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int t[100020], r[100020];
void unire(int x, int y)
{
if (r[x]>r[y])
t[y]=x;
else
{
if (r[x]<r[y])
t[x]=y;
else
{
t[y]=x;
r[x]++;
}
}
}
int findu(int x)
{
int temp, rad;
rad=x;
while (t[rad]!=0)
rad=t[rad];
while (t[x]!=0)
{
temp=t[x];
t[x]=rad;
x=temp;
}
return rad;
}
int n, m, x, y, c;
int r1, r2;
int main()
{
cin>>n>>m;
while (m--)
{
cin>>c>>x>>y;
if (c==1)
{
r1=findu(x);
r2=findu(y);
if (r1!=r2)
unire(x, y);
}
else
{
if (findu(x)!=findu(y))
cout<<"NU"<<'\n';
else
cout<<"DA"<<'\n';
}
}
}