Pagini recente » Cod sursa (job #368993) | Cod sursa (job #282700) | Cod sursa (job #159498) | Cod sursa (job #2365167) | Cod sursa (job #2027913)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#define mod 30011
//i'm never leaving this project
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,i,j,v[100005],father[100005],rang[100005],x,y,X,Y,m,tip,xx,yy;
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==2)
{
while(father[x]) x=father[x];
while(father[y]) y=father[y];
if(x==y) g<<"DA\n";
else g<<"NU\n";
}
else
{
X=x; Y=y;
while(father[X]) X=father[X];
while(father[Y]) Y=father[Y];
if(rang[X]>=rang[Y])
{
father[Y]=X;
rang[X]=max(rang[Y]+1,rang[X]);
while(father[x])
{
xx=father[x];
father[x]=X;
x=xx;
}
}
else
{
father[X]=Y;
rang[Y]=max(rang[X]+1,rang[Y]);
while(father[y])
{
yy=father[y];
father[y]=Y;
y=yy;
}
}
}
}
return 0;
}