Pagini recente » Cod sursa (job #2669984) | Cod sursa (job #1790230) | Cod sursa (job #2608979) | Cod sursa (job #1093766) | Cod sursa (job #1237819)
#include <iostream>
#include <fstream>
using namespace std;
ifstream ka("disjoint.in");
ofstream ki("disjoint.out");
const int N_MAX = 100000;
int rad[N_MAX + 1];
int height[N_MAX + 1];
int n, m;
int radacina(int t)
{
int x = t, aux;
while(rad[t] != t)
{
t = rad[t];
}
while(x != rad[x])
{
aux = rad[x];
rad[x] = t;
x = aux;
}
return t;
}
int main()
{
ka >> n >> m;
int c, x, y;
for(int i = 1; i <= n; i++)
rad[i] = i;
for(int i = 1; i <= m; i++)
{
ka >> c >> x >> y;
int x_root = radacina(x);
int y_root = radacina(y);
if(c == 1)
{
if(height[y_root] > height[x_root])
rad[x_root] = y_root;
else
rad[y_root] = x_root;
if(height[x_root] == height[y_root])
height[x_root]++;
}
else
{
if(x_root == y_root)
ki << "DA";
else
ki << "NU";
ki << '\n';
}
}
return 0;
}