Pagini recente » Cod sursa (job #2304924) | Cod sursa (job #25850) | Cod sursa (job #2074586) | Cod sursa (job #2448441) | Cod sursa (job #3154084)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
struct DSU {
vector<int> p, s;
void init(int n) {
p.resize(n+1);
s.resize(n+1);
for(int i = 1; i <= n; i++) {
p[i] = i;
s[i] = 1;
}
}
int f(int x) {
if(x == p[x])
return x;
return p[x] = f(p[x]);
}
void unite(int x, int y) {
x = f(x);
y = f(y);
if(x == y)
return;
if(s[x] > s[y])
swap(x, y);
p[x] = y;
s[x] += s[y];
}
};
int main()
{
int n, m;
fin >> n >> m;
DSU morbius;
morbius.init(n);
for(int i = 1; i <= m; i++) {
int n1, n2, c;
fin >> c >> n1 >> n2;
if(c == 1)
morbius.unite(n1, n2);
else if(morbius.f(n1) == morbius.f(n2))
fout << "DA" << '\n';
else
fout << "NU" << '\n';
}
return 0;
}