Pagini recente » Istoria paginii runda/preojigim/clasament | Cod sursa (job #1387503) | Cod sursa (job #2225112) | Cod sursa (job #2385770) | Cod sursa (job #2783546)
#include <iostream>
#include <fstream>
#define NMAX 100000
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, x, y, opt;
int parent[NMAX], rang[NMAX];
int gaseste(int x)
{
if (parent[x] == x) return x;
return parent[x] = gaseste(parent[x]);
}
void uneste(int i, int j)
{
int a = gaseste(i), b = gaseste(j);
if (rang[a] < rang[b]) parent[a] = b;
else if (rang[a] > rang[b]) parent[b] = a;
else
{
parent[b] = a;
rang[a]++;
}
}
int main ()
{
fin >> n >> m;
for (int i = 0; i < n; ++i)
parent[i] = i;
for (int i = 0; i < m; ++i)
{
fin >> opt >> x >> y;
--x, --y;
if (opt == 1) uneste(x, y);
else fout << (gaseste(x) == gaseste(y) ? "DA" : "NU") << '\n';
}
fin.close();
fout.close();
return 0;
}