#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
#include <bits/stdc++.h>
using namespace std;
constexpr size_t N = 250000;
vector<unsigned> g[N], t[N];
unsigned l[N], resid[N], c[N];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, m;
cin >> n >> m;
for (size_t i = 0; i < m; ++i)
{
unsigned u, v;
cin >> u >> v;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
size_t k;
cin >> k;
for (size_t i = 0; i < k; ++i)
{
size_t L;
cin >> L;
for (size_t j = 0; j < L; ++j)
{
unsigned u;
cin >> u;
l[u - 1] = L;
resid[u - 1] = j;
t[u - 1].resize(L);
c[u - 1] = i;
}
}
for (size_t i = 0; i < n; ++i)
{
if (t[i].empty())
t[i].resize(1);
fill(t[i].begin(), t[i].end(), UINT_MAX);
}
priority_queue<pair<unsigned, unsigned>, vector<pair<unsigned, unsigned>>,
greater<pair<unsigned, unsigned>>>
q;
q.emplace(0, 0);
t[0][0] = 0;
while (!q.empty())
{
auto [d, u] = q.top();
q.pop();
if (d != t[u][l[u] ? d % l[u] : 0])
continue;
if (u == n - 1)
{
cout << d << '\n';
return 0;
}
if (!l[u])
{
for (unsigned const &w : g[u])
if (!l[w])
{
if (d + 1 < t[w][0])
{
t[w][0] = d + 1;
q.emplace(d + 1, w);
}
}
else
{
if ((d + 1) % l[w] != resid[w] && d + 1 < t[w][(d + 1) % l[w]])
{
t[w][(d + 1) % l[w]] = d + 1;
q.emplace(d + 1, w);
}
unsigned const x = d + (resid[w] - (d % l[w]) + l[w]) % l[w];
if (x + 1 < t[w][(x + 1) % l[w]])
{
t[w][(x + 1) % l[w]] = x + 1;
q.emplace(x + 1, w);
}
}
}
else
{
for (auto it = g[u].begin(); it != g[u].end();)
{
unsigned &w = *it;
if (!l[w])
{
if (d + 1 < t[w][0])
{
t[w][0] = d + 1;
q.emplace(d + 1, w);
}
size_t i = it - g[u].begin();
swap(w, g[u].back());
g[u].pop_back();
it = g[u].begin() + i;
}
else
{
if (c[u] == c[w])
{
if ((d + 1) % l[u] != resid[w] &&
(resid[u] != (resid[w] + 1) % l[u] || (d % l[u]) != resid[w]) &&
d + 1 < t[w][(d + 1) % l[u]])
{
t[w][(d + 1) % l[u]] = d + 1;
q.emplace(d + 1, w);
}
++it;
}
else
{
unsigned x = d + (resid[w] - d % l[w] + l[w]) % l[w];
if (x % l[u] != resid[u])
{
if ((d + 1) % l[w] != resid[w] && d + 1 < t[w][(d + 1) % l[w]])
{
t[w][(d + 1) % l[w]] = d + 1;
q.emplace(d + 1, w);
}
if (x + 1 < t[w][(x + 1) % l[w]])
{
t[w][(x + 1) % l[w]] = x + 1;
q.emplace(x + 1, w);
}
size_t i = it - g[u].begin();
swap(w, g[u].back());
g[u].pop_back();
it = g[u].begin() + i;
}
else
{
if ((d + 1) % l[w] != resid[w] && d + 1 < t[w][(d + 1) % l[w]])
{
t[w][(d + 1) % l[w]] = d + 1;
q.emplace(d + 1, w);
}
d += l[u];
if ((d + 1) % l[w] != resid[w] &&
d + 1 < t[w][(d + 1) % l[w]])
{
t[w][(d + 1) % l[w]] = d + 1;
q.emplace(d + 1, w);
}
x = d + (resid[w] - d % l[w] + l[w]) % l[w];
if (x % l[u] != resid[u] && x + 1 < t[w][(x + 1) % l[w]])
{
t[w][(x + 1) % l[w]] = x + 1;
q.emplace(x + 1, w);
}
++it;
}
}
}
}
if ((d + 1) % l[u] != resid[u] && d + 1 < t[u][(d + 1) % l[u]])
{
t[u][(d + 1) % l[u]] = d + 1;
q.emplace(d + 1, u);
}
}
}
cout << "impossible\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
13012 KB |
Output is correct |
2 |
Correct |
88 ms |
19188 KB |
Output is correct |
3 |
Correct |
71 ms |
18668 KB |
Output is correct |
4 |
Correct |
95 ms |
18768 KB |
Output is correct |
5 |
Correct |
8 ms |
12116 KB |
Output is correct |
6 |
Correct |
67 ms |
18592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
13012 KB |
Output is correct |
2 |
Correct |
68 ms |
19112 KB |
Output is correct |
3 |
Correct |
67 ms |
18572 KB |
Output is correct |
4 |
Correct |
90 ms |
18828 KB |
Output is correct |
5 |
Correct |
8 ms |
12116 KB |
Output is correct |
6 |
Correct |
68 ms |
18568 KB |
Output is correct |
7 |
Correct |
64 ms |
18640 KB |
Output is correct |
8 |
Correct |
62 ms |
18568 KB |
Output is correct |
9 |
Correct |
70 ms |
18588 KB |
Output is correct |
10 |
Correct |
67 ms |
18748 KB |
Output is correct |
11 |
Correct |
64 ms |
18700 KB |
Output is correct |
12 |
Correct |
73 ms |
18636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
13012 KB |
Output is correct |
2 |
Correct |
68 ms |
19112 KB |
Output is correct |
3 |
Correct |
67 ms |
18572 KB |
Output is correct |
4 |
Correct |
90 ms |
18828 KB |
Output is correct |
5 |
Correct |
8 ms |
12116 KB |
Output is correct |
6 |
Correct |
68 ms |
18568 KB |
Output is correct |
7 |
Correct |
64 ms |
18640 KB |
Output is correct |
8 |
Correct |
62 ms |
18568 KB |
Output is correct |
9 |
Correct |
70 ms |
18588 KB |
Output is correct |
10 |
Correct |
67 ms |
18748 KB |
Output is correct |
11 |
Correct |
64 ms |
18700 KB |
Output is correct |
12 |
Correct |
73 ms |
18636 KB |
Output is correct |
13 |
Correct |
19 ms |
13020 KB |
Output is correct |
14 |
Correct |
73 ms |
19164 KB |
Output is correct |
15 |
Correct |
68 ms |
18656 KB |
Output is correct |
16 |
Correct |
102 ms |
18836 KB |
Output is correct |
17 |
Correct |
8 ms |
12116 KB |
Output is correct |
18 |
Correct |
84 ms |
18676 KB |
Output is correct |
19 |
Correct |
65 ms |
18588 KB |
Output is correct |
20 |
Correct |
64 ms |
18636 KB |
Output is correct |
21 |
Correct |
63 ms |
18532 KB |
Output is correct |
22 |
Correct |
85 ms |
18812 KB |
Output is correct |
23 |
Correct |
68 ms |
18636 KB |
Output is correct |
24 |
Correct |
65 ms |
18632 KB |
Output is correct |
25 |
Correct |
1464 ms |
61312 KB |
Output is correct |
26 |
Correct |
1316 ms |
66260 KB |
Output is correct |
27 |
Correct |
1179 ms |
62016 KB |
Output is correct |
28 |
Correct |
1130 ms |
65968 KB |
Output is correct |
29 |
Correct |
1541 ms |
56476 KB |
Output is correct |
30 |
Correct |
1493 ms |
97996 KB |
Output is correct |
31 |
Correct |
1471 ms |
104304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
13012 KB |
Output is correct |
2 |
Correct |
68 ms |
19112 KB |
Output is correct |
3 |
Correct |
67 ms |
18572 KB |
Output is correct |
4 |
Correct |
90 ms |
18828 KB |
Output is correct |
5 |
Correct |
8 ms |
12116 KB |
Output is correct |
6 |
Correct |
68 ms |
18568 KB |
Output is correct |
7 |
Correct |
64 ms |
18640 KB |
Output is correct |
8 |
Correct |
62 ms |
18568 KB |
Output is correct |
9 |
Correct |
70 ms |
18588 KB |
Output is correct |
10 |
Correct |
67 ms |
18748 KB |
Output is correct |
11 |
Correct |
64 ms |
18700 KB |
Output is correct |
12 |
Correct |
73 ms |
18636 KB |
Output is correct |
13 |
Correct |
19 ms |
13020 KB |
Output is correct |
14 |
Correct |
73 ms |
19164 KB |
Output is correct |
15 |
Correct |
68 ms |
18656 KB |
Output is correct |
16 |
Correct |
102 ms |
18836 KB |
Output is correct |
17 |
Correct |
8 ms |
12116 KB |
Output is correct |
18 |
Correct |
84 ms |
18676 KB |
Output is correct |
19 |
Correct |
65 ms |
18588 KB |
Output is correct |
20 |
Correct |
64 ms |
18636 KB |
Output is correct |
21 |
Correct |
63 ms |
18532 KB |
Output is correct |
22 |
Correct |
85 ms |
18812 KB |
Output is correct |
23 |
Correct |
68 ms |
18636 KB |
Output is correct |
24 |
Correct |
65 ms |
18632 KB |
Output is correct |
25 |
Correct |
1464 ms |
61312 KB |
Output is correct |
26 |
Correct |
1316 ms |
66260 KB |
Output is correct |
27 |
Correct |
1179 ms |
62016 KB |
Output is correct |
28 |
Correct |
1130 ms |
65968 KB |
Output is correct |
29 |
Correct |
1541 ms |
56476 KB |
Output is correct |
30 |
Correct |
1493 ms |
97996 KB |
Output is correct |
31 |
Correct |
1471 ms |
104304 KB |
Output is correct |
32 |
Correct |
19 ms |
13524 KB |
Output is correct |
33 |
Correct |
77 ms |
20340 KB |
Output is correct |
34 |
Correct |
67 ms |
19748 KB |
Output is correct |
35 |
Correct |
92 ms |
19984 KB |
Output is correct |
36 |
Correct |
8 ms |
12116 KB |
Output is correct |
37 |
Correct |
69 ms |
19764 KB |
Output is correct |
38 |
Correct |
87 ms |
19672 KB |
Output is correct |
39 |
Correct |
71 ms |
19788 KB |
Output is correct |
40 |
Correct |
83 ms |
19700 KB |
Output is correct |
41 |
Correct |
95 ms |
19980 KB |
Output is correct |
42 |
Correct |
70 ms |
19788 KB |
Output is correct |
43 |
Correct |
90 ms |
19880 KB |
Output is correct |
44 |
Correct |
1433 ms |
99904 KB |
Output is correct |
45 |
Correct |
1428 ms |
104792 KB |
Output is correct |
46 |
Correct |
1317 ms |
100760 KB |
Output is correct |
47 |
Correct |
1111 ms |
104604 KB |
Output is correct |
48 |
Correct |
1465 ms |
95592 KB |
Output is correct |
49 |
Correct |
1467 ms |
98008 KB |
Output is correct |
50 |
Correct |
1504 ms |
104396 KB |
Output is correct |
51 |
Correct |
1927 ms |
109592 KB |
Output is correct |
52 |
Correct |
2257 ms |
120292 KB |
Output is correct |
53 |
Correct |
1897 ms |
110332 KB |
Output is correct |
54 |
Correct |
975 ms |
96480 KB |
Output is correct |
55 |
Execution timed out |
6020 ms |
110176 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
13012 KB |
Output is correct |
2 |
Correct |
88 ms |
19188 KB |
Output is correct |
3 |
Correct |
71 ms |
18668 KB |
Output is correct |
4 |
Correct |
95 ms |
18768 KB |
Output is correct |
5 |
Correct |
8 ms |
12116 KB |
Output is correct |
6 |
Correct |
67 ms |
18592 KB |
Output is correct |
7 |
Correct |
17 ms |
13012 KB |
Output is correct |
8 |
Correct |
68 ms |
19112 KB |
Output is correct |
9 |
Correct |
67 ms |
18572 KB |
Output is correct |
10 |
Correct |
90 ms |
18828 KB |
Output is correct |
11 |
Correct |
8 ms |
12116 KB |
Output is correct |
12 |
Correct |
68 ms |
18568 KB |
Output is correct |
13 |
Correct |
64 ms |
18640 KB |
Output is correct |
14 |
Correct |
62 ms |
18568 KB |
Output is correct |
15 |
Correct |
70 ms |
18588 KB |
Output is correct |
16 |
Correct |
67 ms |
18748 KB |
Output is correct |
17 |
Correct |
64 ms |
18700 KB |
Output is correct |
18 |
Correct |
73 ms |
18636 KB |
Output is correct |
19 |
Correct |
7 ms |
11988 KB |
Output is correct |
20 |
Correct |
7 ms |
11988 KB |
Output is correct |
21 |
Correct |
7 ms |
12052 KB |
Output is correct |
22 |
Correct |
19 ms |
12928 KB |
Output is correct |
23 |
Correct |
68 ms |
19148 KB |
Output is correct |
24 |
Correct |
69 ms |
18712 KB |
Output is correct |
25 |
Correct |
95 ms |
18816 KB |
Output is correct |
26 |
Correct |
8 ms |
12116 KB |
Output is correct |
27 |
Correct |
68 ms |
18608 KB |
Output is correct |
28 |
Correct |
62 ms |
18552 KB |
Output is correct |
29 |
Correct |
63 ms |
18580 KB |
Output is correct |
30 |
Correct |
58 ms |
18648 KB |
Output is correct |
31 |
Correct |
68 ms |
18764 KB |
Output is correct |
32 |
Correct |
63 ms |
18696 KB |
Output is correct |
33 |
Correct |
64 ms |
18636 KB |
Output is correct |
34 |
Incorrect |
1358 ms |
61544 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
13012 KB |
Output is correct |
2 |
Correct |
88 ms |
19188 KB |
Output is correct |
3 |
Correct |
71 ms |
18668 KB |
Output is correct |
4 |
Correct |
95 ms |
18768 KB |
Output is correct |
5 |
Correct |
8 ms |
12116 KB |
Output is correct |
6 |
Correct |
67 ms |
18592 KB |
Output is correct |
7 |
Correct |
17 ms |
13012 KB |
Output is correct |
8 |
Correct |
68 ms |
19112 KB |
Output is correct |
9 |
Correct |
67 ms |
18572 KB |
Output is correct |
10 |
Correct |
90 ms |
18828 KB |
Output is correct |
11 |
Correct |
8 ms |
12116 KB |
Output is correct |
12 |
Correct |
68 ms |
18568 KB |
Output is correct |
13 |
Correct |
64 ms |
18640 KB |
Output is correct |
14 |
Correct |
62 ms |
18568 KB |
Output is correct |
15 |
Correct |
70 ms |
18588 KB |
Output is correct |
16 |
Correct |
67 ms |
18748 KB |
Output is correct |
17 |
Correct |
64 ms |
18700 KB |
Output is correct |
18 |
Correct |
73 ms |
18636 KB |
Output is correct |
19 |
Correct |
19 ms |
13020 KB |
Output is correct |
20 |
Correct |
73 ms |
19164 KB |
Output is correct |
21 |
Correct |
68 ms |
18656 KB |
Output is correct |
22 |
Correct |
102 ms |
18836 KB |
Output is correct |
23 |
Correct |
8 ms |
12116 KB |
Output is correct |
24 |
Correct |
84 ms |
18676 KB |
Output is correct |
25 |
Correct |
65 ms |
18588 KB |
Output is correct |
26 |
Correct |
64 ms |
18636 KB |
Output is correct |
27 |
Correct |
63 ms |
18532 KB |
Output is correct |
28 |
Correct |
85 ms |
18812 KB |
Output is correct |
29 |
Correct |
68 ms |
18636 KB |
Output is correct |
30 |
Correct |
65 ms |
18632 KB |
Output is correct |
31 |
Correct |
1464 ms |
61312 KB |
Output is correct |
32 |
Correct |
1316 ms |
66260 KB |
Output is correct |
33 |
Correct |
1179 ms |
62016 KB |
Output is correct |
34 |
Correct |
1130 ms |
65968 KB |
Output is correct |
35 |
Correct |
1541 ms |
56476 KB |
Output is correct |
36 |
Correct |
1493 ms |
97996 KB |
Output is correct |
37 |
Correct |
1471 ms |
104304 KB |
Output is correct |
38 |
Correct |
7 ms |
11988 KB |
Output is correct |
39 |
Correct |
7 ms |
11988 KB |
Output is correct |
40 |
Correct |
7 ms |
12052 KB |
Output is correct |
41 |
Correct |
19 ms |
12928 KB |
Output is correct |
42 |
Correct |
68 ms |
19148 KB |
Output is correct |
43 |
Correct |
69 ms |
18712 KB |
Output is correct |
44 |
Correct |
95 ms |
18816 KB |
Output is correct |
45 |
Correct |
8 ms |
12116 KB |
Output is correct |
46 |
Correct |
68 ms |
18608 KB |
Output is correct |
47 |
Correct |
62 ms |
18552 KB |
Output is correct |
48 |
Correct |
63 ms |
18580 KB |
Output is correct |
49 |
Correct |
58 ms |
18648 KB |
Output is correct |
50 |
Correct |
68 ms |
18764 KB |
Output is correct |
51 |
Correct |
63 ms |
18696 KB |
Output is correct |
52 |
Correct |
64 ms |
18636 KB |
Output is correct |
53 |
Incorrect |
1358 ms |
61544 KB |
Output isn't correct |
54 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
13012 KB |
Output is correct |
2 |
Correct |
88 ms |
19188 KB |
Output is correct |
3 |
Correct |
71 ms |
18668 KB |
Output is correct |
4 |
Correct |
95 ms |
18768 KB |
Output is correct |
5 |
Correct |
8 ms |
12116 KB |
Output is correct |
6 |
Correct |
67 ms |
18592 KB |
Output is correct |
7 |
Correct |
17 ms |
13012 KB |
Output is correct |
8 |
Correct |
68 ms |
19112 KB |
Output is correct |
9 |
Correct |
67 ms |
18572 KB |
Output is correct |
10 |
Correct |
90 ms |
18828 KB |
Output is correct |
11 |
Correct |
8 ms |
12116 KB |
Output is correct |
12 |
Correct |
68 ms |
18568 KB |
Output is correct |
13 |
Correct |
64 ms |
18640 KB |
Output is correct |
14 |
Correct |
62 ms |
18568 KB |
Output is correct |
15 |
Correct |
70 ms |
18588 KB |
Output is correct |
16 |
Correct |
67 ms |
18748 KB |
Output is correct |
17 |
Correct |
64 ms |
18700 KB |
Output is correct |
18 |
Correct |
73 ms |
18636 KB |
Output is correct |
19 |
Correct |
19 ms |
13020 KB |
Output is correct |
20 |
Correct |
73 ms |
19164 KB |
Output is correct |
21 |
Correct |
68 ms |
18656 KB |
Output is correct |
22 |
Correct |
102 ms |
18836 KB |
Output is correct |
23 |
Correct |
8 ms |
12116 KB |
Output is correct |
24 |
Correct |
84 ms |
18676 KB |
Output is correct |
25 |
Correct |
65 ms |
18588 KB |
Output is correct |
26 |
Correct |
64 ms |
18636 KB |
Output is correct |
27 |
Correct |
63 ms |
18532 KB |
Output is correct |
28 |
Correct |
85 ms |
18812 KB |
Output is correct |
29 |
Correct |
68 ms |
18636 KB |
Output is correct |
30 |
Correct |
65 ms |
18632 KB |
Output is correct |
31 |
Correct |
1464 ms |
61312 KB |
Output is correct |
32 |
Correct |
1316 ms |
66260 KB |
Output is correct |
33 |
Correct |
1179 ms |
62016 KB |
Output is correct |
34 |
Correct |
1130 ms |
65968 KB |
Output is correct |
35 |
Correct |
1541 ms |
56476 KB |
Output is correct |
36 |
Correct |
1493 ms |
97996 KB |
Output is correct |
37 |
Correct |
1471 ms |
104304 KB |
Output is correct |
38 |
Correct |
19 ms |
13524 KB |
Output is correct |
39 |
Correct |
77 ms |
20340 KB |
Output is correct |
40 |
Correct |
67 ms |
19748 KB |
Output is correct |
41 |
Correct |
92 ms |
19984 KB |
Output is correct |
42 |
Correct |
8 ms |
12116 KB |
Output is correct |
43 |
Correct |
69 ms |
19764 KB |
Output is correct |
44 |
Correct |
87 ms |
19672 KB |
Output is correct |
45 |
Correct |
71 ms |
19788 KB |
Output is correct |
46 |
Correct |
83 ms |
19700 KB |
Output is correct |
47 |
Correct |
95 ms |
19980 KB |
Output is correct |
48 |
Correct |
70 ms |
19788 KB |
Output is correct |
49 |
Correct |
90 ms |
19880 KB |
Output is correct |
50 |
Correct |
1433 ms |
99904 KB |
Output is correct |
51 |
Correct |
1428 ms |
104792 KB |
Output is correct |
52 |
Correct |
1317 ms |
100760 KB |
Output is correct |
53 |
Correct |
1111 ms |
104604 KB |
Output is correct |
54 |
Correct |
1465 ms |
95592 KB |
Output is correct |
55 |
Correct |
1467 ms |
98008 KB |
Output is correct |
56 |
Correct |
1504 ms |
104396 KB |
Output is correct |
57 |
Correct |
1927 ms |
109592 KB |
Output is correct |
58 |
Correct |
2257 ms |
120292 KB |
Output is correct |
59 |
Correct |
1897 ms |
110332 KB |
Output is correct |
60 |
Correct |
975 ms |
96480 KB |
Output is correct |
61 |
Execution timed out |
6020 ms |
110176 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |