Pagini recente » Cod sursa (job #553986) | Cod sursa (job #1235456) | Cod sursa (job #630990) | Cod sursa (job #2103100) | Cod sursa (job #3218487)
#include <fstream>
#include <iostream>
#define NMAX 100005
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int tata[NMAX];
int h[NMAX];
void Union(int x, int y)
{
if (h[x] < h[y])
{
tata[x] = y;
}
else if (h[y] < h[x])
tata[y] = x;
else
{
tata[y] = x;
++h[y];
}
}
int Find(int x)
{
if(tata[x] == 0)
return x;
tata[x] = Find(tata[x]);
return tata[x];
}
int n,m;
int main()
{
in>>n>>m;
int x,y,cod;
for(int i=1;i<=m;i++)
{
in>>cod>>x>>y;
int resx = Find(x);
int resy = Find(y);
switch(cod)
{
case 1:
{
Union(resx,resy);
break;
}
case 2:
{
if(resx == resy)
out<<"DA\n";
else
out<<"NU\n";
break;
}
default:
break;
}
}
}