Cod sursa(job #2059107)

Utilizator MotoAMotoi Alexandru MotoA Data 6 noiembrie 2017 17:37:22
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int cc[100001],h[100001];

int Find(int x){
 int y=x,z;
 while(cc[y]!=y)y=cc[y];
 while(x!=y){
  z=cc[x];
  cc[x]=y;
  x=z;
 }
 return y;
}

int Find2(int x){
if(cc[x]==x)return x;
cc[x]=Find2(cc[x]);
return cc[x];
}

void Union(int x,int y){
 if(h[x]>h[y])
  cc[y]=x;
 else{
  cc[x]=y;
  if(h[x]==h[y])h[y]++;
 }
}

void citire(){\
int n,m,x,y,op;
f>>n>>m;
for(int i=1;i<=n;i++)cc[i]=i;
for(int i=1;i<=m;i++){
 f>>op>>x>>y;
 x=Find(x);
 y=Find(y);
 if(op==1)Union(x,y);
  else
    if(x==y)g<<"DA\n";
     else g<<"NU\n";
}
}
int main(){
citire();
}