Pagini recente » Cod sursa (job #1185543) | Cod sursa (job #1135026) | Cod sursa (job #107359) | Cod sursa (job #1579451) | Cod sursa (job #1945534)
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int NMax = 1e5 + 5;
const int inf = 2e9 + 5;
typedef long long ll;
int N,M;
int dad[NMax],nr[NMax];
void unite(int,int);
int findRoot(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: {
if (findRoot(x) == findRoot(y)) {
out<<"DA\n";
}
else {
out<<"NU\n";
}
}
}
}
in.close();out.close();
return 0;
}
void unite(int a,int b) {
int root1 = findRoot(a),
root2 = findRoot(b);
if (nr[root1] > nr[root2]) {
nr[root1] += nr[root2];
dad[root2] = root1;
}
else {
nr[root2] += root1;
dad[root1] = root2;
}
}
int findRoot(int n) {
if (dad[n] == n) {
return n;
}
dad[n] = findRoot(dad[n]);
return dad[n];
}