Pagini recente » Cod sursa (job #617204) | Cod sursa (job #2329121) | Cod sursa (job #2504291) | Cod sursa (job #467406) | Cod sursa (job #1383935)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int p = 100001;
int v[1000000],i,j,n,m,x,a,b;
int findParents(int x) /// this will find the parent of "x"
{
while( v[ x ] != 0 )
x = v[ x ];
return x;
}
void addParents(int a, int b) /// this will add the parents off "a" and "b" to a bigger parent
{
int x,y;
x = findParents( a );
y = findParents( b );
p = p + 1;
v[ x ] = p;
v[ y ] = p;
}
void answer(int a, int b) /// this will answer if "a" has the same parent with "b"
{
int x,y;
x = findParents( a );
y = findParents( b );
if( x == y )
fout<<"DA\n";
else
fout<<"NU\n";
}
int main()
{
fin>>n>>m;
for(i=1 ; i<=m ; ++i)
{
fin>>x>>a>>b;
if( x == 1 )
addParents( a , b );
else
answer( a , b );
}
return 0;
}