artemgf

Раскраска карты

Nov 14th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <set>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <stdio.h>
  9. #include <cmath>
  10. #include <math.h>
  11. #include <queue>
  12. #include <stack>
  13. #include <climits>
  14. #include <deque>
  15. #include <ctime>
  16.  
  17. using namespace std;
  18.  
  19. typedef long long ll;
  20. typedef unsigned long long ull;
  21. typedef unsigned int ui;
  22.  
  23. #define mh() make_heap()
  24. #define poph() pop_heap()
  25. #define pushh() push_heap()
  26. #define sor(n) n.begin(), n.end()
  27. #define mp make_pair
  28.  
  29. const int maxn = 1000001;
  30. bool used[maxn];
  31. int color[maxn];
  32. vector<int> graf[maxn];
  33.  
  34. bool bfs(int start)
  35. {
  36.     queue <int>next;
  37.     next.push(start);
  38.     used[start] = 1;
  39.     color[start] = 0;
  40.     bool good = true;
  41.     while (!next.empty())
  42.     {
  43.         int now = next.front();
  44.         next.pop();
  45.         for (int i = 0; i < graf[now].size(); i++)
  46.         {
  47.             if (!used[graf[now][i]])
  48.             {
  49.                 used[graf[now][i]] = 1;
  50.                 color[graf[now][i]] = !color[now];
  51.                 next.push(graf[now][i]);
  52.             }
  53.             else
  54.                 if (color[graf[now][i]] == color[now])
  55.                     good = false;
  56.         }
  57.     }
  58.     return good;
  59. }
  60. int main()
  61. {
  62.     ll n;
  63.     ll prom;
  64.     cin >> n;
  65.    
  66.     for (int i = 1; i <= n; i++)
  67.     {
  68.         cin >> prom;
  69.         while (prom != 0)
  70.         {
  71.             graf[i].push_back(prom);
  72.             graf[prom].push_back(i);
  73.             cin >> prom;
  74.         }
  75.     }
  76.     bool good = true;
  77.  
  78.     good = bfs(1);
  79.     if (good)
  80.     {
  81.         for (int i = 1; i <= n; i++)
  82.             cout << color[i];
  83.     }
  84.     else
  85.         cout << -1;
  86.     system("pause");
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment