Pagini recente » Profil tabara | Cod sursa (job #1597190) | Cod sursa (job #1513256) | Profil AnaCirligeanu1 | Cod sursa (job #1994454)
#include <iostream>
#include <fstream>
#include <cstring>
#define mod 666013
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,i,j,v[100005],father[100005],rang[100005],tip,m,f1,f2,x,y,aux;
int main()
{
f>>n>>m;
for(i=1;i<=n;i++) rang[i]=1;
for(i=1;i<=m;i++)
{
f>>tip>>x>>y;
if(tip==1)
{
f1=x;
f2=y;
while(father[f1]!=0) f1=father[f1];
while(father[f2]!=0) f2=father[f2];
if(rang[f1]>=rang[f2]) father[f2]=f1;
else father[f1]=f2;
}
else
{
f1=x;
f2=y;
while(father[f1]!=0) f1=father[f1];
while(father[f2]!=0) f2=father[f2];
if(f1==f2) g<<"DA\n";
else g<<"NU\n";
while(x!=f1 && father[x]!=f1)
{
aux=father[x];
father[x]=f1;
x=aux;
}
while(y!=f2 && father[y]!=f2)
{
aux=father[y];
father[y]=f2;
y=aux;
}
}
}
return 0;
}