Pagini recente » Cod sursa (job #395559) | Cod sursa (job #220324) | Cod sursa (job #2924225) | Cod sursa (job #2557464) | Cod sursa (job #1506353)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m,t[100001],h[100001],o,x,y,x1,y1,r1,r2,c;
int main()
{
int i;
f>>n>>m;
for (i=1;i<=n;i++)
h[i]=1,t[i]=i;
for (i=1;i<=m;i++)
{
f>>o>>x>>y;
r1=x;
r2=y;
while (r1!=t[r1]) r1=t[r1];
while (r2!=t[r2]) r2=t[r2];
if (o==1)
{
if (h[r1]>h[r2])
t[r2]=r1,c=r1;
else
t[r1]=r2,c=r2;
if (h[r1]==h[r2])
h[c]++;
}
else
{
if (r1==r2)
g<<"DA"<<'\n';
else
g<<"NU"<<'\n';
while (t[x]!=r1)
x1=t[x],t[x]=t[x1],x=x1;
while (t[y]!=r2)
y1=t[y],t[y]=t[y1],y=y1;
}
}
return 0;
}