Pagini recente » Cod sursa (job #145492) | Cod sursa (job #114810) | Cod sursa (job #1474644) | Cod sursa (job #3226706) | Cod sursa (job #2478455)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
vector < int > rang, t;
int n, m, x, y, op;
void citire();
void leg ( int a, int b )
{
while ( t[b] != b)
{
b = t[b] ;
t[b] = a;
}
}
int cauta( int a )
{
while ( t[a]!= a )
a = t[a];
return a;
}
bool is()
{
int k1 = cauta(x);
int k2 = cauta(y);
leg(k1,x);
leg(k2,y);
return (k1==k2);
}
void reunesc()
{
int k1 = cauta(x), k2 = cauta(y);
leg(k1, x);
leg(k2, y);
while ( t[y] != y)
y = t[y];
if ( rang[x] == rang [y] )
{
if ( x < y )
{
rang [x]++;
t[y] = x;
}
else
{
rang [y]++;
t[x] = y;
}
}
else if ( rang [x] > rang [y] )
t[y] = x;
else t[x] = y;
}
int main()
{
citire();
return 0;
}
void citire()
{
fin >> n >> m;
rang.reserve(n+2);
t.reserve(n+2);
for ( int i = 1; i <= m; i++ )
{
fin >> op >> x >> y;
if ( t[x] == 0) t[x] = 1;
if ( t[y] == 0) t[y] = 1;
if ( op == 1 )
reunesc();
else
fout << is();
}
}