#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 = F + (t - ord[u] + len[route[u]]) % len[route[u]];
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 |
17 ms |
13516 KB |
Output is correct |
2 |
Correct |
74 ms |
19996 KB |
Output is correct |
3 |
Correct |
76 ms |
19652 KB |
Output is correct |
4 |
Correct |
74 ms |
19632 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
77 ms |
19604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
13524 KB |
Output is correct |
2 |
Correct |
98 ms |
20080 KB |
Output is correct |
3 |
Correct |
90 ms |
19608 KB |
Output is correct |
4 |
Correct |
75 ms |
19684 KB |
Output is correct |
5 |
Correct |
8 ms |
12116 KB |
Output is correct |
6 |
Correct |
77 ms |
19576 KB |
Output is correct |
7 |
Correct |
88 ms |
19584 KB |
Output is correct |
8 |
Correct |
100 ms |
19568 KB |
Output is correct |
9 |
Correct |
81 ms |
19500 KB |
Output is correct |
10 |
Correct |
108 ms |
19684 KB |
Output is correct |
11 |
Correct |
90 ms |
19608 KB |
Output is correct |
12 |
Correct |
86 ms |
19560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
13524 KB |
Output is correct |
2 |
Correct |
98 ms |
20080 KB |
Output is correct |
3 |
Correct |
90 ms |
19608 KB |
Output is correct |
4 |
Correct |
75 ms |
19684 KB |
Output is correct |
5 |
Correct |
8 ms |
12116 KB |
Output is correct |
6 |
Correct |
77 ms |
19576 KB |
Output is correct |
7 |
Correct |
88 ms |
19584 KB |
Output is correct |
8 |
Correct |
100 ms |
19568 KB |
Output is correct |
9 |
Correct |
81 ms |
19500 KB |
Output is correct |
10 |
Correct |
108 ms |
19684 KB |
Output is correct |
11 |
Correct |
90 ms |
19608 KB |
Output is correct |
12 |
Correct |
86 ms |
19560 KB |
Output is correct |
13 |
Correct |
17 ms |
13588 KB |
Output is correct |
14 |
Correct |
71 ms |
20092 KB |
Output is correct |
15 |
Correct |
84 ms |
19660 KB |
Output is correct |
16 |
Correct |
94 ms |
19676 KB |
Output is correct |
17 |
Correct |
7 ms |
12116 KB |
Output is correct |
18 |
Correct |
72 ms |
19532 KB |
Output is correct |
19 |
Correct |
65 ms |
19496 KB |
Output is correct |
20 |
Correct |
82 ms |
19552 KB |
Output is correct |
21 |
Correct |
72 ms |
19476 KB |
Output is correct |
22 |
Correct |
79 ms |
19748 KB |
Output is correct |
23 |
Correct |
121 ms |
19508 KB |
Output is correct |
24 |
Correct |
86 ms |
19544 KB |
Output is correct |
25 |
Correct |
1676 ms |
99540 KB |
Output is correct |
26 |
Correct |
1531 ms |
104148 KB |
Output is correct |
27 |
Correct |
1444 ms |
99956 KB |
Output is correct |
28 |
Correct |
1159 ms |
103912 KB |
Output is correct |
29 |
Correct |
1592 ms |
95328 KB |
Output is correct |
30 |
Correct |
1506 ms |
97720 KB |
Output is correct |
31 |
Correct |
1778 ms |
103940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
13524 KB |
Output is correct |
2 |
Correct |
98 ms |
20080 KB |
Output is correct |
3 |
Correct |
90 ms |
19608 KB |
Output is correct |
4 |
Correct |
75 ms |
19684 KB |
Output is correct |
5 |
Correct |
8 ms |
12116 KB |
Output is correct |
6 |
Correct |
77 ms |
19576 KB |
Output is correct |
7 |
Correct |
88 ms |
19584 KB |
Output is correct |
8 |
Correct |
100 ms |
19568 KB |
Output is correct |
9 |
Correct |
81 ms |
19500 KB |
Output is correct |
10 |
Correct |
108 ms |
19684 KB |
Output is correct |
11 |
Correct |
90 ms |
19608 KB |
Output is correct |
12 |
Correct |
86 ms |
19560 KB |
Output is correct |
13 |
Correct |
17 ms |
13588 KB |
Output is correct |
14 |
Correct |
71 ms |
20092 KB |
Output is correct |
15 |
Correct |
84 ms |
19660 KB |
Output is correct |
16 |
Correct |
94 ms |
19676 KB |
Output is correct |
17 |
Correct |
7 ms |
12116 KB |
Output is correct |
18 |
Correct |
72 ms |
19532 KB |
Output is correct |
19 |
Correct |
65 ms |
19496 KB |
Output is correct |
20 |
Correct |
82 ms |
19552 KB |
Output is correct |
21 |
Correct |
72 ms |
19476 KB |
Output is correct |
22 |
Correct |
79 ms |
19748 KB |
Output is correct |
23 |
Correct |
121 ms |
19508 KB |
Output is correct |
24 |
Correct |
86 ms |
19544 KB |
Output is correct |
25 |
Correct |
1676 ms |
99540 KB |
Output is correct |
26 |
Correct |
1531 ms |
104148 KB |
Output is correct |
27 |
Correct |
1444 ms |
99956 KB |
Output is correct |
28 |
Correct |
1159 ms |
103912 KB |
Output is correct |
29 |
Correct |
1592 ms |
95328 KB |
Output is correct |
30 |
Correct |
1506 ms |
97720 KB |
Output is correct |
31 |
Correct |
1778 ms |
103940 KB |
Output is correct |
32 |
Correct |
20 ms |
13492 KB |
Output is correct |
33 |
Correct |
85 ms |
20044 KB |
Output is correct |
34 |
Correct |
86 ms |
19652 KB |
Output is correct |
35 |
Correct |
76 ms |
19644 KB |
Output is correct |
36 |
Correct |
7 ms |
12116 KB |
Output is correct |
37 |
Correct |
70 ms |
19580 KB |
Output is correct |
38 |
Correct |
67 ms |
19564 KB |
Output is correct |
39 |
Correct |
66 ms |
19572 KB |
Output is correct |
40 |
Correct |
74 ms |
19532 KB |
Output is correct |
41 |
Correct |
68 ms |
19724 KB |
Output is correct |
42 |
Correct |
69 ms |
19540 KB |
Output is correct |
43 |
Correct |
72 ms |
19536 KB |
Output is correct |
44 |
Correct |
1721 ms |
98712 KB |
Output is correct |
45 |
Correct |
1652 ms |
103528 KB |
Output is correct |
46 |
Correct |
1511 ms |
99232 KB |
Output is correct |
47 |
Correct |
1177 ms |
102984 KB |
Output is correct |
48 |
Correct |
1490 ms |
94536 KB |
Output is correct |
49 |
Correct |
1479 ms |
96476 KB |
Output is correct |
50 |
Correct |
1858 ms |
102916 KB |
Output is correct |
51 |
Correct |
2255 ms |
107932 KB |
Output is correct |
52 |
Correct |
2509 ms |
117812 KB |
Output is correct |
53 |
Correct |
2033 ms |
108284 KB |
Output is correct |
54 |
Correct |
1115 ms |
94668 KB |
Output is correct |
55 |
Correct |
2152 ms |
107904 KB |
Output is correct |
56 |
Correct |
1984 ms |
100912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
13516 KB |
Output is correct |
2 |
Correct |
74 ms |
19996 KB |
Output is correct |
3 |
Correct |
76 ms |
19652 KB |
Output is correct |
4 |
Correct |
74 ms |
19632 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
77 ms |
19604 KB |
Output is correct |
7 |
Correct |
18 ms |
13524 KB |
Output is correct |
8 |
Correct |
98 ms |
20080 KB |
Output is correct |
9 |
Correct |
90 ms |
19608 KB |
Output is correct |
10 |
Correct |
75 ms |
19684 KB |
Output is correct |
11 |
Correct |
8 ms |
12116 KB |
Output is correct |
12 |
Correct |
77 ms |
19576 KB |
Output is correct |
13 |
Correct |
88 ms |
19584 KB |
Output is correct |
14 |
Correct |
100 ms |
19568 KB |
Output is correct |
15 |
Correct |
81 ms |
19500 KB |
Output is correct |
16 |
Correct |
108 ms |
19684 KB |
Output is correct |
17 |
Correct |
90 ms |
19608 KB |
Output is correct |
18 |
Correct |
86 ms |
19560 KB |
Output is correct |
19 |
Correct |
7 ms |
11988 KB |
Output is correct |
20 |
Correct |
8 ms |
11988 KB |
Output is correct |
21 |
Correct |
7 ms |
12044 KB |
Output is correct |
22 |
Correct |
18 ms |
13556 KB |
Output is correct |
23 |
Correct |
94 ms |
20056 KB |
Output is correct |
24 |
Correct |
74 ms |
19600 KB |
Output is correct |
25 |
Correct |
80 ms |
19720 KB |
Output is correct |
26 |
Correct |
10 ms |
12116 KB |
Output is correct |
27 |
Correct |
78 ms |
19568 KB |
Output is correct |
28 |
Correct |
69 ms |
19488 KB |
Output is correct |
29 |
Correct |
77 ms |
19468 KB |
Output is correct |
30 |
Correct |
73 ms |
19532 KB |
Output is correct |
31 |
Correct |
90 ms |
19748 KB |
Output is correct |
32 |
Correct |
68 ms |
19528 KB |
Output is correct |
33 |
Correct |
72 ms |
19568 KB |
Output is correct |
34 |
Incorrect |
1635 ms |
99140 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
13516 KB |
Output is correct |
2 |
Correct |
74 ms |
19996 KB |
Output is correct |
3 |
Correct |
76 ms |
19652 KB |
Output is correct |
4 |
Correct |
74 ms |
19632 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
77 ms |
19604 KB |
Output is correct |
7 |
Correct |
18 ms |
13524 KB |
Output is correct |
8 |
Correct |
98 ms |
20080 KB |
Output is correct |
9 |
Correct |
90 ms |
19608 KB |
Output is correct |
10 |
Correct |
75 ms |
19684 KB |
Output is correct |
11 |
Correct |
8 ms |
12116 KB |
Output is correct |
12 |
Correct |
77 ms |
19576 KB |
Output is correct |
13 |
Correct |
88 ms |
19584 KB |
Output is correct |
14 |
Correct |
100 ms |
19568 KB |
Output is correct |
15 |
Correct |
81 ms |
19500 KB |
Output is correct |
16 |
Correct |
108 ms |
19684 KB |
Output is correct |
17 |
Correct |
90 ms |
19608 KB |
Output is correct |
18 |
Correct |
86 ms |
19560 KB |
Output is correct |
19 |
Correct |
17 ms |
13588 KB |
Output is correct |
20 |
Correct |
71 ms |
20092 KB |
Output is correct |
21 |
Correct |
84 ms |
19660 KB |
Output is correct |
22 |
Correct |
94 ms |
19676 KB |
Output is correct |
23 |
Correct |
7 ms |
12116 KB |
Output is correct |
24 |
Correct |
72 ms |
19532 KB |
Output is correct |
25 |
Correct |
65 ms |
19496 KB |
Output is correct |
26 |
Correct |
82 ms |
19552 KB |
Output is correct |
27 |
Correct |
72 ms |
19476 KB |
Output is correct |
28 |
Correct |
79 ms |
19748 KB |
Output is correct |
29 |
Correct |
121 ms |
19508 KB |
Output is correct |
30 |
Correct |
86 ms |
19544 KB |
Output is correct |
31 |
Correct |
1676 ms |
99540 KB |
Output is correct |
32 |
Correct |
1531 ms |
104148 KB |
Output is correct |
33 |
Correct |
1444 ms |
99956 KB |
Output is correct |
34 |
Correct |
1159 ms |
103912 KB |
Output is correct |
35 |
Correct |
1592 ms |
95328 KB |
Output is correct |
36 |
Correct |
1506 ms |
97720 KB |
Output is correct |
37 |
Correct |
1778 ms |
103940 KB |
Output is correct |
38 |
Correct |
7 ms |
11988 KB |
Output is correct |
39 |
Correct |
8 ms |
11988 KB |
Output is correct |
40 |
Correct |
7 ms |
12044 KB |
Output is correct |
41 |
Correct |
18 ms |
13556 KB |
Output is correct |
42 |
Correct |
94 ms |
20056 KB |
Output is correct |
43 |
Correct |
74 ms |
19600 KB |
Output is correct |
44 |
Correct |
80 ms |
19720 KB |
Output is correct |
45 |
Correct |
10 ms |
12116 KB |
Output is correct |
46 |
Correct |
78 ms |
19568 KB |
Output is correct |
47 |
Correct |
69 ms |
19488 KB |
Output is correct |
48 |
Correct |
77 ms |
19468 KB |
Output is correct |
49 |
Correct |
73 ms |
19532 KB |
Output is correct |
50 |
Correct |
90 ms |
19748 KB |
Output is correct |
51 |
Correct |
68 ms |
19528 KB |
Output is correct |
52 |
Correct |
72 ms |
19568 KB |
Output is correct |
53 |
Incorrect |
1635 ms |
99140 KB |
Output isn't correct |
54 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
13516 KB |
Output is correct |
2 |
Correct |
74 ms |
19996 KB |
Output is correct |
3 |
Correct |
76 ms |
19652 KB |
Output is correct |
4 |
Correct |
74 ms |
19632 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
77 ms |
19604 KB |
Output is correct |
7 |
Correct |
18 ms |
13524 KB |
Output is correct |
8 |
Correct |
98 ms |
20080 KB |
Output is correct |
9 |
Correct |
90 ms |
19608 KB |
Output is correct |
10 |
Correct |
75 ms |
19684 KB |
Output is correct |
11 |
Correct |
8 ms |
12116 KB |
Output is correct |
12 |
Correct |
77 ms |
19576 KB |
Output is correct |
13 |
Correct |
88 ms |
19584 KB |
Output is correct |
14 |
Correct |
100 ms |
19568 KB |
Output is correct |
15 |
Correct |
81 ms |
19500 KB |
Output is correct |
16 |
Correct |
108 ms |
19684 KB |
Output is correct |
17 |
Correct |
90 ms |
19608 KB |
Output is correct |
18 |
Correct |
86 ms |
19560 KB |
Output is correct |
19 |
Correct |
17 ms |
13588 KB |
Output is correct |
20 |
Correct |
71 ms |
20092 KB |
Output is correct |
21 |
Correct |
84 ms |
19660 KB |
Output is correct |
22 |
Correct |
94 ms |
19676 KB |
Output is correct |
23 |
Correct |
7 ms |
12116 KB |
Output is correct |
24 |
Correct |
72 ms |
19532 KB |
Output is correct |
25 |
Correct |
65 ms |
19496 KB |
Output is correct |
26 |
Correct |
82 ms |
19552 KB |
Output is correct |
27 |
Correct |
72 ms |
19476 KB |
Output is correct |
28 |
Correct |
79 ms |
19748 KB |
Output is correct |
29 |
Correct |
121 ms |
19508 KB |
Output is correct |
30 |
Correct |
86 ms |
19544 KB |
Output is correct |
31 |
Correct |
1676 ms |
99540 KB |
Output is correct |
32 |
Correct |
1531 ms |
104148 KB |
Output is correct |
33 |
Correct |
1444 ms |
99956 KB |
Output is correct |
34 |
Correct |
1159 ms |
103912 KB |
Output is correct |
35 |
Correct |
1592 ms |
95328 KB |
Output is correct |
36 |
Correct |
1506 ms |
97720 KB |
Output is correct |
37 |
Correct |
1778 ms |
103940 KB |
Output is correct |
38 |
Correct |
20 ms |
13492 KB |
Output is correct |
39 |
Correct |
85 ms |
20044 KB |
Output is correct |
40 |
Correct |
86 ms |
19652 KB |
Output is correct |
41 |
Correct |
76 ms |
19644 KB |
Output is correct |
42 |
Correct |
7 ms |
12116 KB |
Output is correct |
43 |
Correct |
70 ms |
19580 KB |
Output is correct |
44 |
Correct |
67 ms |
19564 KB |
Output is correct |
45 |
Correct |
66 ms |
19572 KB |
Output is correct |
46 |
Correct |
74 ms |
19532 KB |
Output is correct |
47 |
Correct |
68 ms |
19724 KB |
Output is correct |
48 |
Correct |
69 ms |
19540 KB |
Output is correct |
49 |
Correct |
72 ms |
19536 KB |
Output is correct |
50 |
Correct |
1721 ms |
98712 KB |
Output is correct |
51 |
Correct |
1652 ms |
103528 KB |
Output is correct |
52 |
Correct |
1511 ms |
99232 KB |
Output is correct |
53 |
Correct |
1177 ms |
102984 KB |
Output is correct |
54 |
Correct |
1490 ms |
94536 KB |
Output is correct |
55 |
Correct |
1479 ms |
96476 KB |
Output is correct |
56 |
Correct |
1858 ms |
102916 KB |
Output is correct |
57 |
Correct |
2255 ms |
107932 KB |
Output is correct |
58 |
Correct |
2509 ms |
117812 KB |
Output is correct |
59 |
Correct |
2033 ms |
108284 KB |
Output is correct |
60 |
Correct |
1115 ms |
94668 KB |
Output is correct |
61 |
Correct |
2152 ms |
107904 KB |
Output is correct |
62 |
Correct |
1984 ms |
100912 KB |
Output is correct |
63 |
Correct |
7 ms |
11988 KB |
Output is correct |
64 |
Correct |
8 ms |
11988 KB |
Output is correct |
65 |
Correct |
7 ms |
12044 KB |
Output is correct |
66 |
Correct |
18 ms |
13556 KB |
Output is correct |
67 |
Correct |
94 ms |
20056 KB |
Output is correct |
68 |
Correct |
74 ms |
19600 KB |
Output is correct |
69 |
Correct |
80 ms |
19720 KB |
Output is correct |
70 |
Correct |
10 ms |
12116 KB |
Output is correct |
71 |
Correct |
78 ms |
19568 KB |
Output is correct |
72 |
Correct |
69 ms |
19488 KB |
Output is correct |
73 |
Correct |
77 ms |
19468 KB |
Output is correct |
74 |
Correct |
73 ms |
19532 KB |
Output is correct |
75 |
Correct |
90 ms |
19748 KB |
Output is correct |
76 |
Correct |
68 ms |
19528 KB |
Output is correct |
77 |
Correct |
72 ms |
19568 KB |
Output is correct |
78 |
Incorrect |
1635 ms |
99140 KB |
Output isn't correct |
79 |
Halted |
0 ms |
0 KB |
- |