Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <string>
- #include <map>
- #include <set>
- #include <algorithm>
- #include <vector>
- #include <stdio.h>
- #include <cmath>
- #include <math.h>
- #include <queue>
- #include <stack>
- #include <climits>
- #include <deque>
- #include <ctime>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef unsigned int ui;
- #define mh() make_heap()
- #define poph() pop_heap()
- #define pushh() push_heap()
- #define sor(n) n.begin(), n.end()
- #define mp make_pair
- const int maxn = 1000001;
- bool used[maxn];
- int color[maxn];
- vector<int> graf[maxn];
- bool bfs(int start)
- {
- queue <int>next;
- next.push(start);
- used[start] = 1;
- color[start] = 0;
- bool good = true;
- while (!next.empty())
- {
- int now = next.front();
- next.pop();
- for (int i = 0; i < graf[now].size(); i++)
- {
- if (!used[graf[now][i]])
- {
- used[graf[now][i]] = 1;
- color[graf[now][i]] = !color[now];
- next.push(graf[now][i]);
- }
- else
- if (color[graf[now][i]] == color[now])
- good = false;
- }
- }
- return good;
- }
- int main()
- {
- ll n;
- ll prom;
- cin >> n;
- for (int i = 1; i <= n; i++)
- {
- cin >> prom;
- while (prom != 0)
- {
- graf[i].push_back(prom);
- graf[prom].push_back(i);
- cin >> prom;
- }
- }
- bool good = true;
- good = bfs(1);
- if (good)
- {
- for (int i = 1; i <= n; i++)
- cout << color[i];
- }
- else
- cout << -1;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment