#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
typedef long long lint;
typedef pair<lint, lint> pi;
const int mod = 1e9 + 7;
const int MAXN = 250005;
const int MAXL = 2755;
vector<lint> dist[MAXN];
vector<int> gph[MAXN];
vector<int> ngph[MAXL];
lint eist[MAXL][MAXL];
struct node{
int x, y; lint dist;
bool mode;
bool operator<(const node &n)const{
return dist > n.dist;
}
};
void solve(int n, vector<pi> edg, vector<int> vect, int s, int e){
memset(eist, 0x3f, sizeof(eist));
vector<int> sum = {0};
for(auto &i : vect){
int nxt = i + sum.back(); sum.push_back(nxt);
}
int p = 0;
vector<pi> cycs;
for(int i = 0; i < sz(vect); i++){
for(int j = 0; j < vect[i]; j++){
dist[p].resize(vect[i], 1e18);
p++;
cycs.emplace_back(i, j);
}
}
while(p < n){
dist[p++].resize(1, 1e18);
cycs.emplace_back(-1, 0);
}
for(auto &[u, v] : edg){
if(cycs[u].first >= 0 && cycs[v].first >= 0){
ngph[u].push_back(v);
ngph[v].push_back(u);
}
else{
gph[u].push_back(v);
gph[v].push_back(u);
}
}
for(int i = 0; i < n; i++){
sort(all(gph[i]));
sort(all(ngph[i]));
}
priority_queue<node> pq;
auto enq = [&](int v, lint d){
if(sz(dist[v]) > 1 && d % sz(dist[v]) == cycs[v].second) return;
if(dist[v][d % sz(dist[v])] > d){
dist[v][d % sz(dist[v])] = d;
pq.push({v, (int)(d % sz(dist[v])), d, 0});
}
};
auto enq2 = [&](int x, int y, lint d){
if(eist[x][y] > d){
eist[x][y] = d;
pq.push({x, y, d, 1});
}
};
memset(eist, 0x3f, sizeof(eist));
enq(s, 0);
vector<int> visCount(n);
while(sz(pq)){
auto x = pq.top(); pq.pop();
int v = x.x;
int w = x.y;
if(x.mode == 0){
if(dist[v][w] != x.dist) continue;
enq(v, x.dist + 1);
visCount[v]++;
for(auto &y : gph[v]){
if(sz(dist[v]) > 1 && visCount[v] > 1 && sz(dist[y]) == 1) break;
enq(y, x.dist + 1);
if(sz(dist[v]) == 1 && sz(dist[y]) > 1){
enq(y, x.dist + 1 + ((cycs[y].second - x.dist) % sz(dist[y]) + sz(dist[y])) % sz(dist[y]));
}
}
if(sz(dist[v]) > 1){
for(int i = 0; i < sz(vect); i++){
lint newDist = x.dist;
enq2(v, sum[i] + newDist % vect[i], newDist);
}
}
}
else{
if(eist[v][w] != x.dist) continue;
int pos = cycs[v].first;
enq2(v, sum[pos] + (x.dist + sz(dist[v])) % vect[pos], x.dist + sz(dist[v]));
int s = lower_bound(all(ngph[v]), w - cycs[w].second) - ngph[v].begin();
for(int i = s; i < sz(ngph[v]); i++){
int y = ngph[v][i];
if(cycs[y].first != cycs[w].first) break;
if(cycs[v].first >= 0 &&
cycs[v].first == cycs[y].first &&
x.dist % sz(dist[v]) == cycs[y].second &&
(x.dist + 1) % sz(dist[v]) == cycs[v].second){
continue;
}
enq(y, x.dist + 1);
}
}
}
lint ans = *min_element(all(dist[e]));
if(ans < 1e17) cout << ans << "\n";
else cout << "impossible\n";
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m; cin >> n >> m;
vector<pi> edg;
for(int i = 0; i < m; i++){
int u, v; cin >> u >> v;
edg.emplace_back(u-1, v-1);
}
vector<int> idx(n, -1);
int cnt = 0;
int k; cin >> k;
vector<int> siz(k);
for(int i = 0; i < k; i++){
cin >> siz[i];
for(int j = 0; j < siz[i]; j++){
int v; cin >> v;
idx[v - 1] = cnt++;
}
}
for(int i = 0; i < n; i++){
if(idx[i] == -1) idx[i] = cnt++;
}
for(auto &[u, v] : edg){
u = idx[u];
v = idx[v];
}
solve(n, edg, siz, idx[0], idx[n - 1]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
75796 KB |
Output is correct |
2 |
Correct |
135 ms |
84024 KB |
Output is correct |
3 |
Correct |
116 ms |
83632 KB |
Output is correct |
4 |
Correct |
136 ms |
83544 KB |
Output is correct |
5 |
Correct |
38 ms |
71600 KB |
Output is correct |
6 |
Correct |
119 ms |
83628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
75748 KB |
Output is correct |
2 |
Correct |
140 ms |
84100 KB |
Output is correct |
3 |
Correct |
109 ms |
83636 KB |
Output is correct |
4 |
Correct |
139 ms |
83672 KB |
Output is correct |
5 |
Correct |
39 ms |
71600 KB |
Output is correct |
6 |
Correct |
137 ms |
83616 KB |
Output is correct |
7 |
Correct |
114 ms |
83520 KB |
Output is correct |
8 |
Correct |
112 ms |
83568 KB |
Output is correct |
9 |
Correct |
108 ms |
83304 KB |
Output is correct |
10 |
Correct |
117 ms |
83768 KB |
Output is correct |
11 |
Correct |
117 ms |
83456 KB |
Output is correct |
12 |
Correct |
114 ms |
83476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
75748 KB |
Output is correct |
2 |
Correct |
140 ms |
84100 KB |
Output is correct |
3 |
Correct |
109 ms |
83636 KB |
Output is correct |
4 |
Correct |
139 ms |
83672 KB |
Output is correct |
5 |
Correct |
39 ms |
71600 KB |
Output is correct |
6 |
Correct |
137 ms |
83616 KB |
Output is correct |
7 |
Correct |
114 ms |
83520 KB |
Output is correct |
8 |
Correct |
112 ms |
83568 KB |
Output is correct |
9 |
Correct |
108 ms |
83304 KB |
Output is correct |
10 |
Correct |
117 ms |
83768 KB |
Output is correct |
11 |
Correct |
117 ms |
83456 KB |
Output is correct |
12 |
Correct |
114 ms |
83476 KB |
Output is correct |
13 |
Correct |
83 ms |
75760 KB |
Output is correct |
14 |
Correct |
126 ms |
84056 KB |
Output is correct |
15 |
Correct |
116 ms |
83512 KB |
Output is correct |
16 |
Correct |
125 ms |
83584 KB |
Output is correct |
17 |
Correct |
41 ms |
71652 KB |
Output is correct |
18 |
Correct |
118 ms |
83512 KB |
Output is correct |
19 |
Correct |
104 ms |
83528 KB |
Output is correct |
20 |
Correct |
105 ms |
83504 KB |
Output is correct |
21 |
Correct |
122 ms |
83364 KB |
Output is correct |
22 |
Correct |
129 ms |
83784 KB |
Output is correct |
23 |
Correct |
104 ms |
83556 KB |
Output is correct |
24 |
Correct |
111 ms |
83500 KB |
Output is correct |
25 |
Correct |
1660 ms |
226168 KB |
Output is correct |
26 |
Correct |
1534 ms |
231828 KB |
Output is correct |
27 |
Correct |
1461 ms |
224284 KB |
Output is correct |
28 |
Correct |
1329 ms |
228440 KB |
Output is correct |
29 |
Correct |
1706 ms |
221768 KB |
Output is correct |
30 |
Correct |
1721 ms |
223864 KB |
Output is correct |
31 |
Correct |
1734 ms |
234136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
75748 KB |
Output is correct |
2 |
Correct |
140 ms |
84100 KB |
Output is correct |
3 |
Correct |
109 ms |
83636 KB |
Output is correct |
4 |
Correct |
139 ms |
83672 KB |
Output is correct |
5 |
Correct |
39 ms |
71600 KB |
Output is correct |
6 |
Correct |
137 ms |
83616 KB |
Output is correct |
7 |
Correct |
114 ms |
83520 KB |
Output is correct |
8 |
Correct |
112 ms |
83568 KB |
Output is correct |
9 |
Correct |
108 ms |
83304 KB |
Output is correct |
10 |
Correct |
117 ms |
83768 KB |
Output is correct |
11 |
Correct |
117 ms |
83456 KB |
Output is correct |
12 |
Correct |
114 ms |
83476 KB |
Output is correct |
13 |
Correct |
83 ms |
75760 KB |
Output is correct |
14 |
Correct |
126 ms |
84056 KB |
Output is correct |
15 |
Correct |
116 ms |
83512 KB |
Output is correct |
16 |
Correct |
125 ms |
83584 KB |
Output is correct |
17 |
Correct |
41 ms |
71652 KB |
Output is correct |
18 |
Correct |
118 ms |
83512 KB |
Output is correct |
19 |
Correct |
104 ms |
83528 KB |
Output is correct |
20 |
Correct |
105 ms |
83504 KB |
Output is correct |
21 |
Correct |
122 ms |
83364 KB |
Output is correct |
22 |
Correct |
129 ms |
83784 KB |
Output is correct |
23 |
Correct |
104 ms |
83556 KB |
Output is correct |
24 |
Correct |
111 ms |
83500 KB |
Output is correct |
25 |
Correct |
1660 ms |
226168 KB |
Output is correct |
26 |
Correct |
1534 ms |
231828 KB |
Output is correct |
27 |
Correct |
1461 ms |
224284 KB |
Output is correct |
28 |
Correct |
1329 ms |
228440 KB |
Output is correct |
29 |
Correct |
1706 ms |
221768 KB |
Output is correct |
30 |
Correct |
1721 ms |
223864 KB |
Output is correct |
31 |
Correct |
1734 ms |
234136 KB |
Output is correct |
32 |
Correct |
78 ms |
75704 KB |
Output is correct |
33 |
Correct |
129 ms |
84096 KB |
Output is correct |
34 |
Correct |
109 ms |
83548 KB |
Output is correct |
35 |
Correct |
126 ms |
83512 KB |
Output is correct |
36 |
Correct |
36 ms |
71608 KB |
Output is correct |
37 |
Correct |
106 ms |
83556 KB |
Output is correct |
38 |
Correct |
121 ms |
83548 KB |
Output is correct |
39 |
Correct |
120 ms |
83384 KB |
Output is correct |
40 |
Correct |
106 ms |
83316 KB |
Output is correct |
41 |
Correct |
113 ms |
83828 KB |
Output is correct |
42 |
Correct |
134 ms |
83416 KB |
Output is correct |
43 |
Correct |
111 ms |
83548 KB |
Output is correct |
44 |
Correct |
1554 ms |
226180 KB |
Output is correct |
45 |
Correct |
1518 ms |
231652 KB |
Output is correct |
46 |
Correct |
1511 ms |
224468 KB |
Output is correct |
47 |
Correct |
1327 ms |
228344 KB |
Output is correct |
48 |
Correct |
1686 ms |
221812 KB |
Output is correct |
49 |
Correct |
1692 ms |
224600 KB |
Output is correct |
50 |
Correct |
1717 ms |
233772 KB |
Output is correct |
51 |
Correct |
2196 ms |
243244 KB |
Output is correct |
52 |
Correct |
2883 ms |
260392 KB |
Output is correct |
53 |
Correct |
2642 ms |
242328 KB |
Output is correct |
54 |
Correct |
1839 ms |
217640 KB |
Output is correct |
55 |
Execution timed out |
6102 ms |
246460 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
75796 KB |
Output is correct |
2 |
Correct |
135 ms |
84024 KB |
Output is correct |
3 |
Correct |
116 ms |
83632 KB |
Output is correct |
4 |
Correct |
136 ms |
83544 KB |
Output is correct |
5 |
Correct |
38 ms |
71600 KB |
Output is correct |
6 |
Correct |
119 ms |
83628 KB |
Output is correct |
7 |
Correct |
87 ms |
75748 KB |
Output is correct |
8 |
Correct |
140 ms |
84100 KB |
Output is correct |
9 |
Correct |
109 ms |
83636 KB |
Output is correct |
10 |
Correct |
139 ms |
83672 KB |
Output is correct |
11 |
Correct |
39 ms |
71600 KB |
Output is correct |
12 |
Correct |
137 ms |
83616 KB |
Output is correct |
13 |
Correct |
114 ms |
83520 KB |
Output is correct |
14 |
Correct |
112 ms |
83568 KB |
Output is correct |
15 |
Correct |
108 ms |
83304 KB |
Output is correct |
16 |
Correct |
117 ms |
83768 KB |
Output is correct |
17 |
Correct |
117 ms |
83456 KB |
Output is correct |
18 |
Correct |
114 ms |
83476 KB |
Output is correct |
19 |
Correct |
34 ms |
71500 KB |
Output is correct |
20 |
Correct |
35 ms |
71500 KB |
Output is correct |
21 |
Correct |
35 ms |
71436 KB |
Output is correct |
22 |
Correct |
73 ms |
75132 KB |
Output is correct |
23 |
Correct |
116 ms |
83512 KB |
Output is correct |
24 |
Correct |
107 ms |
82872 KB |
Output is correct |
25 |
Correct |
125 ms |
82868 KB |
Output is correct |
26 |
Correct |
40 ms |
71656 KB |
Output is correct |
27 |
Correct |
107 ms |
82864 KB |
Output is correct |
28 |
Correct |
103 ms |
82756 KB |
Output is correct |
29 |
Correct |
104 ms |
82800 KB |
Output is correct |
30 |
Correct |
103 ms |
82744 KB |
Output is correct |
31 |
Correct |
109 ms |
83108 KB |
Output is correct |
32 |
Correct |
105 ms |
82780 KB |
Output is correct |
33 |
Correct |
106 ms |
82940 KB |
Output is correct |
34 |
Correct |
1539 ms |
227600 KB |
Output is correct |
35 |
Correct |
1597 ms |
224412 KB |
Output is correct |
36 |
Correct |
1583 ms |
224268 KB |
Output is correct |
37 |
Incorrect |
1399 ms |
228732 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
75796 KB |
Output is correct |
2 |
Correct |
135 ms |
84024 KB |
Output is correct |
3 |
Correct |
116 ms |
83632 KB |
Output is correct |
4 |
Correct |
136 ms |
83544 KB |
Output is correct |
5 |
Correct |
38 ms |
71600 KB |
Output is correct |
6 |
Correct |
119 ms |
83628 KB |
Output is correct |
7 |
Correct |
87 ms |
75748 KB |
Output is correct |
8 |
Correct |
140 ms |
84100 KB |
Output is correct |
9 |
Correct |
109 ms |
83636 KB |
Output is correct |
10 |
Correct |
139 ms |
83672 KB |
Output is correct |
11 |
Correct |
39 ms |
71600 KB |
Output is correct |
12 |
Correct |
137 ms |
83616 KB |
Output is correct |
13 |
Correct |
114 ms |
83520 KB |
Output is correct |
14 |
Correct |
112 ms |
83568 KB |
Output is correct |
15 |
Correct |
108 ms |
83304 KB |
Output is correct |
16 |
Correct |
117 ms |
83768 KB |
Output is correct |
17 |
Correct |
117 ms |
83456 KB |
Output is correct |
18 |
Correct |
114 ms |
83476 KB |
Output is correct |
19 |
Correct |
83 ms |
75760 KB |
Output is correct |
20 |
Correct |
126 ms |
84056 KB |
Output is correct |
21 |
Correct |
116 ms |
83512 KB |
Output is correct |
22 |
Correct |
125 ms |
83584 KB |
Output is correct |
23 |
Correct |
41 ms |
71652 KB |
Output is correct |
24 |
Correct |
118 ms |
83512 KB |
Output is correct |
25 |
Correct |
104 ms |
83528 KB |
Output is correct |
26 |
Correct |
105 ms |
83504 KB |
Output is correct |
27 |
Correct |
122 ms |
83364 KB |
Output is correct |
28 |
Correct |
129 ms |
83784 KB |
Output is correct |
29 |
Correct |
104 ms |
83556 KB |
Output is correct |
30 |
Correct |
111 ms |
83500 KB |
Output is correct |
31 |
Correct |
1660 ms |
226168 KB |
Output is correct |
32 |
Correct |
1534 ms |
231828 KB |
Output is correct |
33 |
Correct |
1461 ms |
224284 KB |
Output is correct |
34 |
Correct |
1329 ms |
228440 KB |
Output is correct |
35 |
Correct |
1706 ms |
221768 KB |
Output is correct |
36 |
Correct |
1721 ms |
223864 KB |
Output is correct |
37 |
Correct |
1734 ms |
234136 KB |
Output is correct |
38 |
Correct |
34 ms |
71500 KB |
Output is correct |
39 |
Correct |
35 ms |
71500 KB |
Output is correct |
40 |
Correct |
35 ms |
71436 KB |
Output is correct |
41 |
Correct |
73 ms |
75132 KB |
Output is correct |
42 |
Correct |
116 ms |
83512 KB |
Output is correct |
43 |
Correct |
107 ms |
82872 KB |
Output is correct |
44 |
Correct |
125 ms |
82868 KB |
Output is correct |
45 |
Correct |
40 ms |
71656 KB |
Output is correct |
46 |
Correct |
107 ms |
82864 KB |
Output is correct |
47 |
Correct |
103 ms |
82756 KB |
Output is correct |
48 |
Correct |
104 ms |
82800 KB |
Output is correct |
49 |
Correct |
103 ms |
82744 KB |
Output is correct |
50 |
Correct |
109 ms |
83108 KB |
Output is correct |
51 |
Correct |
105 ms |
82780 KB |
Output is correct |
52 |
Correct |
106 ms |
82940 KB |
Output is correct |
53 |
Correct |
1539 ms |
227600 KB |
Output is correct |
54 |
Correct |
1597 ms |
224412 KB |
Output is correct |
55 |
Correct |
1583 ms |
224268 KB |
Output is correct |
56 |
Incorrect |
1399 ms |
228732 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
75796 KB |
Output is correct |
2 |
Correct |
135 ms |
84024 KB |
Output is correct |
3 |
Correct |
116 ms |
83632 KB |
Output is correct |
4 |
Correct |
136 ms |
83544 KB |
Output is correct |
5 |
Correct |
38 ms |
71600 KB |
Output is correct |
6 |
Correct |
119 ms |
83628 KB |
Output is correct |
7 |
Correct |
87 ms |
75748 KB |
Output is correct |
8 |
Correct |
140 ms |
84100 KB |
Output is correct |
9 |
Correct |
109 ms |
83636 KB |
Output is correct |
10 |
Correct |
139 ms |
83672 KB |
Output is correct |
11 |
Correct |
39 ms |
71600 KB |
Output is correct |
12 |
Correct |
137 ms |
83616 KB |
Output is correct |
13 |
Correct |
114 ms |
83520 KB |
Output is correct |
14 |
Correct |
112 ms |
83568 KB |
Output is correct |
15 |
Correct |
108 ms |
83304 KB |
Output is correct |
16 |
Correct |
117 ms |
83768 KB |
Output is correct |
17 |
Correct |
117 ms |
83456 KB |
Output is correct |
18 |
Correct |
114 ms |
83476 KB |
Output is correct |
19 |
Correct |
83 ms |
75760 KB |
Output is correct |
20 |
Correct |
126 ms |
84056 KB |
Output is correct |
21 |
Correct |
116 ms |
83512 KB |
Output is correct |
22 |
Correct |
125 ms |
83584 KB |
Output is correct |
23 |
Correct |
41 ms |
71652 KB |
Output is correct |
24 |
Correct |
118 ms |
83512 KB |
Output is correct |
25 |
Correct |
104 ms |
83528 KB |
Output is correct |
26 |
Correct |
105 ms |
83504 KB |
Output is correct |
27 |
Correct |
122 ms |
83364 KB |
Output is correct |
28 |
Correct |
129 ms |
83784 KB |
Output is correct |
29 |
Correct |
104 ms |
83556 KB |
Output is correct |
30 |
Correct |
111 ms |
83500 KB |
Output is correct |
31 |
Correct |
1660 ms |
226168 KB |
Output is correct |
32 |
Correct |
1534 ms |
231828 KB |
Output is correct |
33 |
Correct |
1461 ms |
224284 KB |
Output is correct |
34 |
Correct |
1329 ms |
228440 KB |
Output is correct |
35 |
Correct |
1706 ms |
221768 KB |
Output is correct |
36 |
Correct |
1721 ms |
223864 KB |
Output is correct |
37 |
Correct |
1734 ms |
234136 KB |
Output is correct |
38 |
Correct |
78 ms |
75704 KB |
Output is correct |
39 |
Correct |
129 ms |
84096 KB |
Output is correct |
40 |
Correct |
109 ms |
83548 KB |
Output is correct |
41 |
Correct |
126 ms |
83512 KB |
Output is correct |
42 |
Correct |
36 ms |
71608 KB |
Output is correct |
43 |
Correct |
106 ms |
83556 KB |
Output is correct |
44 |
Correct |
121 ms |
83548 KB |
Output is correct |
45 |
Correct |
120 ms |
83384 KB |
Output is correct |
46 |
Correct |
106 ms |
83316 KB |
Output is correct |
47 |
Correct |
113 ms |
83828 KB |
Output is correct |
48 |
Correct |
134 ms |
83416 KB |
Output is correct |
49 |
Correct |
111 ms |
83548 KB |
Output is correct |
50 |
Correct |
1554 ms |
226180 KB |
Output is correct |
51 |
Correct |
1518 ms |
231652 KB |
Output is correct |
52 |
Correct |
1511 ms |
224468 KB |
Output is correct |
53 |
Correct |
1327 ms |
228344 KB |
Output is correct |
54 |
Correct |
1686 ms |
221812 KB |
Output is correct |
55 |
Correct |
1692 ms |
224600 KB |
Output is correct |
56 |
Correct |
1717 ms |
233772 KB |
Output is correct |
57 |
Correct |
2196 ms |
243244 KB |
Output is correct |
58 |
Correct |
2883 ms |
260392 KB |
Output is correct |
59 |
Correct |
2642 ms |
242328 KB |
Output is correct |
60 |
Correct |
1839 ms |
217640 KB |
Output is correct |
61 |
Execution timed out |
6102 ms |
246460 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |