Guest User

Untitled

a guest
Jul 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #define MAX 100010
  5.  
  6. int vetor[MAX];
  7. int matriz[MAX];
  8.  
  9. int procura(int p,int vetor[]){
  10. if(vetor[p]==p){
  11. return p;
  12. }
  13. else{
  14. procura(vetor[p],vetor);
  15. }
  16. }//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.
  17.  
  18. int main(){
  19. int a,b;
  20. char c;
  21. int x,y;
  22. int i,j;
  23. int k;
  24. int m,n;
  25.  
  26. scanf("%d %d",&a,&b);
  27. for(i=1;i<=a;i++){
  28. vetor[i]=i;
  29. }
  30. for(i=0;i<b;i++){
  31. scanf(" %c %d %d",&c,&x,&y);
  32. if(c=='F'){
  33. if(vetor[x]>vetor[y]){
  34. vetor[x]=vetor[y];
  35. }
  36. else{
  37. vetor[y]=vetor[x];
  38. }
  39.  
  40. }
  41. if(c=='C'){
  42. m=n=0;
  43. m=procura(x,vetor);
  44. n=procura(y,vetor);
  45.  
  46. if(m==n){//depois de achar as raizes,é só comparar.
  47. printf("S\n");
  48. }
  49. else{
  50. printf("N\n");
  51. }
  52.  
  53.  
  54. }
  55. }
  56.  
  57. system("pause");
  58. return 0;
  59. }
Add Comment
Please, Sign In to add comment