Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define MAX 100010
- int vetor[MAX];
- int matriz[MAX];
- int procura(int p,int vetor[]){
- if(vetor[p]==p){
- return p;
- }
- else{
- procura(vetor[p],vetor);
- }
- }//nessa função,eu busco a raíz da fusao(o menor numero a que um numero está ligado).A raiz vai ser //encontrada quando um numero corresponder a sua posição no vetor.
- int main(){
- int a,b;
- char c;
- int x,y;
- int i,j;
- int k;
- int m,n;
- scanf("%d %d",&a,&b);
- for(i=1;i<=a;i++){
- vetor[i]=i;
- }
- for(i=0;i<b;i++){
- scanf(" %c %d %d",&c,&x,&y);
- if(c=='F'){
- if(vetor[x]>vetor[y]){
- vetor[x]=vetor[y];
- }
- else{
- vetor[y]=vetor[x];
- }
- }
- if(c=='C'){
- m=n=0;
- m=procura(x,vetor);
- n=procura(y,vetor);
- if(m==n){//depois de achar as raizes,é só comparar.
- printf("S\n");
- }
- else{
- printf("N\n");
- }
- }
- }
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment