1
0

AtCoder-agc056_c.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. ios::sync_with_stdio(false);
  5. cin.tie(nullptr);
  6. int n, m;
  7. cin >> n >> m;
  8. vector<vector<pair<int, int>>> graph(n + 1);
  9. for (int i = 0; i < n; ++i) {
  10. graph[i].push_back({i + 1, 1});
  11. graph[i + 1].push_back({i, 1});
  12. }
  13. for (int i = 0; i < m; ++i) {
  14. int l, r;
  15. cin >> l >> r;
  16. --l;
  17. graph[l].push_back({r, 0});
  18. graph[r].push_back({l, 0});
  19. }
  20. const int inf = numeric_limits<int>::max() / 4;
  21. vector<int> distance(n + 1, inf);
  22. deque<int> queue;
  23. distance[0] = 0;
  24. queue.push_back(0);
  25. while (!queue.empty()) {
  26. int u = queue.front();
  27. queue.pop_front();
  28. for (auto [v, weight] : graph[u]) {
  29. if (distance[v] <= distance[u] + weight) continue;
  30. distance[v] = distance[u] + weight;
  31. if (weight == 0) queue.push_front(v);
  32. else queue.push_back(v);
  33. }
  34. }
  35. string answer;
  36. answer.reserve(n);
  37. for (int i = 1; i <= n; ++i) {
  38. answer.push_back(distance[i] > distance[i - 1] ? '0' : '1');
  39. }
  40. cout << answer << '\n';
  41. return 0;
  42. }