Pagini recente » Cod sursa (job #956489) | Cod sursa (job #2840430) | Cod sursa (job #901317) | Cod sursa (job #1006532) | Cod sursa (job #2865045)
#include <iostream>
#include <fstream>
#define NMAX 100001
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, p[NMAX], depth[NMAX];
int sef(int x)
{
int t, s=x;
while(s!=p[s])
s=p[s];
while(x!=s)
{
t=p[x];
p[x]=s;
x=t;
}
return s;
}
void add(int x, int y)
{
x=sef(x);
y=sef(y);
p[y]=x;
}
void debug(int step)
{
cout<<"pas "<<step<<'\n';
cout<<"nod : ";
for(int i=1; i<=n; i++)
cout<<i<<" ";
cout<<"\nsefi: ";
for(int i=1; i<=n; i++)
cout<<sef(i)<<" ";
}
int main()
{
int type, x, y;
fin>>n>>m;
for(int i=1; i<=n; i++)
p[i]=i;
for(int oji=1; oji<=m; oji++)
{
//debug(oji);
fin>>type>>x>>y;
if(type==1)
//if(sef(x)!=sef(y))
add(x, y);
else
if(sef(x)==sef(y))
fout<<"DA\n";
else
fout<<"NU\n";
//cout<<"\n-------------------\n";
}
return 0;
}