Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<vector>
  3. using namespace std;
  4. vector<int> head(100050,0);
  5. int root(int a){
  6. if(head[a]==a) return a;
  7. return head[a]=root(head[a]);
  8. }
  9. void mer(int a,int b){
  10. head[b]=root(a);
  11. }
  12. bool check(int a,int b){
  13. return root(a)==root(b);
  14. }
  15. int main()
  16. {
  17. int n,m;
  18. bool k=false;
  19. scanf("%d%d",&n,&m);
  20. for(int i=1;i<=n;i++) head[i]=i;
  21. int u,v;
  22. while(m--){
  23. scanf("%d%d",&u,&v);
  24. if(check(u,v)) k=true;
  25. mer(u,v);
  26. }
  27. if(k) printf("Yes");
  28. else printf("No");
  29. return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement