#include <bits/stdc++.h>
using namespace std;
const int N = 200200;
int n, m;
int a[N];
vector<pair<int, int>> g[N];
int visit[N], tt;
int ans[N];
vector<int> path;
bool dfs(int u, int trg) {
if (u == trg) {
return true;
}
if (visit[u % n] == tt) {
return false;
}
visit[u % n] = tt;
for (auto e : g[u]) {
int v, id;
tie(v, id) = e;
path.push_back(id);
if (dfs(v, trg)) {
return true;
}
path.pop_back();
}
path.pop_back();
return false;
}
bool go(int u) {
visit[u] = ++tt;
for (auto e : g[u]) {
int v, id;
tie(v, id) = e;
if (v == u - 1) {
continue;
}
path.push_back(id);
if (dfs(v, u + n)) {
return true;
}
path.pop_back();
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int l, r;
cin >> l >> r;
if (l > r) {
++a[l], --a[n + 1];
++a[1], --a[r + 1];
--l, r += n;
g[l].emplace_back(r, i);
g[l + n].emplace_back(n + n, i);
} else {
++a[l], --a[r + 1];
l--;
g[l].emplace_back(r, i);
g[l + n].emplace_back(r + n, i);
}
}
for (int i = 1; i <= n; ++i) {
a[i] += a[i - 1];
if (a[i] == 1) {
cout << "impossible\n";
return 0;
}
}
for (int i = 1; i <= n; ++i) {
if (a[i] > 2) {
g[i].emplace_back(i - 1, 0);
g[i + n].emplace_back(i - 1 + n, 0);
}
}
for (int i = 0; i < n; ++i) {
if (go(i)) {
for (int v : path) {
ans[v] = 1;
}
for (int j = 1; j <= m; ++j) {
cout << ans[j];
}
return 0;
}
}
cout << "impossible\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
5 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
4992 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Incorrect |
7 ms |
5120 KB |
no wires in direction 1 between segments 6 and 8 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
5 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
4992 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Incorrect |
7 ms |
5120 KB |
no wires in direction 1 between segments 6 and 8 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
5 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
4992 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Incorrect |
7 ms |
5120 KB |
no wires in direction 1 between segments 6 and 8 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
14312 KB |
Output is correct |
2 |
Correct |
6 ms |
5376 KB |
Output is correct |
3 |
Correct |
23 ms |
9200 KB |
Output is correct |
4 |
Correct |
33 ms |
12280 KB |
Output is correct |
5 |
Correct |
75 ms |
15224 KB |
Output is correct |
6 |
Correct |
77 ms |
11128 KB |
Output is correct |
7 |
Incorrect |
67 ms |
15472 KB |
no wires in direction 1 between segments 25977 and 25977 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
5 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
4992 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Incorrect |
7 ms |
5120 KB |
no wires in direction 1 between segments 6 and 8 |
6 |
Halted |
0 ms |
0 KB |
- |