Pagini recente » Cod sursa (job #803869) | Cod sursa (job #1225871) | Istoria paginii runda/9_3 | Cod sursa (job #443592) | Cod sursa (job #1986864)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
#define ll long long
#define pb push_back
const int NMax = 1e5 + 5;
const ll inf = 9e18 + 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;
switch (tip) {
case 1: {
unite(x,y);
break;
}
case 2: {
out<<( (findRoot(x) == findRoot(y)) ? "DA" : "NU" )<<'\n';
break;
}
}
}
in.close();out.close();
return 0;
}
int findRoot(int node) {
if (dad[node] == node) {
return node;
}
dad[node] = findRoot(dad[node]);
return dad[node];
}
void unite(int x,int y) {
int root1 = findRoot(x),root2 = findRoot(y);
if (nr[root1] > nr[root2]) {
nr[root1] += nr[root2];
//nr[root2] = 0;
dad[root2] = root1;
}
else {
nr[root2] += nr[root1];
//nr[root1] = 0;
dad[root1] = root2;
}
}