Pagini recente » Cod sursa (job #1553938) | Cod sursa (job #1699268) | Cod sursa (job #3001844) | Cod sursa (job #1749432) | Cod sursa (job #1410853)
#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;
return find(p[x]);
/*
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;
}