Pagini recente » Cod sursa (job #1671660) | Cod sursa (job #2795213) | Cod sursa (job #2798687) | Cod sursa (job #1591500) | Cod sursa (job #718133)
Cod sursa(job #718133)
#include<iostream>
#include<fstream>
using namespace std;
int main()
{ifstream f("disjoint.in");
ofstream h("disjoint.out");
int n,m,t[100000],r[100000],i,o,x,y,b,a;
f>>n>>m;
for(i=1;i<=n;i++)
{t[i]=i;
r[i]=1;}
for(i=1;i<=m;i++)
{f>>o>>a>>b;
if(o==1)
{ x=a;
while(t[x]!=x)
x=t[x];
y=b;
while(t[y]!=y)
y=t[y];
if(r[x]>r[y])
t[y]=x;
else
if(r[x]==r[y])
{t[y]=x;
r[y]++;}
else
t[x]=y;}
else
{x=a;
while(t[x]!=x)
x=t[x];
y=b;
while(t[y]!=y)
y=t[y];
if(x!=y)
h<<"NU"<<'\n';
else
h<<"DA"<<'\n';} }
return 0;}