Pagini recente » Cod sursa (job #1544081) | Cod sursa (job #3180823) | Cod sursa (job #2661969) | Cod sursa (job #3181111) | Cod sursa (job #2253141)
#include <bits/c++io.h>
#include <fstream>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout("disjoint.out");
#define nmax 100020
int n, m, f[nmax], rang[nmax];
int search(int x)
{
int i, aux;
for ( i = x; f[i] != i; i = f[i]);
for (; f[x] != x;)
{
aux = f[x];
f[x] = i;
x = aux;
}
return i;
}
void unite(int x, int y)
{
if (rang[x] > rang[y])
f[y] = x;
else f[x] = y;
if (rang[x] == rang[y]) rang[y]++;
}
int main()
{
fin >> n >> m;
int x, y, cod;
for (int i = 1; i <= n; ++i)
{
f[i] = i;
rang[i] = 1;
}
for (int i = 1; i <= m; ++i)
{
fin >> cod >> x >> y;
if (cod == 2){
if (search(x) == search(y)) fout << "DA\n";
else fout << "NU\n";
}
else
{
if (search(x) == search(y)) {
fout << i;
return 0;
}
unite(search(x), search(y));
}
}
return 0;
}