Pagini recente » Cod sursa (job #2400396) | Cod sursa (job #2243574) | Cod sursa (job #1057216) | Cod sursa (job #134419) | Cod sursa (job #2026429)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX = 100000 + 5;
int n, m;
int root[NMAX], rang[NMAX];
void read()
{
fin >> n >> m;
for (int i = 1; i <= n; ++i)
{
root[i] = i;
rang[i] = 1;
}
}
int find_root(int x)
{
int i;
for (i = x; i != root[i]; i = root[i]);
while (x != root[x])
{
int y = root[x];
root[x] = i;
x = y;
}
return i;
}
void unite(int x, int y)
{
if (rang[x] > rang[y])
{
rang[x] += rang[y];
root[y] = x;
}
else
{
rang[y] += rang[x];
root[x] = y;
}
}
int main()
{
int cod, x, y;
read();
for (int i = 1; i <= m; ++i)
{
fin >> cod >> x >> y;
if (cod == 1)
{
if (find_root(x) != find_root(y))
unite(find_root(x), find_root(y));
}
else
{
if (find_root(x) == find_root(y))
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}