Pagini recente » Cod sursa (job #2872968) | Cod sursa (job #1405929) | Cod sursa (job #2316231) | Cod sursa (job #2745853) | Cod sursa (job #1357791)
#include <iostream>
#include <fstream>
#define N 100003
using namespace std;
int t[N];
int v[N];
int n,m;
int Find(int x)
{
if(x==t[x]) return x;
return t[x]=Find(t[x]);
}
void Union(int x,int y)
{
if(v[y]>v[x])
{
t[x]=y;
}
else
{
t[y]=x;
if(v[x]==v[y])
v[x]++;
}
}
int main()
{
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
fin>>n>>m;
int op,x,y;
int i;
for(i=1; i<=n; i++)
t[i]=i;
for(i=1; i<=m; i++)
{
fin>>op>>x>>y;
op--;
if(!op)
Union(Find(x),Find(y));
else
if(Find(x)==Find(y)) fout<<"DA"<<"\n";
else fout<<"NU"<<"\n";
}
return 0;
}