Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #141489) | Cod sursa (job #1792024) | Cod sursa (job #3150667)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>
#include <bitset>
using namespace std;
#define zeros(x) ((x ^ (x - 1)) & x)
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, m;
int t[100001], r[100001];
void read() {
f>>n>>m;
for(int i = 1;i <= n;++i) {
t[i] = i;
r[i] = 1;
}
}
void unite(const int& x, const int& y) {
if(r[x] > r[y]) {
r[x] += r[y];
t[y] = x;
return;
}
r[y] += r[x];
t[x] = y;
}
int find(const int& x) {
if(x == t[x]) {
return x;
}
return t[x] = find(t[x]);
}
void solve() {
int x, y, z;
for(int i = 1;i <= m;++i) {
f>>x>>y>>z;
if(x == 1) {
unite(find(y), find(z));
} else {
g<<(find(y) == find(z) ? "DA\n" : "NU\n");
}
}
}
int main() {
read();
solve();
return 0;
}