Pagini recente » Cod sursa (job #1080373) | Cod sursa (job #220889) | Cod sursa (job #2329977) | Cod sursa (job #2537971) | Cod sursa (job #2609854)
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int Find(int x, int *c) {
int y = x, t;
while(y != c[y])
y = c[y];
while(x != y) {
t = c[x];
c[x] = y;
x = t;
}
return x;
}
void Unite(int x, int y, int *c) {
x = Find(x, c);
y = Find(y, c);
c[y] = x;
}
int main(){
int n,m ,*c,type,a,b;
in >>n>>m;
c = (int *) calloc(n + 1, sizeof(int));
for(int i = 1; i <= n; i++)
c[i] = i;
for(int i=1;i<=m;i++){
in >>type>>a>>b;
if(type==1){
if((a = Find(a, c)) != (b = Find(b, c)))
Unite(a, b, c);
}else{
if(Find(a, c) == Find(b, c)) {
out <<"DA\n";
} else {
out <<"NU\n";
}
}
}
}