Pagini recente » Statistici capalnean Sorana (Sorana) | Cod sursa (job #1260265) | Cod sursa (job #1184474) | Cod sursa (job #1180425) | Cod sursa (job #2414893)
#include <iostream>
#include <fstream>
using namespace std;
const int NMAX=100005;
int tata[NMAX];
int h[NMAX];
int find_father(int x)
{
if(x==tata[x])
return x;
return find_father(tata[x]);
tata[x]=tata[tata[x]];
/*int t=x;
int aux;
while(tata[t]!=t)
t=tata[t];
while(tata[x]!=t)
{
aux=tata[x];
tata[x]=t;
x=aux;
}
return t;*/
}
void reunite(int x, int y)
{
if(h[x]>=h[y])
{
tata[y]=x;
h[x]++;
}
else
{
tata[x]=y;
h[y]++;
}
}
int main()
{
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int N,M,i,op,x,y;
in>>N>>M;
for(i=1;i<=N;i++)
{
tata[i]=i;
h[i]=1;
}
for(i=1;i<=M;i++)
{
in>>op>>x>>y;
if(op==1)
reunite(find_father(x),find_father(y));
if(op==2)
if(find_father(x)==find_father(y))
out<<"DA"<<endl;
else
out<<"NU"<<endl;
}
return 0;
}