Cod sursa(job #1741061)

Utilizator Bulgaru_Robert_Razvan_323CBBulgaru Robert Razvan Bulgaru_Robert_Razvan_323CB Data 12 august 2016 21:36:06
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>

using namespace std;
 
int parent[100001];
int ranks[100001];
 
void make_set(int x) {
  parent[x]=x;
  ranks[x]=0;
}
 
int find_set(int x) {
  if(x!=parent[x]) {
    parent[x]=find_set(parent[x]);
  }
 
  return parent[x];
}
 
void union_set(int x, int y) {
  int px=find_set(x);
  int py=find_set(y);
 
  if(px>py) {
    parent[py]=px;
  }
  else if(py>px) {
    parent[px]=py;
  }
  
  else {
    ranks[py]=px;
    ranks[px]++;
  }
}
 
 
int main() {
ifstream in("disjoint.in");
ofstream out("disjoint.out");
 
int N,M,op,x,y;
 
in>>N>>M;
 
for(int i=1; i<N; i++) {
  make_set(i);
}
 
for(int i=1; i<=M; i++) {
  in>>op>>x>>y;
  if(op) {
    union_set(x,y);
  }
  else {
    if (find_set(x)==find_set(y)) {
      out<<"DA\n";
    }
    else {
      out<<"NU\n";
    }
  }
}
 
 
return 0;
}