Pagini recente » Cod sursa (job #24394) | Cod sursa (job #203993) | Cod sursa (job #2965752) | Cod sursa (job #901226) | Cod sursa (job #3128669)
#include <fstream>
#include <iostream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX = 100000;
int v[NMAX+5];
int find_root(const int &number)
{
if (v[number] < 0) return number;
v[number] = find_root(v[number]);
return v[number];
}
void unite(const int &x, const int &y)
{
int findx = find_root(x), findy = find_root(y);
if (findx > findy) swap(findx, findy);
v[findy] += v[findx];
v[findx] = findy;
}
int main()
{
int n, m, x, y;
short type;
fin>>n>>m;
for (int i = 1; i <= n; i++) v[i] = -1;
while (m--) {
fin>>type>>x>>y;
int findx = find_root(x), findy = find_root(y);
if (type == 1) {
if (findx != findy) unite(findx, findy);
}
else
if (findx != findy) fout<<"NU\n";
else fout<<"DA\n";
}
return 0;
}