Advertisement
1WaKa_WaKa1

Task_O

May 18th, 2022
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <vector>
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. bool answer = true;
  9. unordered_map<short, vector<short>> neighbours;
  10. unordered_map<short, short> color;
  11.  
  12. inline char invert(short c) {
  13.     return c == 1 ? 2 : 1;
  14. }
  15.  
  16. void search(short v, short c) {
  17.     color[v] = c;
  18.  
  19.     for (short u: neighbours[v]) {
  20.         if (color[u] == 0) {
  21.             search(u, invert(c));
  22.         } else if (color[u] == c) {
  23.             answer = false;
  24.         }
  25.     }
  26. }
  27.  
  28. int main() {
  29.     //Ввод данных
  30.     short n, m;
  31.     cin >> n >> m;
  32.  
  33.     for (int i = 0; i < m; i++) {
  34.         short temp1, temp2;
  35.         cin >> temp1 >> temp2;
  36.  
  37.         if (color.count(temp1) == 0) {
  38.             color.insert(make_pair(temp1, 0));
  39.         }
  40.         if (color.count(temp2) == 0) {
  41.             color.insert(make_pair(temp2, 0));
  42.         }
  43.  
  44.         if (neighbours.count(temp1) == 0){
  45.             vector<short> top = {};
  46.             top.push_back(temp2);
  47.             neighbours.insert(make_pair(temp1, top));
  48.         } else {
  49.             neighbours[temp1].push_back(temp2);
  50.         }
  51.     }
  52.  
  53.     //Тело программы
  54.     for (short i = 0; i < color.size(); i++) {
  55.         if (color[i] == 0) {
  56.             search(i, 1);
  57.         }
  58.     }
  59.  
  60.     //Вывод ответа
  61.     if (answer){
  62.         cout << "YES";
  63.     } else cout << "NO";
  64.     return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement