// pana la ora 1
bool home = 0;
bool verbose = 1;
#include <iostream>
#include <cstdio>
#include <vector>
#include <cassert>
#include <queue>
using namespace std;
struct T {
int v;
int r;
};
const int N = 250000 + 7;
const int V = (int)1e7 + 7;
const int INF = (int)1e9 + 7;
int n;
int m;
int cnt_cycles;
int suml;
vector<pair<int, int>> edges;
vector<int> cycles[N];
int cycle_id[N];
int cycle_location[N];
int single_vertex_id[N];
vector<int> cycle_vertex_id[N];
int dp[V];
T what[V];
int y;
vector<int> g[N];
priority_queue<pair<int, int>> pq;
void relax(int id, int d) {
assert(1 <= id && id <= y);
if (d < dp[id]) {
dp[id] = d;
pq.push({ -dp[id], id });
}
}
int main() {
if (!home) {
verbose = 0;
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
if (home) {
verbose = 1;
// FILE* stream;
// freopen_s(&stream, "input.txt", "r", stdin);
}
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
edges.push_back({ a, b });
}
bool debug = 1;
cin >> cnt_cycles;
debug &= (cnt_cycles == 21);
for (int i = 1; i <= cnt_cycles; i++) {
int l;
cin >> l;
suml += l;
if (i == 1) {
debug &= (l == 60);
}
else {
debug &= (l == 3);
}
cycles[i].resize(l);
for (int j = 0; j < (int)cycles[i].size(); j++) {
cin >> cycles[i][j];
assert(cycle_id[cycles[i][j]] == 0);
cycle_id[cycles[i][j]] = i;
cycle_location[cycles[i][j]] = j;
}
if (verbose) {
cout << " ---> ";
for (auto& v : cycles[i]) {
cout << v << " ";
}
cout << "\n";
}
}
for (int a = 1; a <= n; a++) {
if (cycle_id[a] == 0) {
y++;
single_vertex_id[a] = y;
what[y] = { a, 0 };
}
else {
int id = cycle_id[a];
cycle_vertex_id[a].resize((int) cycles[id].size());
for (int j = 0; j < (int)cycles[id].size(); j++) {
y++;
cycle_vertex_id[a][j] = y;
what[y] = { a, j };
}
}
}
if (debug) {
assert(0);
exit(0);
}
for (int i = 1; i <= y; i++) {
dp[i] = INF;
}
if (verbose) {
cout << "################################################################################### \n";
}
assert(cycle_id[1] == 0);
assert(cycle_id[n] == 0);
dp[single_vertex_id[1]] = 0;
pq.push({ 0, single_vertex_id[1] });
while (!pq.empty()) {
int d = -pq.top().first;
int idv = pq.top().second;
pq.pop();
if (dp[idv] != d) {
continue;
}
int vertex = what[idv].v;
int r = what[idv].r;
if (cycle_id[vertex] == 0) {
assert(r == 0);
for (auto& vecin : g[vertex]) {
if (cycle_id[vecin] == 0) {
// vecin is a single vertex
relax(single_vertex_id[vecin], d + 1);
}
else {
// vecin is on a cycle
int cycle_vecin = cycle_id[vecin];
int l_vecin = cycles[cycle_vecin].size();
for (int twait = 0; twait < l_vecin; twait++) {
// la momentul d + 1 + twait
if ((d + 1 + twait) % l_vecin == cycle_location[vecin]) {
continue;
}
relax(cycle_vertex_id[vecin][(d + 1 + twait) % l_vecin], d + 1 + twait);
}
}
}
}
else {
int cycle = cycle_id[vertex];
int l = cycles[cycle].size();
/*
wait a moment here
*/
if ((d + 1) % l != cycle_location[vertex]) {
relax(cycle_vertex_id[vertex][(d + 1) % l], d + 1);
}
/*
don't wait a moment here
*/
assert(0 <= r && r < l);
assert(d % l == r);
for (auto& vecin : g[vertex]) {
if (cycle_id[vecin] == 0) {
// vecin is a single vertex
relax(single_vertex_id[vecin], d + 1);
}
else {
// vecin is on a cycle
int cycle_vecin = cycle_id[vecin];
int l_vecin = cycles[cycle_vecin].size();
if (cycle_vecin == cycle) {
// vecin is on the same cycle
if ((d + 1) % l_vecin == cycle_location[vecin]) {
continue;
}
if (d % l_vecin == cycle_location[vecin] && (d + 1) % l_vecin == cycle_location[vertex]) {
continue;
}
relax(cycle_vertex_id[vecin][(d + 1) % l_vecin], d + 1);
/*
vertex -> vecin <- vecin2
*/
}
else {
// vecin is on a different cycle
if ((d + 1) % l_vecin == cycle_location[vecin]) {
continue;
}
relax(cycle_vertex_id[vecin][(d + 1) % l_vecin], d + 1);
}
}
}
}
}
int sol = dp[single_vertex_id[n]];
if (sol == INF) {
cout << "impossible\n";
return 0;
}
cout << sol << "\n";
return 0;
/*for (auto& edge : edges) {
int a = edge.first, b = edge.second;
if (cycle_id[a] == 0 && cycle_id[b] == 0) {
// just a normal edge
addEdge(single_vertex_id[a], single_vertex_id[b], 1);
continue;
}
if (cycle_id[a] != 0 && cycle_id[b] == 0) {
swap(a, b);
}
if (cycle_id[a] == 0 && cycle_id[b] != 0) {
continue;
}
assert(cycle_id[a] != 0 && cycle_id[b] != 0);
}*/
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
20032 KB |
Output is correct |
2 |
Correct |
59 ms |
24368 KB |
Output is correct |
3 |
Correct |
61 ms |
24108 KB |
Output is correct |
4 |
Correct |
96 ms |
24408 KB |
Output is correct |
5 |
Correct |
12 ms |
18176 KB |
Output is correct |
6 |
Correct |
62 ms |
24112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
20080 KB |
Output is correct |
2 |
Correct |
82 ms |
24280 KB |
Output is correct |
3 |
Correct |
71 ms |
24068 KB |
Output is correct |
4 |
Correct |
89 ms |
24420 KB |
Output is correct |
5 |
Correct |
12 ms |
18132 KB |
Output is correct |
6 |
Correct |
60 ms |
24080 KB |
Output is correct |
7 |
Correct |
58 ms |
23796 KB |
Output is correct |
8 |
Correct |
66 ms |
23832 KB |
Output is correct |
9 |
Correct |
55 ms |
23800 KB |
Output is correct |
10 |
Correct |
72 ms |
24044 KB |
Output is correct |
11 |
Correct |
64 ms |
23804 KB |
Output is correct |
12 |
Correct |
72 ms |
23920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
20080 KB |
Output is correct |
2 |
Correct |
82 ms |
24280 KB |
Output is correct |
3 |
Correct |
71 ms |
24068 KB |
Output is correct |
4 |
Correct |
89 ms |
24420 KB |
Output is correct |
5 |
Correct |
12 ms |
18132 KB |
Output is correct |
6 |
Correct |
60 ms |
24080 KB |
Output is correct |
7 |
Correct |
58 ms |
23796 KB |
Output is correct |
8 |
Correct |
66 ms |
23832 KB |
Output is correct |
9 |
Correct |
55 ms |
23800 KB |
Output is correct |
10 |
Correct |
72 ms |
24044 KB |
Output is correct |
11 |
Correct |
64 ms |
23804 KB |
Output is correct |
12 |
Correct |
72 ms |
23920 KB |
Output is correct |
13 |
Correct |
87 ms |
20056 KB |
Output is correct |
14 |
Correct |
63 ms |
24340 KB |
Output is correct |
15 |
Correct |
69 ms |
24152 KB |
Output is correct |
16 |
Correct |
103 ms |
24408 KB |
Output is correct |
17 |
Correct |
12 ms |
18132 KB |
Output is correct |
18 |
Correct |
63 ms |
24064 KB |
Output is correct |
19 |
Correct |
58 ms |
23828 KB |
Output is correct |
20 |
Correct |
70 ms |
23744 KB |
Output is correct |
21 |
Correct |
57 ms |
23700 KB |
Output is correct |
22 |
Correct |
71 ms |
24068 KB |
Output is correct |
23 |
Correct |
60 ms |
23744 KB |
Output is correct |
24 |
Correct |
75 ms |
23812 KB |
Output is correct |
25 |
Correct |
1384 ms |
91056 KB |
Output is correct |
26 |
Correct |
1402 ms |
97408 KB |
Output is correct |
27 |
Correct |
1290 ms |
90948 KB |
Output is correct |
28 |
Correct |
1086 ms |
94880 KB |
Output is correct |
29 |
Execution timed out |
6080 ms |
85940 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
20080 KB |
Output is correct |
2 |
Correct |
82 ms |
24280 KB |
Output is correct |
3 |
Correct |
71 ms |
24068 KB |
Output is correct |
4 |
Correct |
89 ms |
24420 KB |
Output is correct |
5 |
Correct |
12 ms |
18132 KB |
Output is correct |
6 |
Correct |
60 ms |
24080 KB |
Output is correct |
7 |
Correct |
58 ms |
23796 KB |
Output is correct |
8 |
Correct |
66 ms |
23832 KB |
Output is correct |
9 |
Correct |
55 ms |
23800 KB |
Output is correct |
10 |
Correct |
72 ms |
24044 KB |
Output is correct |
11 |
Correct |
64 ms |
23804 KB |
Output is correct |
12 |
Correct |
72 ms |
23920 KB |
Output is correct |
13 |
Correct |
87 ms |
20056 KB |
Output is correct |
14 |
Correct |
63 ms |
24340 KB |
Output is correct |
15 |
Correct |
69 ms |
24152 KB |
Output is correct |
16 |
Correct |
103 ms |
24408 KB |
Output is correct |
17 |
Correct |
12 ms |
18132 KB |
Output is correct |
18 |
Correct |
63 ms |
24064 KB |
Output is correct |
19 |
Correct |
58 ms |
23828 KB |
Output is correct |
20 |
Correct |
70 ms |
23744 KB |
Output is correct |
21 |
Correct |
57 ms |
23700 KB |
Output is correct |
22 |
Correct |
71 ms |
24068 KB |
Output is correct |
23 |
Correct |
60 ms |
23744 KB |
Output is correct |
24 |
Correct |
75 ms |
23812 KB |
Output is correct |
25 |
Correct |
1384 ms |
91056 KB |
Output is correct |
26 |
Correct |
1402 ms |
97408 KB |
Output is correct |
27 |
Correct |
1290 ms |
90948 KB |
Output is correct |
28 |
Correct |
1086 ms |
94880 KB |
Output is correct |
29 |
Execution timed out |
6080 ms |
85940 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
20032 KB |
Output is correct |
2 |
Correct |
59 ms |
24368 KB |
Output is correct |
3 |
Correct |
61 ms |
24108 KB |
Output is correct |
4 |
Correct |
96 ms |
24408 KB |
Output is correct |
5 |
Correct |
12 ms |
18176 KB |
Output is correct |
6 |
Correct |
62 ms |
24112 KB |
Output is correct |
7 |
Correct |
94 ms |
20080 KB |
Output is correct |
8 |
Correct |
82 ms |
24280 KB |
Output is correct |
9 |
Correct |
71 ms |
24068 KB |
Output is correct |
10 |
Correct |
89 ms |
24420 KB |
Output is correct |
11 |
Correct |
12 ms |
18132 KB |
Output is correct |
12 |
Correct |
60 ms |
24080 KB |
Output is correct |
13 |
Correct |
58 ms |
23796 KB |
Output is correct |
14 |
Correct |
66 ms |
23832 KB |
Output is correct |
15 |
Correct |
55 ms |
23800 KB |
Output is correct |
16 |
Correct |
72 ms |
24044 KB |
Output is correct |
17 |
Correct |
64 ms |
23804 KB |
Output is correct |
18 |
Correct |
72 ms |
23920 KB |
Output is correct |
19 |
Correct |
12 ms |
17920 KB |
Output is correct |
20 |
Correct |
11 ms |
17876 KB |
Output is correct |
21 |
Correct |
10 ms |
17876 KB |
Output is correct |
22 |
Correct |
89 ms |
20048 KB |
Output is correct |
23 |
Correct |
94 ms |
24488 KB |
Output is correct |
24 |
Correct |
84 ms |
24280 KB |
Output is correct |
25 |
Correct |
115 ms |
24448 KB |
Output is correct |
26 |
Correct |
13 ms |
18204 KB |
Output is correct |
27 |
Correct |
66 ms |
24200 KB |
Output is correct |
28 |
Correct |
70 ms |
23916 KB |
Output is correct |
29 |
Correct |
68 ms |
23868 KB |
Output is correct |
30 |
Correct |
66 ms |
23824 KB |
Output is correct |
31 |
Correct |
90 ms |
24124 KB |
Output is correct |
32 |
Correct |
84 ms |
23876 KB |
Output is correct |
33 |
Correct |
79 ms |
23932 KB |
Output is correct |
34 |
Correct |
1433 ms |
91392 KB |
Output is correct |
35 |
Correct |
1512 ms |
87840 KB |
Output is correct |
36 |
Correct |
1542 ms |
87824 KB |
Output is correct |
37 |
Incorrect |
1315 ms |
94196 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
20032 KB |
Output is correct |
2 |
Correct |
59 ms |
24368 KB |
Output is correct |
3 |
Correct |
61 ms |
24108 KB |
Output is correct |
4 |
Correct |
96 ms |
24408 KB |
Output is correct |
5 |
Correct |
12 ms |
18176 KB |
Output is correct |
6 |
Correct |
62 ms |
24112 KB |
Output is correct |
7 |
Correct |
94 ms |
20080 KB |
Output is correct |
8 |
Correct |
82 ms |
24280 KB |
Output is correct |
9 |
Correct |
71 ms |
24068 KB |
Output is correct |
10 |
Correct |
89 ms |
24420 KB |
Output is correct |
11 |
Correct |
12 ms |
18132 KB |
Output is correct |
12 |
Correct |
60 ms |
24080 KB |
Output is correct |
13 |
Correct |
58 ms |
23796 KB |
Output is correct |
14 |
Correct |
66 ms |
23832 KB |
Output is correct |
15 |
Correct |
55 ms |
23800 KB |
Output is correct |
16 |
Correct |
72 ms |
24044 KB |
Output is correct |
17 |
Correct |
64 ms |
23804 KB |
Output is correct |
18 |
Correct |
72 ms |
23920 KB |
Output is correct |
19 |
Correct |
87 ms |
20056 KB |
Output is correct |
20 |
Correct |
63 ms |
24340 KB |
Output is correct |
21 |
Correct |
69 ms |
24152 KB |
Output is correct |
22 |
Correct |
103 ms |
24408 KB |
Output is correct |
23 |
Correct |
12 ms |
18132 KB |
Output is correct |
24 |
Correct |
63 ms |
24064 KB |
Output is correct |
25 |
Correct |
58 ms |
23828 KB |
Output is correct |
26 |
Correct |
70 ms |
23744 KB |
Output is correct |
27 |
Correct |
57 ms |
23700 KB |
Output is correct |
28 |
Correct |
71 ms |
24068 KB |
Output is correct |
29 |
Correct |
60 ms |
23744 KB |
Output is correct |
30 |
Correct |
75 ms |
23812 KB |
Output is correct |
31 |
Correct |
1384 ms |
91056 KB |
Output is correct |
32 |
Correct |
1402 ms |
97408 KB |
Output is correct |
33 |
Correct |
1290 ms |
90948 KB |
Output is correct |
34 |
Correct |
1086 ms |
94880 KB |
Output is correct |
35 |
Execution timed out |
6080 ms |
85940 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
20032 KB |
Output is correct |
2 |
Correct |
59 ms |
24368 KB |
Output is correct |
3 |
Correct |
61 ms |
24108 KB |
Output is correct |
4 |
Correct |
96 ms |
24408 KB |
Output is correct |
5 |
Correct |
12 ms |
18176 KB |
Output is correct |
6 |
Correct |
62 ms |
24112 KB |
Output is correct |
7 |
Correct |
94 ms |
20080 KB |
Output is correct |
8 |
Correct |
82 ms |
24280 KB |
Output is correct |
9 |
Correct |
71 ms |
24068 KB |
Output is correct |
10 |
Correct |
89 ms |
24420 KB |
Output is correct |
11 |
Correct |
12 ms |
18132 KB |
Output is correct |
12 |
Correct |
60 ms |
24080 KB |
Output is correct |
13 |
Correct |
58 ms |
23796 KB |
Output is correct |
14 |
Correct |
66 ms |
23832 KB |
Output is correct |
15 |
Correct |
55 ms |
23800 KB |
Output is correct |
16 |
Correct |
72 ms |
24044 KB |
Output is correct |
17 |
Correct |
64 ms |
23804 KB |
Output is correct |
18 |
Correct |
72 ms |
23920 KB |
Output is correct |
19 |
Correct |
87 ms |
20056 KB |
Output is correct |
20 |
Correct |
63 ms |
24340 KB |
Output is correct |
21 |
Correct |
69 ms |
24152 KB |
Output is correct |
22 |
Correct |
103 ms |
24408 KB |
Output is correct |
23 |
Correct |
12 ms |
18132 KB |
Output is correct |
24 |
Correct |
63 ms |
24064 KB |
Output is correct |
25 |
Correct |
58 ms |
23828 KB |
Output is correct |
26 |
Correct |
70 ms |
23744 KB |
Output is correct |
27 |
Correct |
57 ms |
23700 KB |
Output is correct |
28 |
Correct |
71 ms |
24068 KB |
Output is correct |
29 |
Correct |
60 ms |
23744 KB |
Output is correct |
30 |
Correct |
75 ms |
23812 KB |
Output is correct |
31 |
Correct |
1384 ms |
91056 KB |
Output is correct |
32 |
Correct |
1402 ms |
97408 KB |
Output is correct |
33 |
Correct |
1290 ms |
90948 KB |
Output is correct |
34 |
Correct |
1086 ms |
94880 KB |
Output is correct |
35 |
Execution timed out |
6080 ms |
85940 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |