achulkov2

Untitled

Apr 15th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. vector<vector<int>> graph;
  7.  
  8. void dfs(int v, int num) {
  9. color[v] = num;
  10. for (int to : graph[v]) {
  11. if (color[to] == 0) {
  12. dfs(to, (num * -1));
  13. } else if (color[to] == num) {
  14. cout << "NO" << endl;
  15. exit(0);
  16. }
  17. }
  18. }
  19.  
  20. int main() {
  21. int n, m, a, b;
  22. cin >> n >> m;
  23. graph.resize(n + 1);
  24. color.assign(n + 1, 0);
  25. for (int j = 0; j < m; j++) {
  26. cin >> a >> b;
  27. --a;
  28. --b;
  29. graph[a].push_back(b);
  30. graph[b].push_back(a);
  31. }
  32. for (int i = 0; i < n; i++) {
  33. if (color[i] == 0) {
  34. dfs(i, 1);
  35. }
  36. }
  37. cout << "YES" << endl;
  38. }
Add Comment
Please, Sign In to add comment