Pagini recente » Cod sursa (job #1125744) | Borderou de evaluare (job #1794951) | Cod sursa (job #1197624) | Cod sursa (job #1197626) | Cod sursa (job #2374818)
#include <iostream>
#include <fstream>
#define Nmax 100020
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int comp[Nmax],grad[Nmax];
int N,M;
int find(int x)
{
int R,next;
for(R=x;comp[R]!=R;R=comp[R]);
for(;comp[x]!=x;)
{
next=comp[x];
comp[x]=R;
x=next;
}
return R;
}
void unire(int x,int y)
{
if(grad[y]<grad[x])
comp[y]=x;
else comp[x]=y;
if(grad[x]==grad[y])grad[y]++;
}
int main()
{
fin>>N>>M;
for(int i=1;i<=N;++i)
{
comp[i]=i;
grad[i]=1;
}
int op,x,y;
for(int i=1;i<=M;++i)
{
fin>>op>>x>>y;
if(op==2)
{
if(find(x)==find(y))fout<<"DA\n";
else fout<<"NU\n";
}
else
{
if(find(x)==find(y))return 0;
unire(find(x),find(y));
}
}
return 0;
}