Pagini recente » Cod sursa (job #3353105) | Cod sursa (job #2084462) | Cod sursa (job #2313867) | Cod sursa (job #2136530) | Cod sursa (job #3304554)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
const int maxn = 1e5;
int root[maxn+1], rankof[maxn+1], n, m;
int find( int varf )
{
if( root[varf] == varf )
return varf;
else
{
int ans = find( root[varf] );
root[varf] = ans;
}
}
void myUnion( int a, int b )
{
int roota = find( a );
int rootb = find( b );
if( rankof[roota] == rankof[rootb] )
{
rankof[a] = max( rankof[a], rankof[b]+1 );
root[b] = a;
}
else if( rankof[roota] < rankof[rootb] )
{
rankof[b] = max( rankof[b], rankof[a]+1 );
root[a] = b;
}
else
{
rankof[a] = max( rankof[a], rankof[b]+1 );
root[b] = a;
}
}
int main() {
ios_base::sync_with_stdio(false);
f.tie(nullptr);
g.tie(nullptr);
f >> n >> m;
for( int i = 1; i <= n; ++i )
{
root[i] = i;
rankof[i] = 0;
}
for( int i = 1; i <= m; ++i )
{
int c, x, y;
f >> c >> x >> y;
if( c == 1 )
{
myUnion( x, y );
}
else
{
if( find(x) == find(y) )
g << "DA\n";
else
g << "NU\n";
}
}
return 0;
}