Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 100005;
  5. const int red = 1;
  6. const int blue = 2;
  7. vector<int> adj[100005];
  8. bool color[100005];
  9. bool vis[100005];
  10. int par[100005];
  11. bool oddCycle(int u) {
  12. if (vis[u] == true) return false;
  13. queue<int> q;
  14. q.push(u);
  15. color[u] = false;
  16. vis[u] = true;
  17. while (!q.empty()) {
  18. int v = q.front();
  19. q.pop();
  20. for (int i = 0; i < adj[v].size(); i++) {
  21. int w = adj[v][i];
  22. if (!vis[w]) {
  23. vis[w] = true;
  24. q.push(w);
  25. color[w] = !color[v];
  26. }
  27. else{
  28. if(color[w]==color[v]){
  29. return true;
  30. }
  31. }
  32. }
  33. }
  34. return false;
  35. }
  36.  
  37. int main() {
  38. int n, e;
  39. scanf("%d %d", &n, &e);
  40. for (int i = 0; i < n; i++) par[i] = -1;
  41. for (int i = 0; i < e; i++) {
  42. int u, v;
  43. scanf("%d %d", &v, &u);
  44. adj[u].push_back(v);
  45. adj[v].push_back(u);
  46. }
  47. bool t = false;
  48. for (int i = 0; i < n; i++) {
  49. t = oddCycle(i);
  50. if (t) break;
  51. }
  52. if (t)
  53. printf("Yes\n");
  54. else
  55. printf("No\n");
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement