Pagini recente » Cod sursa (job #3288372) | Cod sursa (job #3186114) | Cod sursa (job #1229667) | Flux si cuplaj | Cod sursa (job #1986698)
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#define ll long long
#define pb push_back
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NLIM = 1e5 + 10;
int N, M;
int parent[NLIM];
int findParent( int x )
{
if( parent[x] == x )
return x;
parent[x] = findParent( parent[x] );
return parent[x];
}
int join( int x, int y )
{
int px = findParent( x );
int py = findParent( y );
if( px != py )
{
parent[px] = py;
}
}
int main()
{
fin >> N >> M;
for( int i = 1; i <= N; ++i )
parent[i] = i;
while( M-- )
{
int t, x, y;
fin >> t >> x >> y;
if( t == 1 )
{
join( x, y );
}
else
{
//cout << findParent( x ) << " " << findParent( y ) << "\n";
if( findParent( x ) == findParent( y ) )
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}