Pagini recente » Cod sursa (job #2578104) | Cod sursa (job #93170) | Cod sursa (job #238848) | Cod sursa (job #2134868) | Cod sursa (job #2176941)
#include <bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int parent[100005], sz[100005];
int n,m;
int Find( int x )
{
if( parent[x] == x ) return x;
return ( Find(parent[x]));
}
int Union ( int x, int y )
{
x = Find(x);
y = Find(y);
if( sz[x] > sz[y] )
{
parent[y] = x;
sz[x] += sz[y];
}
else
{
parent[x] = y;
sz[y] += sz[x];
}
}
int main()
{
ios::sync_with_stdio(0);
in >> n >> m;
for(int i=1; i<=n; i++) parent[i] = i, sz[i] = 1;
int qw, x,y;
for(int i=1; i<=m; i++)
{
in >> qw >> x >> y;
if( qw == 1 )
{
Union(x,y);
} else
{
if( Find(x) == Find(y) ) out << "DA" << '\n'; else out << "NU" << '\n';
}
}
return 0;
}