# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
640391 |
2022-09-14T14:10:03 Z |
chenwz |
Parking (CEOI22_parking) |
C++11 |
|
544 ms |
85384 KB |
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
const int MAXN = 400005;
int n, m, moze = 1;
int b[MAXN], t[MAXN], fa[MAXN], siz[MAXN], InCycle[MAXN], TPCnt[MAXN],
cnt[MAXN];
set<int> Free, G[MAXN];
vector<int> Loc[MAXN];
vector<pair<pi, int> > red;
vector<pi> sol;
void move(int x, int y) {
sol.push_back({x, y});
if (++cnt[y] == 1) Free.erase(y);
if (--cnt[x] == 0) Free.insert(x);
}
int find_set(int x) { return x == fa[x] ? x : fa[x] = find_set(fa[x]); }
void add_edge(int x, int y) {
G[x].insert(y), G[y].insert(x);
TPCnt[y] += (x % 2) && (y % 2);
x = find_set(x), y = find_set(y);
if (x == y)
InCycle[x] = 1;
else
fa[x] = y, siz[y] += siz[x], TPCnt[y] += TPCnt[x];
}
void remove_edge(int x, int y) { G[x].erase(y), G[y].erase(x); }
int get_end_of_chain(int x, int last) {
if (G[x].size() == 1) return x;
assert(G[x].size() == 2);
for (auto v : G[x])
if (v != last) return get_end_of_chain(v, x);
return -1;
}
void solve_path(int idx) {
int curr = get_end_of_chain(idx, -1), last = -1;
vector<int> r;
while (curr != -1) {
r.push_back(curr);
int nxt = -1;
for (auto v : G[curr])
if (v != last) nxt = v;
last = curr, curr = nxt;
}
for (int i = 0, sz = r.size(); i < sz; i++) {
if (r[i + 1] % 2 == 1) {
move(r[i + 1] / 2, r[i] / 2), i++;
continue;
}
int k = -1;
for (int j = i; j < sz - 1; j++) {
if (r[j] % 2 == 1 && r[j + 1] % 2 == 1) {
k = j;
break;
}
}
if (k != -1) {
if (Free.empty()) {
moze = 0;
return;
}
int to = *Free.begin();
move(r[k] / 2, to), move(r[k + 1] / 2, to);
for (int j = k - 1; j >= i; j -= 2) move(r[j - 1] / 2, r[j] / 2);
i = k + 1;
} else {
for (int j = sz - 1; j > i + 1; j -= 2) move(r[j - 1] / 2, r[j] / 2);
move(r[i] / 2, r[i + 1] / 2);
break;
}
}
}
void solve_cycle(int idx) {
if (Free.size() == 0) {
moze = 0;
return;
}
int pocetak = idx, curr = idx, last = -1;
vector<int> r;
while (curr != -1) {
r.push_back(curr);
int nxt = -1;
for (auto v : G[curr]) {
if (v == last || v == pocetak) continue;
nxt = v;
}
last = curr;
curr = nxt;
}
int R = r.size();
int k = -1;
for (int i = 0; i < R; i++) {
if (r[i] % 2 == 1) k = i;
if (r[i] % 2 == 1 && r[(i + 1) % R] % 2 == 1) {
k = i;
break;
}
}
int x = r[k], y = r[(k + 1) % R];
if (y % 2 == 1) {
int to = *Free.begin();
move(x / 2, to);
move(y / 2, to);
remove_edge(r[(k - 1 + R) % R], r[k]);
remove_edge(r[k], r[(k + 1) % R]);
remove_edge(r[(k + 1) % R], r[(k + 2) % R]);
solve_path(r[(k + 2) % r.size()]);
} else {
int to = *Free.begin();
if (x / 2 != y / 2) {
move(x / 2, to);
for (int i = 2; i < R; i += 2) {
move(r[(k - i + R) % R] / 2, r[(k - i + 1 + R) % R] / 2);
}
move(y / 2, to);
} else {
move(x / 2, to);
for (int i = 1; i + 1 < R; i += 2) {
move(r[(k + i + 1) % R] / 2, r[(k + i) % R] / 2);
}
move(r[(k - 1 + R) % R] / 2, to);
}
}
}
void ispis() {}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> b[i] >> t[i];
if (b[i]) Loc[b[i]].push_back(2 * i);
if (t[i]) Loc[t[i]].push_back(2 * i + 1);
fa[2 * i] = 2 * i, siz[2 * i] = 1;
fa[2 * i + 1] = 2 * i + 1, siz[2 * i + 1] = 1;
cnt[i] = (b[i] > 0) + (t[i] > 0);
if (cnt[i] == 0) Free.insert(i);
}
for (int i = 1; i <= n; i++) {
int x = Loc[i][0], y = Loc[i][1];
if (x / 2 != y / 2) add_edge(x, y);
}
for (int i = 0; i < m; i++)
if (b[i] && t[i] && b[i] != t[i]) add_edge(2 * i, 2 * i + 1);
for (int i = 0; i < 2 * m; i++)
if (i == fa[i] && siz[i] > 1) red.push_back({{InCycle[i], TPCnt[i]}, i});
sort(red.begin(), red.end());
for (int i = 0; i < red.size(); i++) {
int idx = red[i].second;
if (!InCycle[idx])
solve_path(idx);
else
solve_cycle(idx);
}
if (moze == 0) {
cout << -1 << '\n';
} else {
cout << sol.size() << endl;
for (auto pp : sol) cout << pp.first + 1 << " " << pp.second + 1 << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:153:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
153 | for (int i = 0; i < red.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
28500 KB |
Output is correct |
2 |
Correct |
14 ms |
28532 KB |
Output is correct |
3 |
Correct |
15 ms |
28512 KB |
Output is correct |
4 |
Correct |
16 ms |
28472 KB |
Output is correct |
5 |
Correct |
14 ms |
28476 KB |
Output is correct |
6 |
Correct |
14 ms |
28480 KB |
Output is correct |
7 |
Correct |
15 ms |
28500 KB |
Output is correct |
8 |
Correct |
14 ms |
28536 KB |
Output is correct |
9 |
Correct |
14 ms |
28504 KB |
Output is correct |
10 |
Correct |
16 ms |
28440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
204 ms |
55900 KB |
Output is correct |
2 |
Correct |
219 ms |
59328 KB |
Output is correct |
3 |
Correct |
175 ms |
52300 KB |
Output is correct |
4 |
Correct |
178 ms |
51480 KB |
Output is correct |
5 |
Correct |
254 ms |
59196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
28756 KB |
Output is correct |
2 |
Correct |
14 ms |
28500 KB |
Output is correct |
3 |
Correct |
15 ms |
28804 KB |
Output is correct |
4 |
Correct |
15 ms |
28756 KB |
Output is correct |
5 |
Correct |
14 ms |
28756 KB |
Output is correct |
6 |
Correct |
15 ms |
28756 KB |
Output is correct |
7 |
Correct |
15 ms |
28756 KB |
Output is correct |
8 |
Correct |
16 ms |
28772 KB |
Output is correct |
9 |
Correct |
14 ms |
28756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
28756 KB |
Output is correct |
2 |
Correct |
14 ms |
28500 KB |
Output is correct |
3 |
Correct |
15 ms |
28804 KB |
Output is correct |
4 |
Correct |
15 ms |
28756 KB |
Output is correct |
5 |
Correct |
14 ms |
28756 KB |
Output is correct |
6 |
Correct |
15 ms |
28756 KB |
Output is correct |
7 |
Correct |
15 ms |
28756 KB |
Output is correct |
8 |
Correct |
16 ms |
28772 KB |
Output is correct |
9 |
Correct |
14 ms |
28756 KB |
Output is correct |
10 |
Correct |
448 ms |
85384 KB |
Output is correct |
11 |
Correct |
91 ms |
39896 KB |
Output is correct |
12 |
Correct |
346 ms |
78176 KB |
Output is correct |
13 |
Correct |
445 ms |
82700 KB |
Output is correct |
14 |
Correct |
303 ms |
80204 KB |
Output is correct |
15 |
Correct |
345 ms |
78292 KB |
Output is correct |
16 |
Correct |
544 ms |
85272 KB |
Output is correct |
17 |
Correct |
423 ms |
78976 KB |
Output is correct |
18 |
Correct |
449 ms |
84692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
28796 KB |
Output is correct |
2 |
Correct |
16 ms |
28712 KB |
Output is correct |
3 |
Correct |
18 ms |
28772 KB |
Output is correct |
4 |
Correct |
16 ms |
28628 KB |
Output is correct |
5 |
Correct |
16 ms |
28512 KB |
Output is correct |
6 |
Correct |
16 ms |
28756 KB |
Output is correct |
7 |
Correct |
16 ms |
28708 KB |
Output is correct |
8 |
Correct |
16 ms |
28756 KB |
Output is correct |
9 |
Correct |
18 ms |
28756 KB |
Output is correct |
10 |
Correct |
17 ms |
28736 KB |
Output is correct |
11 |
Correct |
16 ms |
28756 KB |
Output is correct |
12 |
Correct |
17 ms |
28756 KB |
Output is correct |
13 |
Correct |
14 ms |
28756 KB |
Output is correct |
14 |
Correct |
16 ms |
28756 KB |
Output is correct |
15 |
Correct |
15 ms |
28812 KB |
Output is correct |
16 |
Correct |
19 ms |
28732 KB |
Output is correct |
17 |
Correct |
16 ms |
28720 KB |
Output is correct |
18 |
Correct |
19 ms |
28756 KB |
Output is correct |
19 |
Correct |
15 ms |
28736 KB |
Output is correct |
20 |
Correct |
15 ms |
28756 KB |
Output is correct |
21 |
Correct |
15 ms |
28756 KB |
Output is correct |
22 |
Correct |
16 ms |
28728 KB |
Output is correct |
23 |
Correct |
15 ms |
28692 KB |
Output is correct |
24 |
Correct |
15 ms |
28756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
28500 KB |
Output is correct |
2 |
Correct |
14 ms |
28532 KB |
Output is correct |
3 |
Correct |
15 ms |
28512 KB |
Output is correct |
4 |
Correct |
16 ms |
28472 KB |
Output is correct |
5 |
Correct |
14 ms |
28476 KB |
Output is correct |
6 |
Correct |
14 ms |
28480 KB |
Output is correct |
7 |
Correct |
15 ms |
28500 KB |
Output is correct |
8 |
Correct |
14 ms |
28536 KB |
Output is correct |
9 |
Correct |
14 ms |
28504 KB |
Output is correct |
10 |
Correct |
16 ms |
28440 KB |
Output is correct |
11 |
Correct |
204 ms |
55900 KB |
Output is correct |
12 |
Correct |
219 ms |
59328 KB |
Output is correct |
13 |
Correct |
175 ms |
52300 KB |
Output is correct |
14 |
Correct |
178 ms |
51480 KB |
Output is correct |
15 |
Correct |
254 ms |
59196 KB |
Output is correct |
16 |
Correct |
16 ms |
28756 KB |
Output is correct |
17 |
Correct |
14 ms |
28500 KB |
Output is correct |
18 |
Correct |
15 ms |
28804 KB |
Output is correct |
19 |
Correct |
15 ms |
28756 KB |
Output is correct |
20 |
Correct |
14 ms |
28756 KB |
Output is correct |
21 |
Correct |
15 ms |
28756 KB |
Output is correct |
22 |
Correct |
15 ms |
28756 KB |
Output is correct |
23 |
Correct |
16 ms |
28772 KB |
Output is correct |
24 |
Correct |
14 ms |
28756 KB |
Output is correct |
25 |
Correct |
448 ms |
85384 KB |
Output is correct |
26 |
Correct |
91 ms |
39896 KB |
Output is correct |
27 |
Correct |
346 ms |
78176 KB |
Output is correct |
28 |
Correct |
445 ms |
82700 KB |
Output is correct |
29 |
Correct |
303 ms |
80204 KB |
Output is correct |
30 |
Correct |
345 ms |
78292 KB |
Output is correct |
31 |
Correct |
544 ms |
85272 KB |
Output is correct |
32 |
Correct |
423 ms |
78976 KB |
Output is correct |
33 |
Correct |
449 ms |
84692 KB |
Output is correct |
34 |
Correct |
17 ms |
28796 KB |
Output is correct |
35 |
Correct |
16 ms |
28712 KB |
Output is correct |
36 |
Correct |
18 ms |
28772 KB |
Output is correct |
37 |
Correct |
16 ms |
28628 KB |
Output is correct |
38 |
Correct |
16 ms |
28512 KB |
Output is correct |
39 |
Correct |
16 ms |
28756 KB |
Output is correct |
40 |
Correct |
16 ms |
28708 KB |
Output is correct |
41 |
Correct |
16 ms |
28756 KB |
Output is correct |
42 |
Correct |
18 ms |
28756 KB |
Output is correct |
43 |
Correct |
17 ms |
28736 KB |
Output is correct |
44 |
Correct |
16 ms |
28756 KB |
Output is correct |
45 |
Correct |
17 ms |
28756 KB |
Output is correct |
46 |
Correct |
14 ms |
28756 KB |
Output is correct |
47 |
Correct |
16 ms |
28756 KB |
Output is correct |
48 |
Correct |
15 ms |
28812 KB |
Output is correct |
49 |
Correct |
19 ms |
28732 KB |
Output is correct |
50 |
Correct |
16 ms |
28720 KB |
Output is correct |
51 |
Correct |
19 ms |
28756 KB |
Output is correct |
52 |
Correct |
15 ms |
28736 KB |
Output is correct |
53 |
Correct |
15 ms |
28756 KB |
Output is correct |
54 |
Correct |
15 ms |
28756 KB |
Output is correct |
55 |
Correct |
16 ms |
28728 KB |
Output is correct |
56 |
Correct |
15 ms |
28692 KB |
Output is correct |
57 |
Correct |
15 ms |
28756 KB |
Output is correct |
58 |
Correct |
387 ms |
77888 KB |
Output is correct |
59 |
Correct |
431 ms |
83012 KB |
Output is correct |
60 |
Correct |
345 ms |
72260 KB |
Output is correct |
61 |
Correct |
420 ms |
80532 KB |
Output is correct |
62 |
Correct |
86 ms |
40280 KB |
Output is correct |
63 |
Correct |
399 ms |
81256 KB |
Output is correct |
64 |
Correct |
389 ms |
78940 KB |
Output is correct |
65 |
Correct |
418 ms |
81904 KB |
Output is correct |
66 |
Correct |
448 ms |
84168 KB |
Output is correct |
67 |
Correct |
302 ms |
77228 KB |
Output is correct |
68 |
Correct |
422 ms |
83436 KB |
Output is correct |
69 |
Correct |
408 ms |
81684 KB |
Output is correct |
70 |
Correct |
329 ms |
77376 KB |
Output is correct |
71 |
Correct |
331 ms |
79976 KB |
Output is correct |
72 |
Correct |
307 ms |
79244 KB |
Output is correct |
73 |
Correct |
440 ms |
85272 KB |
Output is correct |
74 |
Correct |
323 ms |
78456 KB |
Output is correct |
75 |
Correct |
451 ms |
83788 KB |
Output is correct |
76 |
Correct |
486 ms |
84728 KB |
Output is correct |
77 |
Correct |
434 ms |
83092 KB |
Output is correct |
78 |
Correct |
387 ms |
79040 KB |
Output is correct |
79 |
Correct |
481 ms |
82732 KB |
Output is correct |
80 |
Correct |
335 ms |
79384 KB |
Output is correct |
81 |
Correct |
460 ms |
83624 KB |
Output is correct |