#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);
}
++it;
}
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";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
13012 KB |
Output is correct |
2 |
Correct |
72 ms |
19076 KB |
Output is correct |
3 |
Correct |
69 ms |
18628 KB |
Output is correct |
4 |
Correct |
98 ms |
18832 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
75 ms |
18596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
13012 KB |
Output is correct |
2 |
Correct |
74 ms |
19112 KB |
Output is correct |
3 |
Correct |
69 ms |
18636 KB |
Output is correct |
4 |
Correct |
90 ms |
18832 KB |
Output is correct |
5 |
Correct |
8 ms |
12116 KB |
Output is correct |
6 |
Correct |
70 ms |
18604 KB |
Output is correct |
7 |
Correct |
66 ms |
18636 KB |
Output is correct |
8 |
Correct |
65 ms |
18660 KB |
Output is correct |
9 |
Correct |
84 ms |
18596 KB |
Output is correct |
10 |
Correct |
87 ms |
18756 KB |
Output is correct |
11 |
Correct |
89 ms |
18696 KB |
Output is correct |
12 |
Correct |
83 ms |
18524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
13012 KB |
Output is correct |
2 |
Correct |
74 ms |
19112 KB |
Output is correct |
3 |
Correct |
69 ms |
18636 KB |
Output is correct |
4 |
Correct |
90 ms |
18832 KB |
Output is correct |
5 |
Correct |
8 ms |
12116 KB |
Output is correct |
6 |
Correct |
70 ms |
18604 KB |
Output is correct |
7 |
Correct |
66 ms |
18636 KB |
Output is correct |
8 |
Correct |
65 ms |
18660 KB |
Output is correct |
9 |
Correct |
84 ms |
18596 KB |
Output is correct |
10 |
Correct |
87 ms |
18756 KB |
Output is correct |
11 |
Correct |
89 ms |
18696 KB |
Output is correct |
12 |
Correct |
83 ms |
18524 KB |
Output is correct |
13 |
Correct |
18 ms |
12932 KB |
Output is correct |
14 |
Correct |
77 ms |
19148 KB |
Output is correct |
15 |
Correct |
68 ms |
18680 KB |
Output is correct |
16 |
Correct |
91 ms |
18832 KB |
Output is correct |
17 |
Correct |
8 ms |
12116 KB |
Output is correct |
18 |
Correct |
72 ms |
18564 KB |
Output is correct |
19 |
Correct |
80 ms |
18640 KB |
Output is correct |
20 |
Correct |
72 ms |
18572 KB |
Output is correct |
21 |
Correct |
65 ms |
18636 KB |
Output is correct |
22 |
Correct |
80 ms |
18820 KB |
Output is correct |
23 |
Correct |
74 ms |
18592 KB |
Output is correct |
24 |
Correct |
72 ms |
18532 KB |
Output is correct |
25 |
Correct |
1360 ms |
61516 KB |
Output is correct |
26 |
Correct |
1334 ms |
66176 KB |
Output is correct |
27 |
Correct |
1260 ms |
61968 KB |
Output is correct |
28 |
Correct |
1018 ms |
65948 KB |
Output is correct |
29 |
Execution timed out |
6035 ms |
56668 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
13012 KB |
Output is correct |
2 |
Correct |
74 ms |
19112 KB |
Output is correct |
3 |
Correct |
69 ms |
18636 KB |
Output is correct |
4 |
Correct |
90 ms |
18832 KB |
Output is correct |
5 |
Correct |
8 ms |
12116 KB |
Output is correct |
6 |
Correct |
70 ms |
18604 KB |
Output is correct |
7 |
Correct |
66 ms |
18636 KB |
Output is correct |
8 |
Correct |
65 ms |
18660 KB |
Output is correct |
9 |
Correct |
84 ms |
18596 KB |
Output is correct |
10 |
Correct |
87 ms |
18756 KB |
Output is correct |
11 |
Correct |
89 ms |
18696 KB |
Output is correct |
12 |
Correct |
83 ms |
18524 KB |
Output is correct |
13 |
Correct |
18 ms |
12932 KB |
Output is correct |
14 |
Correct |
77 ms |
19148 KB |
Output is correct |
15 |
Correct |
68 ms |
18680 KB |
Output is correct |
16 |
Correct |
91 ms |
18832 KB |
Output is correct |
17 |
Correct |
8 ms |
12116 KB |
Output is correct |
18 |
Correct |
72 ms |
18564 KB |
Output is correct |
19 |
Correct |
80 ms |
18640 KB |
Output is correct |
20 |
Correct |
72 ms |
18572 KB |
Output is correct |
21 |
Correct |
65 ms |
18636 KB |
Output is correct |
22 |
Correct |
80 ms |
18820 KB |
Output is correct |
23 |
Correct |
74 ms |
18592 KB |
Output is correct |
24 |
Correct |
72 ms |
18532 KB |
Output is correct |
25 |
Correct |
1360 ms |
61516 KB |
Output is correct |
26 |
Correct |
1334 ms |
66176 KB |
Output is correct |
27 |
Correct |
1260 ms |
61968 KB |
Output is correct |
28 |
Correct |
1018 ms |
65948 KB |
Output is correct |
29 |
Execution timed out |
6035 ms |
56668 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
13012 KB |
Output is correct |
2 |
Correct |
72 ms |
19076 KB |
Output is correct |
3 |
Correct |
69 ms |
18628 KB |
Output is correct |
4 |
Correct |
98 ms |
18832 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
75 ms |
18596 KB |
Output is correct |
7 |
Correct |
18 ms |
13012 KB |
Output is correct |
8 |
Correct |
74 ms |
19112 KB |
Output is correct |
9 |
Correct |
69 ms |
18636 KB |
Output is correct |
10 |
Correct |
90 ms |
18832 KB |
Output is correct |
11 |
Correct |
8 ms |
12116 KB |
Output is correct |
12 |
Correct |
70 ms |
18604 KB |
Output is correct |
13 |
Correct |
66 ms |
18636 KB |
Output is correct |
14 |
Correct |
65 ms |
18660 KB |
Output is correct |
15 |
Correct |
84 ms |
18596 KB |
Output is correct |
16 |
Correct |
87 ms |
18756 KB |
Output is correct |
17 |
Correct |
89 ms |
18696 KB |
Output is correct |
18 |
Correct |
83 ms |
18524 KB |
Output is correct |
19 |
Correct |
8 ms |
11988 KB |
Output is correct |
20 |
Correct |
9 ms |
11988 KB |
Output is correct |
21 |
Correct |
8 ms |
11988 KB |
Output is correct |
22 |
Correct |
21 ms |
13040 KB |
Output is correct |
23 |
Correct |
73 ms |
19188 KB |
Output is correct |
24 |
Correct |
69 ms |
18672 KB |
Output is correct |
25 |
Correct |
89 ms |
18820 KB |
Output is correct |
26 |
Correct |
8 ms |
12152 KB |
Output is correct |
27 |
Correct |
73 ms |
18616 KB |
Output is correct |
28 |
Correct |
70 ms |
18728 KB |
Output is correct |
29 |
Correct |
74 ms |
18636 KB |
Output is correct |
30 |
Correct |
75 ms |
18540 KB |
Output is correct |
31 |
Correct |
73 ms |
18760 KB |
Output is correct |
32 |
Correct |
66 ms |
18636 KB |
Output is correct |
33 |
Correct |
70 ms |
18600 KB |
Output is correct |
34 |
Incorrect |
1375 ms |
61516 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
13012 KB |
Output is correct |
2 |
Correct |
72 ms |
19076 KB |
Output is correct |
3 |
Correct |
69 ms |
18628 KB |
Output is correct |
4 |
Correct |
98 ms |
18832 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
75 ms |
18596 KB |
Output is correct |
7 |
Correct |
18 ms |
13012 KB |
Output is correct |
8 |
Correct |
74 ms |
19112 KB |
Output is correct |
9 |
Correct |
69 ms |
18636 KB |
Output is correct |
10 |
Correct |
90 ms |
18832 KB |
Output is correct |
11 |
Correct |
8 ms |
12116 KB |
Output is correct |
12 |
Correct |
70 ms |
18604 KB |
Output is correct |
13 |
Correct |
66 ms |
18636 KB |
Output is correct |
14 |
Correct |
65 ms |
18660 KB |
Output is correct |
15 |
Correct |
84 ms |
18596 KB |
Output is correct |
16 |
Correct |
87 ms |
18756 KB |
Output is correct |
17 |
Correct |
89 ms |
18696 KB |
Output is correct |
18 |
Correct |
83 ms |
18524 KB |
Output is correct |
19 |
Correct |
18 ms |
12932 KB |
Output is correct |
20 |
Correct |
77 ms |
19148 KB |
Output is correct |
21 |
Correct |
68 ms |
18680 KB |
Output is correct |
22 |
Correct |
91 ms |
18832 KB |
Output is correct |
23 |
Correct |
8 ms |
12116 KB |
Output is correct |
24 |
Correct |
72 ms |
18564 KB |
Output is correct |
25 |
Correct |
80 ms |
18640 KB |
Output is correct |
26 |
Correct |
72 ms |
18572 KB |
Output is correct |
27 |
Correct |
65 ms |
18636 KB |
Output is correct |
28 |
Correct |
80 ms |
18820 KB |
Output is correct |
29 |
Correct |
74 ms |
18592 KB |
Output is correct |
30 |
Correct |
72 ms |
18532 KB |
Output is correct |
31 |
Correct |
1360 ms |
61516 KB |
Output is correct |
32 |
Correct |
1334 ms |
66176 KB |
Output is correct |
33 |
Correct |
1260 ms |
61968 KB |
Output is correct |
34 |
Correct |
1018 ms |
65948 KB |
Output is correct |
35 |
Execution timed out |
6035 ms |
56668 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
13012 KB |
Output is correct |
2 |
Correct |
72 ms |
19076 KB |
Output is correct |
3 |
Correct |
69 ms |
18628 KB |
Output is correct |
4 |
Correct |
98 ms |
18832 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
75 ms |
18596 KB |
Output is correct |
7 |
Correct |
18 ms |
13012 KB |
Output is correct |
8 |
Correct |
74 ms |
19112 KB |
Output is correct |
9 |
Correct |
69 ms |
18636 KB |
Output is correct |
10 |
Correct |
90 ms |
18832 KB |
Output is correct |
11 |
Correct |
8 ms |
12116 KB |
Output is correct |
12 |
Correct |
70 ms |
18604 KB |
Output is correct |
13 |
Correct |
66 ms |
18636 KB |
Output is correct |
14 |
Correct |
65 ms |
18660 KB |
Output is correct |
15 |
Correct |
84 ms |
18596 KB |
Output is correct |
16 |
Correct |
87 ms |
18756 KB |
Output is correct |
17 |
Correct |
89 ms |
18696 KB |
Output is correct |
18 |
Correct |
83 ms |
18524 KB |
Output is correct |
19 |
Correct |
18 ms |
12932 KB |
Output is correct |
20 |
Correct |
77 ms |
19148 KB |
Output is correct |
21 |
Correct |
68 ms |
18680 KB |
Output is correct |
22 |
Correct |
91 ms |
18832 KB |
Output is correct |
23 |
Correct |
8 ms |
12116 KB |
Output is correct |
24 |
Correct |
72 ms |
18564 KB |
Output is correct |
25 |
Correct |
80 ms |
18640 KB |
Output is correct |
26 |
Correct |
72 ms |
18572 KB |
Output is correct |
27 |
Correct |
65 ms |
18636 KB |
Output is correct |
28 |
Correct |
80 ms |
18820 KB |
Output is correct |
29 |
Correct |
74 ms |
18592 KB |
Output is correct |
30 |
Correct |
72 ms |
18532 KB |
Output is correct |
31 |
Correct |
1360 ms |
61516 KB |
Output is correct |
32 |
Correct |
1334 ms |
66176 KB |
Output is correct |
33 |
Correct |
1260 ms |
61968 KB |
Output is correct |
34 |
Correct |
1018 ms |
65948 KB |
Output is correct |
35 |
Execution timed out |
6035 ms |
56668 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |