Advertisement
Guest User

Untitled

a guest
Dec 18th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int *id,N;
  5.  
  6. int connected(int p,int q);
  7. int root(int i);
  8. void unn(int p,int q);
  9.  
  10. int main()
  11. {
  12. FILE* file=fopen("a.txt","r");
  13. int i,p,q,c;
  14. fscanf(file,"%d",&N);
  15.  
  16. id=(int *)malloc(N*sizeof(int));
  17.  
  18. for(i=0;i<N;i++)
  19. *(id+i)=i;
  20.  
  21. while(!feof(file))
  22. {
  23. if(2 == fscanf(file,"%d %d",&p,&q))
  24. if(!connected(p,q))
  25. unn(p,q);
  26. }
  27. fclose(file);
  28. c=1;
  29.  
  30. while(c==1)
  31. {
  32. scanf("%d %d",&p,&q);
  33. printf("%d\nYes(1) or No(0) ",connected(p,q));
  34. scanf("%d",&c);
  35. }
  36.  
  37. return 0;
  38. }
  39.  
  40. int connected(int p,int q)
  41. {
  42. return((root(p))==(root(q)));
  43. }
  44.  
  45. void unn(int p,int q)
  46. {
  47. int j=root(q);
  48. int i=root(p);
  49. *(id+j)=i;
  50. }
  51.  
  52. int root(int i)
  53. {
  54. while(i!=(*(id+i)))
  55. i=*(id+i);
  56. return(i);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement