Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- int *id,N;
- int connected(int p,int q);
- int root(int i);
- void unn(int p,int q);
- int main()
- {
- FILE* file=fopen("a.txt","r");
- int i,p,q,c;
- fscanf(file,"%d",&N);
- id=(int *)malloc(N*sizeof(int));
- for(i=0;i<N;i++)
- *(id+i)=i;
- while(!feof(file))
- {
- if(2 == fscanf(file,"%d %d",&p,&q))
- if(!connected(p,q))
- unn(p,q);
- }
- fclose(file);
- c=1;
- while(c==1)
- {
- scanf("%d %d",&p,&q);
- printf("%d\nYes(1) or No(0) ",connected(p,q));
- scanf("%d",&c);
- }
- return 0;
- }
- int connected(int p,int q)
- {
- return((root(p))==(root(q)));
- }
- void unn(int p,int q)
- {
- int j=root(q);
- int i=root(p);
- *(id+j)=i;
- }
- int root(int i)
- {
- while(i!=(*(id+i)))
- i=*(id+i);
- return(i);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement