Pagini recente » Monitorul de evaluare | Cod sursa (job #1235417) | Monitorul de evaluare | Cod sursa (job #1235410) | Cod sursa (job #1235418)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
#define cout g
#define nmax 100010
int n,m,i,x,y,tip;
int T[nmax],R[nmax];
int rad(int x)
{
int y,r=x;
while (x!=T[x]) x=T[x];
for(;T[x]!=x;)
{
y=T[x];
T[x]=r;
x=y;
}
return x;
}
void uneste(int a,int b)
{
if (R[a]>=R[b]) T[b]=a;
else T[a]=b;
if(R[a]==R[b]) ++R[a];
}
int main()
{
f>>n>>m;
for(i=1;i<=n;++i) T[i]=i,R[i]=1;
for(;m;--m)
{
f>>tip>>x>>y;
if(tip==1) uneste(rad(x),rad(y));
else if(rad(x)==rad(y)) cout<<"DA\n";
else cout<<"NU\n";
}
return 0;
}