#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, a, b) for (int i = (a); i >= (b); --i)
#define fe(x, y) for (auto& x : y)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define pw(x) (1LL << (x))
#define sz(x) (int)(x).size()
using namespace std;
mt19937_64 rng(228);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key
template <typename T>
bool umn(T& a, T b) { return (a > b ? (a = b, 1) : 0); }
template <typename T>
bool umx(T& a, T b) { return (a < b ? (a = b, 1) : 0); }
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;
const int N = 3e5 + 5;
int n, m, k;
ve<int> g[N];
ve<int> v[N];
bool mark[N];
ve<int> dp[N];
int bad[N];
int nxt[N];
queue<array<int, 3>> q;
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios::sync_with_stdio(0);
cin.tie(0);
#endif
cin >> n >> m;
fr (i, 1, m) {
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
cin >> k;
fr (i, 1, k) {
int sz;
cin >> sz;
v[i].resize(sz);
int idx = 0;
int prv = -1;
fe (x, v[i]) {
cin >> x;
if (prv != -1) nxt[prv] = x;
prv = x;
assert(!mark[x]);
mark[x] = 1;
dp[x].resize(sz, 1e9);
bad[x] = idx;
idx++;
}
nxt[v[i].back()] = v[i][0];
}
fr (i, 1, n) {
if (!mark[i]) {
dp[i].resize(2, 1e9);
bad[i] = -1;
nxt[i] = -1;
}
}
assert(!mark[1]);
assert(!mark[n]);
// assert(k == 1);
dp[1][0] = 0;
q.push({0, 1, 0});
// fr (i, 1, n) {
// cout << i << " " << bad[i] << "\n";
// }
while (sz(q)) {
int v = (q.front())[1];
int rem = (q.front())[2];
int t = (q.front())[0];
q.pop();
assert(dp[v][rem] == t);
assert(rem == t % sz(dp[v]));
int nt = (t + 1) % sz(dp[v]);
bool good = 0;
fe (to, g[v]) {
if (!mark[to] && min(dp[to][0], dp[to][1]) < t) {
good = 1;
break;
}
}
if (bad[v] != nt || good) {
if (dp[v][nt] > t + 1) {
dp[v][nt] = t + 1;
q.push({dp[v][nt], v, nt});
}
}
if (rem == bad[v]) continue;
fe (to, g[v]) {
int nt = (t + 1) % sz(dp[to]);
if (bad[to] == nt) continue;
if ((t + 1) % sz(dp[v]) == bad[v] && nxt[to] == v) continue;
if (to == n) {
cout << t + 1 << "\n";
return 0;
}
if (dp[to][nt] > t + 1) {
dp[to][nt] = t + 1;
q.push({dp[to][nt], to, nt});
}
}
}
int mn = *min_element(all(dp[n]));
if (mn == int(1e9)) {
cout << "impossible\n";
return 0;
}
cout << mn << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
22988 KB |
Output is correct |
2 |
Correct |
100 ms |
29748 KB |
Output is correct |
3 |
Correct |
96 ms |
29196 KB |
Output is correct |
4 |
Correct |
100 ms |
29284 KB |
Output is correct |
5 |
Correct |
12 ms |
21552 KB |
Output is correct |
6 |
Correct |
83 ms |
29236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23008 KB |
Output is correct |
2 |
Correct |
82 ms |
29820 KB |
Output is correct |
3 |
Correct |
74 ms |
29232 KB |
Output is correct |
4 |
Correct |
108 ms |
29260 KB |
Output is correct |
5 |
Correct |
13 ms |
21460 KB |
Output is correct |
6 |
Correct |
89 ms |
29272 KB |
Output is correct |
7 |
Correct |
97 ms |
29260 KB |
Output is correct |
8 |
Correct |
74 ms |
29184 KB |
Output is correct |
9 |
Correct |
80 ms |
29132 KB |
Output is correct |
10 |
Correct |
94 ms |
29260 KB |
Output is correct |
11 |
Correct |
84 ms |
29156 KB |
Output is correct |
12 |
Correct |
85 ms |
29196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23008 KB |
Output is correct |
2 |
Correct |
82 ms |
29820 KB |
Output is correct |
3 |
Correct |
74 ms |
29232 KB |
Output is correct |
4 |
Correct |
108 ms |
29260 KB |
Output is correct |
5 |
Correct |
13 ms |
21460 KB |
Output is correct |
6 |
Correct |
89 ms |
29272 KB |
Output is correct |
7 |
Correct |
97 ms |
29260 KB |
Output is correct |
8 |
Correct |
74 ms |
29184 KB |
Output is correct |
9 |
Correct |
80 ms |
29132 KB |
Output is correct |
10 |
Correct |
94 ms |
29260 KB |
Output is correct |
11 |
Correct |
84 ms |
29156 KB |
Output is correct |
12 |
Correct |
85 ms |
29196 KB |
Output is correct |
13 |
Correct |
24 ms |
22996 KB |
Output is correct |
14 |
Correct |
87 ms |
29812 KB |
Output is correct |
15 |
Correct |
78 ms |
29272 KB |
Output is correct |
16 |
Correct |
124 ms |
29340 KB |
Output is correct |
17 |
Correct |
13 ms |
21540 KB |
Output is correct |
18 |
Correct |
80 ms |
29248 KB |
Output is correct |
19 |
Correct |
99 ms |
29156 KB |
Output is correct |
20 |
Correct |
83 ms |
29192 KB |
Output is correct |
21 |
Correct |
82 ms |
29132 KB |
Output is correct |
22 |
Correct |
84 ms |
29264 KB |
Output is correct |
23 |
Correct |
74 ms |
29152 KB |
Output is correct |
24 |
Correct |
94 ms |
29132 KB |
Output is correct |
25 |
Correct |
1761 ms |
110620 KB |
Output is correct |
26 |
Correct |
1638 ms |
114272 KB |
Output is correct |
27 |
Correct |
1674 ms |
110060 KB |
Output is correct |
28 |
Correct |
1170 ms |
114024 KB |
Output is correct |
29 |
Execution timed out |
6022 ms |
107260 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23008 KB |
Output is correct |
2 |
Correct |
82 ms |
29820 KB |
Output is correct |
3 |
Correct |
74 ms |
29232 KB |
Output is correct |
4 |
Correct |
108 ms |
29260 KB |
Output is correct |
5 |
Correct |
13 ms |
21460 KB |
Output is correct |
6 |
Correct |
89 ms |
29272 KB |
Output is correct |
7 |
Correct |
97 ms |
29260 KB |
Output is correct |
8 |
Correct |
74 ms |
29184 KB |
Output is correct |
9 |
Correct |
80 ms |
29132 KB |
Output is correct |
10 |
Correct |
94 ms |
29260 KB |
Output is correct |
11 |
Correct |
84 ms |
29156 KB |
Output is correct |
12 |
Correct |
85 ms |
29196 KB |
Output is correct |
13 |
Correct |
24 ms |
22996 KB |
Output is correct |
14 |
Correct |
87 ms |
29812 KB |
Output is correct |
15 |
Correct |
78 ms |
29272 KB |
Output is correct |
16 |
Correct |
124 ms |
29340 KB |
Output is correct |
17 |
Correct |
13 ms |
21540 KB |
Output is correct |
18 |
Correct |
80 ms |
29248 KB |
Output is correct |
19 |
Correct |
99 ms |
29156 KB |
Output is correct |
20 |
Correct |
83 ms |
29192 KB |
Output is correct |
21 |
Correct |
82 ms |
29132 KB |
Output is correct |
22 |
Correct |
84 ms |
29264 KB |
Output is correct |
23 |
Correct |
74 ms |
29152 KB |
Output is correct |
24 |
Correct |
94 ms |
29132 KB |
Output is correct |
25 |
Correct |
1761 ms |
110620 KB |
Output is correct |
26 |
Correct |
1638 ms |
114272 KB |
Output is correct |
27 |
Correct |
1674 ms |
110060 KB |
Output is correct |
28 |
Correct |
1170 ms |
114024 KB |
Output is correct |
29 |
Execution timed out |
6022 ms |
107260 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
22988 KB |
Output is correct |
2 |
Correct |
100 ms |
29748 KB |
Output is correct |
3 |
Correct |
96 ms |
29196 KB |
Output is correct |
4 |
Correct |
100 ms |
29284 KB |
Output is correct |
5 |
Correct |
12 ms |
21552 KB |
Output is correct |
6 |
Correct |
83 ms |
29236 KB |
Output is correct |
7 |
Correct |
23 ms |
23008 KB |
Output is correct |
8 |
Correct |
82 ms |
29820 KB |
Output is correct |
9 |
Correct |
74 ms |
29232 KB |
Output is correct |
10 |
Correct |
108 ms |
29260 KB |
Output is correct |
11 |
Correct |
13 ms |
21460 KB |
Output is correct |
12 |
Correct |
89 ms |
29272 KB |
Output is correct |
13 |
Correct |
97 ms |
29260 KB |
Output is correct |
14 |
Correct |
74 ms |
29184 KB |
Output is correct |
15 |
Correct |
80 ms |
29132 KB |
Output is correct |
16 |
Correct |
94 ms |
29260 KB |
Output is correct |
17 |
Correct |
84 ms |
29156 KB |
Output is correct |
18 |
Correct |
85 ms |
29196 KB |
Output is correct |
19 |
Correct |
11 ms |
21460 KB |
Output is correct |
20 |
Correct |
11 ms |
21460 KB |
Output is correct |
21 |
Correct |
12 ms |
21460 KB |
Output is correct |
22 |
Correct |
21 ms |
22944 KB |
Output is correct |
23 |
Correct |
97 ms |
29824 KB |
Output is correct |
24 |
Correct |
78 ms |
29160 KB |
Output is correct |
25 |
Correct |
113 ms |
29236 KB |
Output is correct |
26 |
Correct |
12 ms |
21548 KB |
Output is correct |
27 |
Correct |
81 ms |
29244 KB |
Output is correct |
28 |
Correct |
70 ms |
29188 KB |
Output is correct |
29 |
Correct |
85 ms |
29216 KB |
Output is correct |
30 |
Correct |
98 ms |
29128 KB |
Output is correct |
31 |
Correct |
82 ms |
29260 KB |
Output is correct |
32 |
Correct |
73 ms |
29252 KB |
Output is correct |
33 |
Correct |
76 ms |
29160 KB |
Output is correct |
34 |
Correct |
1797 ms |
111056 KB |
Output is correct |
35 |
Correct |
1842 ms |
107812 KB |
Output is correct |
36 |
Correct |
1911 ms |
107784 KB |
Output is correct |
37 |
Incorrect |
1544 ms |
112644 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
22988 KB |
Output is correct |
2 |
Correct |
100 ms |
29748 KB |
Output is correct |
3 |
Correct |
96 ms |
29196 KB |
Output is correct |
4 |
Correct |
100 ms |
29284 KB |
Output is correct |
5 |
Correct |
12 ms |
21552 KB |
Output is correct |
6 |
Correct |
83 ms |
29236 KB |
Output is correct |
7 |
Correct |
23 ms |
23008 KB |
Output is correct |
8 |
Correct |
82 ms |
29820 KB |
Output is correct |
9 |
Correct |
74 ms |
29232 KB |
Output is correct |
10 |
Correct |
108 ms |
29260 KB |
Output is correct |
11 |
Correct |
13 ms |
21460 KB |
Output is correct |
12 |
Correct |
89 ms |
29272 KB |
Output is correct |
13 |
Correct |
97 ms |
29260 KB |
Output is correct |
14 |
Correct |
74 ms |
29184 KB |
Output is correct |
15 |
Correct |
80 ms |
29132 KB |
Output is correct |
16 |
Correct |
94 ms |
29260 KB |
Output is correct |
17 |
Correct |
84 ms |
29156 KB |
Output is correct |
18 |
Correct |
85 ms |
29196 KB |
Output is correct |
19 |
Correct |
24 ms |
22996 KB |
Output is correct |
20 |
Correct |
87 ms |
29812 KB |
Output is correct |
21 |
Correct |
78 ms |
29272 KB |
Output is correct |
22 |
Correct |
124 ms |
29340 KB |
Output is correct |
23 |
Correct |
13 ms |
21540 KB |
Output is correct |
24 |
Correct |
80 ms |
29248 KB |
Output is correct |
25 |
Correct |
99 ms |
29156 KB |
Output is correct |
26 |
Correct |
83 ms |
29192 KB |
Output is correct |
27 |
Correct |
82 ms |
29132 KB |
Output is correct |
28 |
Correct |
84 ms |
29264 KB |
Output is correct |
29 |
Correct |
74 ms |
29152 KB |
Output is correct |
30 |
Correct |
94 ms |
29132 KB |
Output is correct |
31 |
Correct |
1761 ms |
110620 KB |
Output is correct |
32 |
Correct |
1638 ms |
114272 KB |
Output is correct |
33 |
Correct |
1674 ms |
110060 KB |
Output is correct |
34 |
Correct |
1170 ms |
114024 KB |
Output is correct |
35 |
Execution timed out |
6022 ms |
107260 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
22988 KB |
Output is correct |
2 |
Correct |
100 ms |
29748 KB |
Output is correct |
3 |
Correct |
96 ms |
29196 KB |
Output is correct |
4 |
Correct |
100 ms |
29284 KB |
Output is correct |
5 |
Correct |
12 ms |
21552 KB |
Output is correct |
6 |
Correct |
83 ms |
29236 KB |
Output is correct |
7 |
Correct |
23 ms |
23008 KB |
Output is correct |
8 |
Correct |
82 ms |
29820 KB |
Output is correct |
9 |
Correct |
74 ms |
29232 KB |
Output is correct |
10 |
Correct |
108 ms |
29260 KB |
Output is correct |
11 |
Correct |
13 ms |
21460 KB |
Output is correct |
12 |
Correct |
89 ms |
29272 KB |
Output is correct |
13 |
Correct |
97 ms |
29260 KB |
Output is correct |
14 |
Correct |
74 ms |
29184 KB |
Output is correct |
15 |
Correct |
80 ms |
29132 KB |
Output is correct |
16 |
Correct |
94 ms |
29260 KB |
Output is correct |
17 |
Correct |
84 ms |
29156 KB |
Output is correct |
18 |
Correct |
85 ms |
29196 KB |
Output is correct |
19 |
Correct |
24 ms |
22996 KB |
Output is correct |
20 |
Correct |
87 ms |
29812 KB |
Output is correct |
21 |
Correct |
78 ms |
29272 KB |
Output is correct |
22 |
Correct |
124 ms |
29340 KB |
Output is correct |
23 |
Correct |
13 ms |
21540 KB |
Output is correct |
24 |
Correct |
80 ms |
29248 KB |
Output is correct |
25 |
Correct |
99 ms |
29156 KB |
Output is correct |
26 |
Correct |
83 ms |
29192 KB |
Output is correct |
27 |
Correct |
82 ms |
29132 KB |
Output is correct |
28 |
Correct |
84 ms |
29264 KB |
Output is correct |
29 |
Correct |
74 ms |
29152 KB |
Output is correct |
30 |
Correct |
94 ms |
29132 KB |
Output is correct |
31 |
Correct |
1761 ms |
110620 KB |
Output is correct |
32 |
Correct |
1638 ms |
114272 KB |
Output is correct |
33 |
Correct |
1674 ms |
110060 KB |
Output is correct |
34 |
Correct |
1170 ms |
114024 KB |
Output is correct |
35 |
Execution timed out |
6022 ms |
107260 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |