Pagini recente » Cod sursa (job #2651037) | Cod sursa (job #2689767) | Cod sursa (job #291935) | Cod sursa (job #9211) | Cod sursa (job #2049017)
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
const int NMax = 1e5 + 5;
const int inf = 1e9 + 5;
int N,M;
int dad[NMax],nr[NMax];
int findRoot(int);
void unite(int,int);
int main() {
in>>N>>M;
for (int i=1;i <= N;++i) {
dad[i] = i;
nr[i] = 1;
}
while (M--) {
int tip,x,y;
in>>tip>>x>>y;
if (tip == 1) {
unite(x,y);
}
else {
int rootX = findRoot(x);
int rootY = findRoot(y);
if (rootX == rootY) {
out<<"DA\n";
}
else {
out<<"NU\n";
}
}
}
in.close();out.close();
return 0;
}
int findRoot(int node) {
if (dad[node] == node) {
return node;
}
int root = findRoot(dad[node]);
dad[node] = root;
return root;
}
void unite(int x,int y) {
int rootX = findRoot(x),
rootY = findRoot(y);
if (nr[rootX] > nr[rootY]) {
nr[rootX] += nr[rootY];
nr[rootY] = 0;
dad[rootY] = rootX;
}
else {
nr[rootY] += nr[rootX];
nr[rootX] = 0;
dad[rootX] = rootY;
}
}