Skip to content

Instantly share code, notes, and snippets.

@mikebsg01
Created November 10, 2014 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikebsg01/2f81ccc0fe6209e52758 to your computer and use it in GitHub Desktop.
Save mikebsg01/2f81ccc0fe6209e52758 to your computer and use it in GitHub Desktop.
Noticias - 100pts (AC)
#include <bits/stdc++.h>
using namespace std;
int N, M;
int raiz[1000015];
int rango[1000015];
int encontrar(int n) {
if( raiz[n] == n )
return n;
else raiz[n] = encontrar( raiz[n] );
return raiz[n];
}
void unir(int x, int y){
int rX, rY;
rX = encontrar(x);
rY = encontrar(y);
if(rX != rY) {
if(rango[rX] > rango[rY] ){
raiz[rY] = rX;
rango[rX] += rango[rY];
} else {
raiz[rX] = rY;
rango[rY] += rango[rX];
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int i;
char op;
int a, b, nodo, rnodo;
cin>>N>>M;
for(i=0; i<=N; ++i){
raiz[i] = i;
rango[i] = 1;
}
for(i=0; i<M; ++i){
cin>>op;
if(op == 'A'){
cin>>a>>b;
unir(a, b);
} else {
cin>>nodo;
rnodo = encontrar(nodo);
cout<<rango[rnodo]<<"\n";
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment