Pagini recente » Cod sursa (job #77857) | Cod sursa (job #463182) | Cod sursa (job #650303) | Cod sursa (job #359926) | Cod sursa (job #2199473)
#include <fstream>
#define Nmax 100005
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int rg[Nmax], t[Nmax];
int n, m, c, x, y;
int find ( int x )
{
int R, y;
for ( R = x ; t[R]!=R ; R=t[R] );
for(;t[x]!=x;)
{
y=t[x];
t[x]=R;
x=y;
}
return R;
}
void unite(int x, int y)
{
if(rg[x]>rg[y])
t[y]=x;
else
t[x]=y;
if(rg[x]==rg[y])
rg[y]++;
}
int main()
{
f >> n >> m;
for ( int i = 1 ; i <= n ; i ++ )
t[i]=i, rg[i]=1;
while(m--)
{
f >> c >> x >> y;
if ( c == 2 )
{
if(find(x)==find(y)) g << "DA\n";
else g << "NU\n";
}
else
unite(find(x), find(y));
}
return 0;
}