Pagini recente » Cod sursa (job #89316) | Cod sursa (job #3301335) | Borderou de evaluare (job #1033297) | Cod sursa (job #3296991) | Cod sursa (job #3344258)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m;
const int nmax=1e5, mmax=1e5;
int parinte[nmax+1], rang[nmax+1];
int root(int nod)
{
if(parinte[nod]!=nod)
parinte[nod]=root(parinte[nod]);
return parinte[nod];
}
int main()
{
fin >> n >> m;
for(int i=1; i<=n; i++)
parinte[i]=i;
int c, x, y;
while(m--)
{
fin >> c >> x >> y;
if(c==1)
{
if(rang[x]>rang[y])
{
parinte[root(y)]=root(x);
rang[y]=max(rang[x], rang[y]+1);
}
else
{
parinte[root(x)]=root(y);
rang[x]=max(rang[y], rang[x]+1);
}
}
else
{
if(root(x)==root(y))
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}