#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
void Hollwo_Pelw();
signed main(){
#ifndef hollwo_pelw_local
if (fopen(".inp", "r"))
assert(freopen(".inp", "r", stdin)), assert(freopen(".out", "w", stdout));
#else
using namespace chrono;
auto start = steady_clock::now();
#endif
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = steady_clock::now();
cout << "\nExecution time : " << duration_cast<milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
const int N = 250000, K = 1000, inf = 1e9 + 7;
int n, m, k, len[K], route[N], ord[N];
vector<int> dist[N], adj[N];
inline int get_round(int a, int b, int len) {
return a + (b - a % len + len) % len;
}
void Hollwo_Pelw(){
cin >> n >> m;
for (int i = 0, u, v; i < m; i++) {
cin >> u >> v, -- u, -- v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> k;
len[0] = 1;
for (int i = 1; i <= k; i++) {
cin >> len[i];
for (int j = 0, v; j < len[i]; j++) {
cin >> v, -- v;
ord[v] = j, route[v] = i;
}
}
for (int i = 0; i < n; i++) {
dist[i].resize(len[route[i]], inf);
}
priority_queue<array<int, 3>> pq;
auto update = [&](int u, int d) {
int t = d % len[route[u]];
if ((t != ord[u] || len[route[u]] == 1) && dist[u][t] > d)
dist[u][t] = d, pq.push({-d, u, t});
};
update(0, 0);
while (!pq.empty()) {
int d = -pq.top()[0], u = pq.top()[1], t = pq.top()[2]; pq.pop();
if (dist[u][t] != d) continue;
if (u == n - 1) {
cout << d << '\n';
return ;
}
// cout << u << ' ' << d << '\n';
update(u, d + 1); // stay here
for (int i = 0; i < (int) adj[u].size(); i++) {
// move to neighbor
int &v = adj[u][i];
if (!route[u] && !route[v]) {
update(v, d + 1);
swap(v, adj[u].back()), adj[u].pop_back(), -- i;
continue;
}
if (!route[u] && route[v]) {
update(v, d + 1);
// update(v, first time watchmen passed)
update(v, get_round(d, ord[v], len[route[v]]) + 1);
swap(v, adj[u].back()), adj[u].pop_back(), -- i;
continue;
}
if (route[u] && !route[v]) {
update(v, d + 1);
swap(v, adj[u].back()), adj[u].pop_back(), -- i;
continue;
}
if (route[u] == route[v]) {
if (ord[u] != (ord[v] + 1) % len[route[u]] || t != ord[v])
update(v, d + 1);
}
if (route[u] != route[v])
update(v, d + 1);
if (route[u] != route[v] || (ord[u] != (ord[v] + 1) % len[route[u]] && ord[v] != (ord[u] + 1) % len[route[u]])) {
int F = get_round(d, ord[v], len[route[v]]); // first time meet watchmen in route[v]
int G = get_round(F, ord[u], len[route[u]]); // first time meet watchmen in route[u]
if (F != G) { // can go back and forth between u, v
// update(v, first time watchmen passed)
update(v, F + 1);
swap(v, adj[u].back()), adj[u].pop_back(), -- i;
continue;
} else {
d = get_round(F, t, len[route[u]]); // go around until watchmen pass v
update(v, d + 1);
if (len[route[v]] % len[route[u]] != 0 && (len[route[u]] % len[route[v]] != 0 || (ord[u] - t + len[route[u]]) % len[route[u]] >= len[route[v]]))
update(v, get_round(d, ord[v], len[route[v]]) + 1);
}
}
}
}
cout << "impossible\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
13524 KB |
Output is correct |
2 |
Correct |
78 ms |
20012 KB |
Output is correct |
3 |
Correct |
82 ms |
19632 KB |
Output is correct |
4 |
Correct |
78 ms |
19660 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
75 ms |
19484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
13496 KB |
Output is correct |
2 |
Correct |
77 ms |
20080 KB |
Output is correct |
3 |
Correct |
74 ms |
19644 KB |
Output is correct |
4 |
Correct |
76 ms |
19660 KB |
Output is correct |
5 |
Correct |
7 ms |
12068 KB |
Output is correct |
6 |
Correct |
71 ms |
19576 KB |
Output is correct |
7 |
Correct |
84 ms |
19532 KB |
Output is correct |
8 |
Correct |
63 ms |
19488 KB |
Output is correct |
9 |
Correct |
66 ms |
19532 KB |
Output is correct |
10 |
Correct |
73 ms |
19764 KB |
Output is correct |
11 |
Correct |
67 ms |
19616 KB |
Output is correct |
12 |
Correct |
84 ms |
19532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
13496 KB |
Output is correct |
2 |
Correct |
77 ms |
20080 KB |
Output is correct |
3 |
Correct |
74 ms |
19644 KB |
Output is correct |
4 |
Correct |
76 ms |
19660 KB |
Output is correct |
5 |
Correct |
7 ms |
12068 KB |
Output is correct |
6 |
Correct |
71 ms |
19576 KB |
Output is correct |
7 |
Correct |
84 ms |
19532 KB |
Output is correct |
8 |
Correct |
63 ms |
19488 KB |
Output is correct |
9 |
Correct |
66 ms |
19532 KB |
Output is correct |
10 |
Correct |
73 ms |
19764 KB |
Output is correct |
11 |
Correct |
67 ms |
19616 KB |
Output is correct |
12 |
Correct |
84 ms |
19532 KB |
Output is correct |
13 |
Correct |
18 ms |
13492 KB |
Output is correct |
14 |
Correct |
85 ms |
19968 KB |
Output is correct |
15 |
Correct |
87 ms |
19652 KB |
Output is correct |
16 |
Correct |
77 ms |
19696 KB |
Output is correct |
17 |
Correct |
8 ms |
12116 KB |
Output is correct |
18 |
Correct |
86 ms |
19580 KB |
Output is correct |
19 |
Correct |
68 ms |
19484 KB |
Output is correct |
20 |
Correct |
67 ms |
19576 KB |
Output is correct |
21 |
Correct |
70 ms |
19460 KB |
Output is correct |
22 |
Correct |
84 ms |
19764 KB |
Output is correct |
23 |
Correct |
85 ms |
19532 KB |
Output is correct |
24 |
Correct |
77 ms |
19492 KB |
Output is correct |
25 |
Correct |
1683 ms |
75768 KB |
Output is correct |
26 |
Correct |
1540 ms |
80444 KB |
Output is correct |
27 |
Correct |
1497 ms |
76088 KB |
Output is correct |
28 |
Correct |
1088 ms |
80252 KB |
Output is correct |
29 |
Correct |
1432 ms |
71448 KB |
Output is correct |
30 |
Correct |
1391 ms |
73988 KB |
Output is correct |
31 |
Correct |
1714 ms |
80476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
13496 KB |
Output is correct |
2 |
Correct |
77 ms |
20080 KB |
Output is correct |
3 |
Correct |
74 ms |
19644 KB |
Output is correct |
4 |
Correct |
76 ms |
19660 KB |
Output is correct |
5 |
Correct |
7 ms |
12068 KB |
Output is correct |
6 |
Correct |
71 ms |
19576 KB |
Output is correct |
7 |
Correct |
84 ms |
19532 KB |
Output is correct |
8 |
Correct |
63 ms |
19488 KB |
Output is correct |
9 |
Correct |
66 ms |
19532 KB |
Output is correct |
10 |
Correct |
73 ms |
19764 KB |
Output is correct |
11 |
Correct |
67 ms |
19616 KB |
Output is correct |
12 |
Correct |
84 ms |
19532 KB |
Output is correct |
13 |
Correct |
18 ms |
13492 KB |
Output is correct |
14 |
Correct |
85 ms |
19968 KB |
Output is correct |
15 |
Correct |
87 ms |
19652 KB |
Output is correct |
16 |
Correct |
77 ms |
19696 KB |
Output is correct |
17 |
Correct |
8 ms |
12116 KB |
Output is correct |
18 |
Correct |
86 ms |
19580 KB |
Output is correct |
19 |
Correct |
68 ms |
19484 KB |
Output is correct |
20 |
Correct |
67 ms |
19576 KB |
Output is correct |
21 |
Correct |
70 ms |
19460 KB |
Output is correct |
22 |
Correct |
84 ms |
19764 KB |
Output is correct |
23 |
Correct |
85 ms |
19532 KB |
Output is correct |
24 |
Correct |
77 ms |
19492 KB |
Output is correct |
25 |
Correct |
1683 ms |
75768 KB |
Output is correct |
26 |
Correct |
1540 ms |
80444 KB |
Output is correct |
27 |
Correct |
1497 ms |
76088 KB |
Output is correct |
28 |
Correct |
1088 ms |
80252 KB |
Output is correct |
29 |
Correct |
1432 ms |
71448 KB |
Output is correct |
30 |
Correct |
1391 ms |
73988 KB |
Output is correct |
31 |
Correct |
1714 ms |
80476 KB |
Output is correct |
32 |
Correct |
18 ms |
13524 KB |
Output is correct |
33 |
Correct |
74 ms |
19972 KB |
Output is correct |
34 |
Correct |
81 ms |
19708 KB |
Output is correct |
35 |
Correct |
86 ms |
19692 KB |
Output is correct |
36 |
Correct |
8 ms |
12096 KB |
Output is correct |
37 |
Correct |
77 ms |
19536 KB |
Output is correct |
38 |
Correct |
71 ms |
19516 KB |
Output is correct |
39 |
Correct |
65 ms |
19516 KB |
Output is correct |
40 |
Correct |
74 ms |
19480 KB |
Output is correct |
41 |
Correct |
80 ms |
19752 KB |
Output is correct |
42 |
Correct |
89 ms |
19604 KB |
Output is correct |
43 |
Correct |
82 ms |
19504 KB |
Output is correct |
44 |
Correct |
1631 ms |
75524 KB |
Output is correct |
45 |
Correct |
1538 ms |
79716 KB |
Output is correct |
46 |
Correct |
1620 ms |
75136 KB |
Output is correct |
47 |
Correct |
1211 ms |
78536 KB |
Output is correct |
48 |
Correct |
1451 ms |
69096 KB |
Output is correct |
49 |
Correct |
1393 ms |
71008 KB |
Output is correct |
50 |
Correct |
1767 ms |
77352 KB |
Output is correct |
51 |
Correct |
2232 ms |
80944 KB |
Output is correct |
52 |
Correct |
2502 ms |
91388 KB |
Output is correct |
53 |
Correct |
2236 ms |
79884 KB |
Output is correct |
54 |
Correct |
1084 ms |
65400 KB |
Output is correct |
55 |
Correct |
2190 ms |
78584 KB |
Output is correct |
56 |
Correct |
1991 ms |
70368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
13524 KB |
Output is correct |
2 |
Correct |
78 ms |
20012 KB |
Output is correct |
3 |
Correct |
82 ms |
19632 KB |
Output is correct |
4 |
Correct |
78 ms |
19660 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
75 ms |
19484 KB |
Output is correct |
7 |
Correct |
17 ms |
13496 KB |
Output is correct |
8 |
Correct |
77 ms |
20080 KB |
Output is correct |
9 |
Correct |
74 ms |
19644 KB |
Output is correct |
10 |
Correct |
76 ms |
19660 KB |
Output is correct |
11 |
Correct |
7 ms |
12068 KB |
Output is correct |
12 |
Correct |
71 ms |
19576 KB |
Output is correct |
13 |
Correct |
84 ms |
19532 KB |
Output is correct |
14 |
Correct |
63 ms |
19488 KB |
Output is correct |
15 |
Correct |
66 ms |
19532 KB |
Output is correct |
16 |
Correct |
73 ms |
19764 KB |
Output is correct |
17 |
Correct |
67 ms |
19616 KB |
Output is correct |
18 |
Correct |
84 ms |
19532 KB |
Output is correct |
19 |
Correct |
7 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
11988 KB |
Output is correct |
21 |
Correct |
6 ms |
11988 KB |
Output is correct |
22 |
Correct |
18 ms |
13560 KB |
Output is correct |
23 |
Correct |
80 ms |
20060 KB |
Output is correct |
24 |
Correct |
89 ms |
19624 KB |
Output is correct |
25 |
Correct |
91 ms |
19724 KB |
Output is correct |
26 |
Correct |
8 ms |
12116 KB |
Output is correct |
27 |
Correct |
76 ms |
19588 KB |
Output is correct |
28 |
Correct |
67 ms |
19532 KB |
Output is correct |
29 |
Correct |
75 ms |
19664 KB |
Output is correct |
30 |
Correct |
72 ms |
19476 KB |
Output is correct |
31 |
Correct |
75 ms |
19784 KB |
Output is correct |
32 |
Correct |
70 ms |
19536 KB |
Output is correct |
33 |
Correct |
73 ms |
19552 KB |
Output is correct |
34 |
Incorrect |
1656 ms |
67292 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
13524 KB |
Output is correct |
2 |
Correct |
78 ms |
20012 KB |
Output is correct |
3 |
Correct |
82 ms |
19632 KB |
Output is correct |
4 |
Correct |
78 ms |
19660 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
75 ms |
19484 KB |
Output is correct |
7 |
Correct |
17 ms |
13496 KB |
Output is correct |
8 |
Correct |
77 ms |
20080 KB |
Output is correct |
9 |
Correct |
74 ms |
19644 KB |
Output is correct |
10 |
Correct |
76 ms |
19660 KB |
Output is correct |
11 |
Correct |
7 ms |
12068 KB |
Output is correct |
12 |
Correct |
71 ms |
19576 KB |
Output is correct |
13 |
Correct |
84 ms |
19532 KB |
Output is correct |
14 |
Correct |
63 ms |
19488 KB |
Output is correct |
15 |
Correct |
66 ms |
19532 KB |
Output is correct |
16 |
Correct |
73 ms |
19764 KB |
Output is correct |
17 |
Correct |
67 ms |
19616 KB |
Output is correct |
18 |
Correct |
84 ms |
19532 KB |
Output is correct |
19 |
Correct |
18 ms |
13492 KB |
Output is correct |
20 |
Correct |
85 ms |
19968 KB |
Output is correct |
21 |
Correct |
87 ms |
19652 KB |
Output is correct |
22 |
Correct |
77 ms |
19696 KB |
Output is correct |
23 |
Correct |
8 ms |
12116 KB |
Output is correct |
24 |
Correct |
86 ms |
19580 KB |
Output is correct |
25 |
Correct |
68 ms |
19484 KB |
Output is correct |
26 |
Correct |
67 ms |
19576 KB |
Output is correct |
27 |
Correct |
70 ms |
19460 KB |
Output is correct |
28 |
Correct |
84 ms |
19764 KB |
Output is correct |
29 |
Correct |
85 ms |
19532 KB |
Output is correct |
30 |
Correct |
77 ms |
19492 KB |
Output is correct |
31 |
Correct |
1683 ms |
75768 KB |
Output is correct |
32 |
Correct |
1540 ms |
80444 KB |
Output is correct |
33 |
Correct |
1497 ms |
76088 KB |
Output is correct |
34 |
Correct |
1088 ms |
80252 KB |
Output is correct |
35 |
Correct |
1432 ms |
71448 KB |
Output is correct |
36 |
Correct |
1391 ms |
73988 KB |
Output is correct |
37 |
Correct |
1714 ms |
80476 KB |
Output is correct |
38 |
Correct |
7 ms |
11988 KB |
Output is correct |
39 |
Correct |
6 ms |
11988 KB |
Output is correct |
40 |
Correct |
6 ms |
11988 KB |
Output is correct |
41 |
Correct |
18 ms |
13560 KB |
Output is correct |
42 |
Correct |
80 ms |
20060 KB |
Output is correct |
43 |
Correct |
89 ms |
19624 KB |
Output is correct |
44 |
Correct |
91 ms |
19724 KB |
Output is correct |
45 |
Correct |
8 ms |
12116 KB |
Output is correct |
46 |
Correct |
76 ms |
19588 KB |
Output is correct |
47 |
Correct |
67 ms |
19532 KB |
Output is correct |
48 |
Correct |
75 ms |
19664 KB |
Output is correct |
49 |
Correct |
72 ms |
19476 KB |
Output is correct |
50 |
Correct |
75 ms |
19784 KB |
Output is correct |
51 |
Correct |
70 ms |
19536 KB |
Output is correct |
52 |
Correct |
73 ms |
19552 KB |
Output is correct |
53 |
Incorrect |
1656 ms |
67292 KB |
Output isn't correct |
54 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
13524 KB |
Output is correct |
2 |
Correct |
78 ms |
20012 KB |
Output is correct |
3 |
Correct |
82 ms |
19632 KB |
Output is correct |
4 |
Correct |
78 ms |
19660 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
75 ms |
19484 KB |
Output is correct |
7 |
Correct |
17 ms |
13496 KB |
Output is correct |
8 |
Correct |
77 ms |
20080 KB |
Output is correct |
9 |
Correct |
74 ms |
19644 KB |
Output is correct |
10 |
Correct |
76 ms |
19660 KB |
Output is correct |
11 |
Correct |
7 ms |
12068 KB |
Output is correct |
12 |
Correct |
71 ms |
19576 KB |
Output is correct |
13 |
Correct |
84 ms |
19532 KB |
Output is correct |
14 |
Correct |
63 ms |
19488 KB |
Output is correct |
15 |
Correct |
66 ms |
19532 KB |
Output is correct |
16 |
Correct |
73 ms |
19764 KB |
Output is correct |
17 |
Correct |
67 ms |
19616 KB |
Output is correct |
18 |
Correct |
84 ms |
19532 KB |
Output is correct |
19 |
Correct |
18 ms |
13492 KB |
Output is correct |
20 |
Correct |
85 ms |
19968 KB |
Output is correct |
21 |
Correct |
87 ms |
19652 KB |
Output is correct |
22 |
Correct |
77 ms |
19696 KB |
Output is correct |
23 |
Correct |
8 ms |
12116 KB |
Output is correct |
24 |
Correct |
86 ms |
19580 KB |
Output is correct |
25 |
Correct |
68 ms |
19484 KB |
Output is correct |
26 |
Correct |
67 ms |
19576 KB |
Output is correct |
27 |
Correct |
70 ms |
19460 KB |
Output is correct |
28 |
Correct |
84 ms |
19764 KB |
Output is correct |
29 |
Correct |
85 ms |
19532 KB |
Output is correct |
30 |
Correct |
77 ms |
19492 KB |
Output is correct |
31 |
Correct |
1683 ms |
75768 KB |
Output is correct |
32 |
Correct |
1540 ms |
80444 KB |
Output is correct |
33 |
Correct |
1497 ms |
76088 KB |
Output is correct |
34 |
Correct |
1088 ms |
80252 KB |
Output is correct |
35 |
Correct |
1432 ms |
71448 KB |
Output is correct |
36 |
Correct |
1391 ms |
73988 KB |
Output is correct |
37 |
Correct |
1714 ms |
80476 KB |
Output is correct |
38 |
Correct |
18 ms |
13524 KB |
Output is correct |
39 |
Correct |
74 ms |
19972 KB |
Output is correct |
40 |
Correct |
81 ms |
19708 KB |
Output is correct |
41 |
Correct |
86 ms |
19692 KB |
Output is correct |
42 |
Correct |
8 ms |
12096 KB |
Output is correct |
43 |
Correct |
77 ms |
19536 KB |
Output is correct |
44 |
Correct |
71 ms |
19516 KB |
Output is correct |
45 |
Correct |
65 ms |
19516 KB |
Output is correct |
46 |
Correct |
74 ms |
19480 KB |
Output is correct |
47 |
Correct |
80 ms |
19752 KB |
Output is correct |
48 |
Correct |
89 ms |
19604 KB |
Output is correct |
49 |
Correct |
82 ms |
19504 KB |
Output is correct |
50 |
Correct |
1631 ms |
75524 KB |
Output is correct |
51 |
Correct |
1538 ms |
79716 KB |
Output is correct |
52 |
Correct |
1620 ms |
75136 KB |
Output is correct |
53 |
Correct |
1211 ms |
78536 KB |
Output is correct |
54 |
Correct |
1451 ms |
69096 KB |
Output is correct |
55 |
Correct |
1393 ms |
71008 KB |
Output is correct |
56 |
Correct |
1767 ms |
77352 KB |
Output is correct |
57 |
Correct |
2232 ms |
80944 KB |
Output is correct |
58 |
Correct |
2502 ms |
91388 KB |
Output is correct |
59 |
Correct |
2236 ms |
79884 KB |
Output is correct |
60 |
Correct |
1084 ms |
65400 KB |
Output is correct |
61 |
Correct |
2190 ms |
78584 KB |
Output is correct |
62 |
Correct |
1991 ms |
70368 KB |
Output is correct |
63 |
Correct |
7 ms |
11988 KB |
Output is correct |
64 |
Correct |
6 ms |
11988 KB |
Output is correct |
65 |
Correct |
6 ms |
11988 KB |
Output is correct |
66 |
Correct |
18 ms |
13560 KB |
Output is correct |
67 |
Correct |
80 ms |
20060 KB |
Output is correct |
68 |
Correct |
89 ms |
19624 KB |
Output is correct |
69 |
Correct |
91 ms |
19724 KB |
Output is correct |
70 |
Correct |
8 ms |
12116 KB |
Output is correct |
71 |
Correct |
76 ms |
19588 KB |
Output is correct |
72 |
Correct |
67 ms |
19532 KB |
Output is correct |
73 |
Correct |
75 ms |
19664 KB |
Output is correct |
74 |
Correct |
72 ms |
19476 KB |
Output is correct |
75 |
Correct |
75 ms |
19784 KB |
Output is correct |
76 |
Correct |
70 ms |
19536 KB |
Output is correct |
77 |
Correct |
73 ms |
19552 KB |
Output is correct |
78 |
Incorrect |
1656 ms |
67292 KB |
Output isn't correct |
79 |
Halted |
0 ms |
0 KB |
- |