Pagini recente » Cod sursa (job #2029921) | Cod sursa (job #821843) | Cod sursa (job #2051470) | Cod sursa (job #1355239) | Cod sursa (job #2942379)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int n, m;
vector < int > v, cnt;
int main()
{
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f >> n >> m;
v.resize( n + 1 );
for( int i = 1; i <= n; ++i )
v[i] = i;
cnt = vector < int > ( n + 1, 1 );
for( int i = 0; i < m; ++i )
{
int op, x, y;
f >> op >> x >> y;
if( op == 1 )
{
if( cnt[ v[x] ] >= cnt[ v[y] ] )
{
cnt[ v[x] ] += cnt[ v[y] ];
cnt[ v[y] ] = 0;
int multime = v[y];
for( int j = 1; j <= n; ++j )
if( v[j] == multime )
v[j] = v[x];
}
else
{
cnt[ v[y] ] += cnt[ v[x] ];
cnt[ v[x] ] = 0;
int multime = v[x];
for( int j = 1; j <= n; ++j )
if( v[j] == multime )
v[j] = v[y];
}
}
else
g << ( ( v[x] == v[y] ) ? "DA\n" : "NU\n" );
}
f.close();
g.close();
return 0;
}