fork download
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. bool valid;
  8. void dfs(int node, vector<vector<int>> & graph, vector<int> & color){
  9. for(int child : graph[node]){
  10. if(color[node] == color[child])
  11. valid = false;
  12.  
  13. if(color[child] == -1){
  14. color[child] = !color[node];
  15. dfs(child, graph, color);
  16. }
  17. }
  18.  
  19.  
  20.  
  21. }
  22. int main(){
  23.  
  24. int nodes, from, to;
  25.  
  26. while(cin >> nodes){
  27. valid = true;
  28. if(nodes == 0)
  29. break;
  30. vector<vector<int>> graph(nodes + 1);
  31. vector<int> color(nodes + 1, -1);
  32.  
  33. while(cin >> from >> to){
  34. if(from == 0 && to == 0)
  35. break;
  36.  
  37. graph[from].push_back(to);
  38. graph[to].push_back(from);
  39.  
  40. }
  41. for(int node = 1; node <= nodes; ++node){
  42. if(color[node] == -1){
  43. color[node] = 1;
  44. dfs(node, graph, color);
  45. }
  46. }
  47. if(valid)
  48. cout <<"YES\n";
  49. else
  50. cout <<"NO\n";
  51. }
  52.  
  53. /* */
  54. }
Success #stdin #stdout 0s 4508KB
stdin
4
1 2
1 3
1 4
2 3
2 4
3 4
0 0
6
1 2
1 3
1 6
2 3
2 5
3 4
4 5
4 6
5 6
0 0
0
stdout
NO
NO