Pagini recente » Cod sursa (job #873482) | Cod sursa (job #2229735) | Cod sursa (job #2964813) | Cod sursa (job #5039) | Cod sursa (job #2260553)
#include <bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n,m,fat[100005];
int tata(int x)
{
int r=x;
while(r!=fat[r])
{
r=fat[r];
}
while(x!=fat[x])
{
int c=x;
x=fat[x];
fat[c]=r;
}
return r;
}
int main()
{
in >> n >> m;
for(int i=1; i<=n; i++)
{
fat[i]=i;
}
for(int i=1; i<=m; i++)
{
int x,y,z;
in >> x >> y >> z;
int a=tata(y);
int b=tata(z);
if(x==1)
{
if(a!=b)
fat[a]=b;
}
else
{
if(a==b)
out << "DA\n";
else
out << "NU\n";
}
}
return 0;
}