Pagini recente » Cod sursa (job #1369313) | Statistici Gelu Bugnaru (roznek) | Cod sursa (job #2502255) | Profil MihneaMihnea300 | Cod sursa (job #2842316)
#include <bits/stdc++.h>
#define Dmax 100005
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int T[Dmax],R[Dmax];
int root(int x)
{
if(T[x]==x)
return x;
else
{
T[x]=root(T[x]);
return T[x];
}
}
void Union(int x, int y)
{
if(R[x] > R[y])
T[y] = x;
else
T[x] = y;
if(R[x]==R[y])
R[y]++;
}
int main()
{
int n,m;
f>>n>>m;
for(int i = 1; i <= n; i++)
T[i]=i;
for(int i = 1; i <= m; i++)
{
int p,x,y;
f>>p>>x>>y;
if(p==1)
{
if(root(x)!=root(y))
Union(root(x),root(y));
}
else
if(root(x)==root(y))
g<<"DA\n";
else
g<<"NU\n";
}
return 0;
}