Pagini recente » Cod sursa (job #2661792) | Cod sursa (job #2456306) | Cod sursa (job #2844238) | Cod sursa (job #2818546) | Cod sursa (job #1750836)
#include<bits/stdc++.h>
using namespace std;
#define in f
#define out g
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
int n;
int m;
int x;
int y;
int code;
int aux;
int father[100010];
int rang[100010];
int fin(int x) {
if(father[x] == x) {
return x;
} else {
return fin(father[x]);
}
}
int uni (int x, int y) {
int rooty;
int rootx;
rooty = fin(y);
rootx = fin(x);
if(rang[rootx] == rang[rooty]) {
rang[rootx]++;
father[rooty] = rootx;
} else {
if(rang[rootx] > rang[rooty]) {
father[rootx] = rooty;
} else {
father[rooty] = rootx;
}
}
}
int query (int x, int y) {
int rootx = fin(x);
int rooty = fin(y);
if(rootx == rooty) {
out << "DA" << endl;
} else {
out << "NU" << endl;
}
}
int main() {
in >> n;
in >> m;
for(int i = 1; i <= n; i++) {
father[i] = i;
}
for(int i = 1; i <= m; i++) {
in >> code;
in >> x;
in >> y;
if(code == 1) {
uni(x, y);
}
if(code == 2) {
query(x, y);
}
}
return 0;
}
/*
int find(int x)
{
if(father[x] == x)
{
return x;
}
else
{
return find(father[x]);
}
}
void uniune(int a, int b)
{
int rootX = find(a);
int rootY = find(b);
if(rootX == rootY)
return;
if(ranc[rootX] > ranc[rootY])
father[rootY] = rootX;
else if(ranc[rootX] < ranc[rootY])
father[rootX] = rootY;
else
father[rootX] = rootY;
ranc[rootY]++;
}
*/