Pagini recente » Cod sursa (job #2640342) | Cod sursa (job #826956) | Cod sursa (job #2070687) | Cod sursa (job #2379601) | Cod sursa (job #1286290)
// I WAS LOOKING AT ALL THE LIFE.
// THERE WERE PLANTS AND BIRDS AND ROCKS AND THINGS.
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <map>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int head[100000 + 10], rang[100000 + 10];
int find (int nod) {
int r, y;
for (r = head[nod]; r != head[r]; r = head[r]);
while (head[nod] != r) {
y = head[nod];
head[nod] = r;
nod = y;
}
return r;
}
void unite (int a, int b) {
if (rang[a] >= rang[b])
head[b] = head[a];
else
head[a] = head[b];
if (rang[a] == rang[b])
++rang[a];
}
int n, m;
int main() {
fin >> n >> m;
for (int i = 1; i <= n; ++i)
head[i] = i;
for (int i = 1; i <= m; ++i) {
int t, x, y;
fin >> t >> x >> y;
if (t == 2)
fout << (find(x) == find(y) ? "DA" : "NU") << "\n";
else
unite(find(x), find(y));
}
return 0;
}