Pagini recente » Cod sursa (job #1901753) | Cod sursa (job #1758160) | Cod sursa (job #791564) | Cod sursa (job #1598881) | Cod sursa (job #2514573)
#include <bits/stdc++.h>
#define nmax 100001
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int x,y,xx,yy,cost,cost_total,k,n,m,i,t[nmax],r[nmax],cod;
int findset(int x)
{
int nod = x;
while (t[x]!=x)
x=t[x];
while (t[nod]!=nod)
{
int aux = t[nod];
t[nod]=x;
nod = aux;
}
return x;
}
void union_set(int x,int y)
{
if (r[x]>r[y])
t[y]=x;
else if (r[y]>r[x])
t[x]=y;
else
{
r[x]++;
t[y]=x;
}
}
int main()
{
in>>n>>m;
for (i=1; i<=n; ++i)
{
t[i]=i;
r[i]=1;
}
for (i=1; i<=m; ++i)
{
in>>cod>>xx>>yy;
int x = findset(xx);
int y = findset(yy);
if(cod==1)
{
union_set(x,y);
}
else
{
if(x==y)
out<<"DA";
else
out<<"NU";
out<<'\n';
}
}
}