Pagini recente » Cod sursa (job #2010883) | Cod sursa (job #187075) | Cod sursa (job #2019355) | Cod sursa (job #2597129) | Cod sursa (job #1410879)
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#define nmax 100005
using namespace std;
int p[nmax], n, m, op, x, y;
void join(int rootX, int rootY) {
if(abs(p[rootX]) < abs(p[rootY])) swap(rootX, rootY);
p[rootX] += p[rootY];
p[rootY] = rootX;
}
int find(int x) {
if(p[x] < 0) return x;
else {
int sol = find(p[x]);
p[x] = sol;
return sol;
}
}
int main() {
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f>>n>>m;
for(int i=1; i<=n; i++) p[i] = -1;
for(int i=1; i<=m; i++) {
f>>op>>x>>y;
int rootX = find(x);
int rootY = find(y);
if(op == 1) join(rootX, rootY);
if(op == 2)
g<<(rootX == rootY? "DA\n":"NU\n");
}
return 0;
}