#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));
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){
int sum = 0;
for(int i = 0; i < sz(vect); i++){
lint newDist = x.dist;
enq2(v, sum + newDist % vect[i], newDist);
sum += vect[i];
}
}
}
else{
if(eist[v][w] != x.dist) continue;
enq2(v, (x.dist + sz(dist[v])) % vect[cycs[v].first], 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]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
75192 KB |
Output is correct |
2 |
Correct |
115 ms |
83420 KB |
Output is correct |
3 |
Correct |
105 ms |
82848 KB |
Output is correct |
4 |
Correct |
120 ms |
82868 KB |
Output is correct |
5 |
Correct |
37 ms |
71664 KB |
Output is correct |
6 |
Correct |
106 ms |
82872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
75176 KB |
Output is correct |
2 |
Correct |
118 ms |
83588 KB |
Output is correct |
3 |
Correct |
111 ms |
82796 KB |
Output is correct |
4 |
Correct |
121 ms |
82892 KB |
Output is correct |
5 |
Correct |
37 ms |
71652 KB |
Output is correct |
6 |
Correct |
105 ms |
82788 KB |
Output is correct |
7 |
Correct |
110 ms |
82772 KB |
Output is correct |
8 |
Correct |
107 ms |
82800 KB |
Output is correct |
9 |
Correct |
98 ms |
82616 KB |
Output is correct |
10 |
Correct |
122 ms |
83076 KB |
Output is correct |
11 |
Correct |
101 ms |
82832 KB |
Output is correct |
12 |
Correct |
108 ms |
82780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
75176 KB |
Output is correct |
2 |
Correct |
118 ms |
83588 KB |
Output is correct |
3 |
Correct |
111 ms |
82796 KB |
Output is correct |
4 |
Correct |
121 ms |
82892 KB |
Output is correct |
5 |
Correct |
37 ms |
71652 KB |
Output is correct |
6 |
Correct |
105 ms |
82788 KB |
Output is correct |
7 |
Correct |
110 ms |
82772 KB |
Output is correct |
8 |
Correct |
107 ms |
82800 KB |
Output is correct |
9 |
Correct |
98 ms |
82616 KB |
Output is correct |
10 |
Correct |
122 ms |
83076 KB |
Output is correct |
11 |
Correct |
101 ms |
82832 KB |
Output is correct |
12 |
Correct |
108 ms |
82780 KB |
Output is correct |
13 |
Correct |
74 ms |
75176 KB |
Output is correct |
14 |
Correct |
116 ms |
83352 KB |
Output is correct |
15 |
Correct |
106 ms |
82892 KB |
Output is correct |
16 |
Correct |
121 ms |
82936 KB |
Output is correct |
17 |
Correct |
36 ms |
71644 KB |
Output is correct |
18 |
Correct |
109 ms |
82900 KB |
Output is correct |
19 |
Correct |
122 ms |
82928 KB |
Output is correct |
20 |
Correct |
104 ms |
82720 KB |
Output is correct |
21 |
Correct |
102 ms |
82592 KB |
Output is correct |
22 |
Correct |
107 ms |
83044 KB |
Output is correct |
23 |
Correct |
103 ms |
82748 KB |
Output is correct |
24 |
Correct |
109 ms |
82760 KB |
Output is correct |
25 |
Correct |
1526 ms |
225432 KB |
Output is correct |
26 |
Correct |
1523 ms |
231512 KB |
Output is correct |
27 |
Correct |
1409 ms |
223892 KB |
Output is correct |
28 |
Correct |
1279 ms |
227968 KB |
Output is correct |
29 |
Correct |
1616 ms |
221392 KB |
Output is correct |
30 |
Correct |
1628 ms |
223896 KB |
Output is correct |
31 |
Correct |
1616 ms |
233552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
75176 KB |
Output is correct |
2 |
Correct |
118 ms |
83588 KB |
Output is correct |
3 |
Correct |
111 ms |
82796 KB |
Output is correct |
4 |
Correct |
121 ms |
82892 KB |
Output is correct |
5 |
Correct |
37 ms |
71652 KB |
Output is correct |
6 |
Correct |
105 ms |
82788 KB |
Output is correct |
7 |
Correct |
110 ms |
82772 KB |
Output is correct |
8 |
Correct |
107 ms |
82800 KB |
Output is correct |
9 |
Correct |
98 ms |
82616 KB |
Output is correct |
10 |
Correct |
122 ms |
83076 KB |
Output is correct |
11 |
Correct |
101 ms |
82832 KB |
Output is correct |
12 |
Correct |
108 ms |
82780 KB |
Output is correct |
13 |
Correct |
74 ms |
75176 KB |
Output is correct |
14 |
Correct |
116 ms |
83352 KB |
Output is correct |
15 |
Correct |
106 ms |
82892 KB |
Output is correct |
16 |
Correct |
121 ms |
82936 KB |
Output is correct |
17 |
Correct |
36 ms |
71644 KB |
Output is correct |
18 |
Correct |
109 ms |
82900 KB |
Output is correct |
19 |
Correct |
122 ms |
82928 KB |
Output is correct |
20 |
Correct |
104 ms |
82720 KB |
Output is correct |
21 |
Correct |
102 ms |
82592 KB |
Output is correct |
22 |
Correct |
107 ms |
83044 KB |
Output is correct |
23 |
Correct |
103 ms |
82748 KB |
Output is correct |
24 |
Correct |
109 ms |
82760 KB |
Output is correct |
25 |
Correct |
1526 ms |
225432 KB |
Output is correct |
26 |
Correct |
1523 ms |
231512 KB |
Output is correct |
27 |
Correct |
1409 ms |
223892 KB |
Output is correct |
28 |
Correct |
1279 ms |
227968 KB |
Output is correct |
29 |
Correct |
1616 ms |
221392 KB |
Output is correct |
30 |
Correct |
1628 ms |
223896 KB |
Output is correct |
31 |
Correct |
1616 ms |
233552 KB |
Output is correct |
32 |
Correct |
75 ms |
75388 KB |
Output is correct |
33 |
Correct |
113 ms |
83508 KB |
Output is correct |
34 |
Correct |
107 ms |
83032 KB |
Output is correct |
35 |
Correct |
145 ms |
83256 KB |
Output is correct |
36 |
Correct |
40 ms |
71560 KB |
Output is correct |
37 |
Correct |
106 ms |
83052 KB |
Output is correct |
38 |
Correct |
105 ms |
83012 KB |
Output is correct |
39 |
Correct |
108 ms |
83000 KB |
Output is correct |
40 |
Correct |
109 ms |
82872 KB |
Output is correct |
41 |
Correct |
112 ms |
83264 KB |
Output is correct |
42 |
Correct |
106 ms |
82996 KB |
Output is correct |
43 |
Correct |
107 ms |
83032 KB |
Output is correct |
44 |
Correct |
1548 ms |
225740 KB |
Output is correct |
45 |
Correct |
1506 ms |
231476 KB |
Output is correct |
46 |
Correct |
1588 ms |
224028 KB |
Output is correct |
47 |
Correct |
1290 ms |
228024 KB |
Output is correct |
48 |
Correct |
1604 ms |
221400 KB |
Output is correct |
49 |
Correct |
1627 ms |
224360 KB |
Output is correct |
50 |
Correct |
1594 ms |
233468 KB |
Output is correct |
51 |
Correct |
2106 ms |
242980 KB |
Output is correct |
52 |
Correct |
3336 ms |
304264 KB |
Output is correct |
53 |
Correct |
3112 ms |
286756 KB |
Output is correct |
54 |
Correct |
1976 ms |
226040 KB |
Output is correct |
55 |
Execution timed out |
6093 ms |
255036 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
75192 KB |
Output is correct |
2 |
Correct |
115 ms |
83420 KB |
Output is correct |
3 |
Correct |
105 ms |
82848 KB |
Output is correct |
4 |
Correct |
120 ms |
82868 KB |
Output is correct |
5 |
Correct |
37 ms |
71664 KB |
Output is correct |
6 |
Correct |
106 ms |
82872 KB |
Output is correct |
7 |
Correct |
73 ms |
75176 KB |
Output is correct |
8 |
Correct |
118 ms |
83588 KB |
Output is correct |
9 |
Correct |
111 ms |
82796 KB |
Output is correct |
10 |
Correct |
121 ms |
82892 KB |
Output is correct |
11 |
Correct |
37 ms |
71652 KB |
Output is correct |
12 |
Correct |
105 ms |
82788 KB |
Output is correct |
13 |
Correct |
110 ms |
82772 KB |
Output is correct |
14 |
Correct |
107 ms |
82800 KB |
Output is correct |
15 |
Correct |
98 ms |
82616 KB |
Output is correct |
16 |
Correct |
122 ms |
83076 KB |
Output is correct |
17 |
Correct |
101 ms |
82832 KB |
Output is correct |
18 |
Correct |
108 ms |
82780 KB |
Output is correct |
19 |
Correct |
38 ms |
71452 KB |
Output is correct |
20 |
Correct |
38 ms |
71452 KB |
Output is correct |
21 |
Correct |
40 ms |
71500 KB |
Output is correct |
22 |
Correct |
77 ms |
75736 KB |
Output is correct |
23 |
Correct |
113 ms |
84204 KB |
Output is correct |
24 |
Correct |
108 ms |
83680 KB |
Output is correct |
25 |
Correct |
125 ms |
83700 KB |
Output is correct |
26 |
Correct |
40 ms |
71620 KB |
Output is correct |
27 |
Correct |
109 ms |
83636 KB |
Output is correct |
28 |
Correct |
107 ms |
83556 KB |
Output is correct |
29 |
Correct |
117 ms |
83624 KB |
Output is correct |
30 |
Correct |
104 ms |
83380 KB |
Output is correct |
31 |
Correct |
110 ms |
83860 KB |
Output is correct |
32 |
Correct |
104 ms |
83640 KB |
Output is correct |
33 |
Correct |
114 ms |
83652 KB |
Output is correct |
34 |
Correct |
1550 ms |
228212 KB |
Output is correct |
35 |
Correct |
1586 ms |
225300 KB |
Output is correct |
36 |
Correct |
1595 ms |
225076 KB |
Output is correct |
37 |
Incorrect |
1374 ms |
229720 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
75192 KB |
Output is correct |
2 |
Correct |
115 ms |
83420 KB |
Output is correct |
3 |
Correct |
105 ms |
82848 KB |
Output is correct |
4 |
Correct |
120 ms |
82868 KB |
Output is correct |
5 |
Correct |
37 ms |
71664 KB |
Output is correct |
6 |
Correct |
106 ms |
82872 KB |
Output is correct |
7 |
Correct |
73 ms |
75176 KB |
Output is correct |
8 |
Correct |
118 ms |
83588 KB |
Output is correct |
9 |
Correct |
111 ms |
82796 KB |
Output is correct |
10 |
Correct |
121 ms |
82892 KB |
Output is correct |
11 |
Correct |
37 ms |
71652 KB |
Output is correct |
12 |
Correct |
105 ms |
82788 KB |
Output is correct |
13 |
Correct |
110 ms |
82772 KB |
Output is correct |
14 |
Correct |
107 ms |
82800 KB |
Output is correct |
15 |
Correct |
98 ms |
82616 KB |
Output is correct |
16 |
Correct |
122 ms |
83076 KB |
Output is correct |
17 |
Correct |
101 ms |
82832 KB |
Output is correct |
18 |
Correct |
108 ms |
82780 KB |
Output is correct |
19 |
Correct |
74 ms |
75176 KB |
Output is correct |
20 |
Correct |
116 ms |
83352 KB |
Output is correct |
21 |
Correct |
106 ms |
82892 KB |
Output is correct |
22 |
Correct |
121 ms |
82936 KB |
Output is correct |
23 |
Correct |
36 ms |
71644 KB |
Output is correct |
24 |
Correct |
109 ms |
82900 KB |
Output is correct |
25 |
Correct |
122 ms |
82928 KB |
Output is correct |
26 |
Correct |
104 ms |
82720 KB |
Output is correct |
27 |
Correct |
102 ms |
82592 KB |
Output is correct |
28 |
Correct |
107 ms |
83044 KB |
Output is correct |
29 |
Correct |
103 ms |
82748 KB |
Output is correct |
30 |
Correct |
109 ms |
82760 KB |
Output is correct |
31 |
Correct |
1526 ms |
225432 KB |
Output is correct |
32 |
Correct |
1523 ms |
231512 KB |
Output is correct |
33 |
Correct |
1409 ms |
223892 KB |
Output is correct |
34 |
Correct |
1279 ms |
227968 KB |
Output is correct |
35 |
Correct |
1616 ms |
221392 KB |
Output is correct |
36 |
Correct |
1628 ms |
223896 KB |
Output is correct |
37 |
Correct |
1616 ms |
233552 KB |
Output is correct |
38 |
Correct |
38 ms |
71452 KB |
Output is correct |
39 |
Correct |
38 ms |
71452 KB |
Output is correct |
40 |
Correct |
40 ms |
71500 KB |
Output is correct |
41 |
Correct |
77 ms |
75736 KB |
Output is correct |
42 |
Correct |
113 ms |
84204 KB |
Output is correct |
43 |
Correct |
108 ms |
83680 KB |
Output is correct |
44 |
Correct |
125 ms |
83700 KB |
Output is correct |
45 |
Correct |
40 ms |
71620 KB |
Output is correct |
46 |
Correct |
109 ms |
83636 KB |
Output is correct |
47 |
Correct |
107 ms |
83556 KB |
Output is correct |
48 |
Correct |
117 ms |
83624 KB |
Output is correct |
49 |
Correct |
104 ms |
83380 KB |
Output is correct |
50 |
Correct |
110 ms |
83860 KB |
Output is correct |
51 |
Correct |
104 ms |
83640 KB |
Output is correct |
52 |
Correct |
114 ms |
83652 KB |
Output is correct |
53 |
Correct |
1550 ms |
228212 KB |
Output is correct |
54 |
Correct |
1586 ms |
225300 KB |
Output is correct |
55 |
Correct |
1595 ms |
225076 KB |
Output is correct |
56 |
Incorrect |
1374 ms |
229720 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
75192 KB |
Output is correct |
2 |
Correct |
115 ms |
83420 KB |
Output is correct |
3 |
Correct |
105 ms |
82848 KB |
Output is correct |
4 |
Correct |
120 ms |
82868 KB |
Output is correct |
5 |
Correct |
37 ms |
71664 KB |
Output is correct |
6 |
Correct |
106 ms |
82872 KB |
Output is correct |
7 |
Correct |
73 ms |
75176 KB |
Output is correct |
8 |
Correct |
118 ms |
83588 KB |
Output is correct |
9 |
Correct |
111 ms |
82796 KB |
Output is correct |
10 |
Correct |
121 ms |
82892 KB |
Output is correct |
11 |
Correct |
37 ms |
71652 KB |
Output is correct |
12 |
Correct |
105 ms |
82788 KB |
Output is correct |
13 |
Correct |
110 ms |
82772 KB |
Output is correct |
14 |
Correct |
107 ms |
82800 KB |
Output is correct |
15 |
Correct |
98 ms |
82616 KB |
Output is correct |
16 |
Correct |
122 ms |
83076 KB |
Output is correct |
17 |
Correct |
101 ms |
82832 KB |
Output is correct |
18 |
Correct |
108 ms |
82780 KB |
Output is correct |
19 |
Correct |
74 ms |
75176 KB |
Output is correct |
20 |
Correct |
116 ms |
83352 KB |
Output is correct |
21 |
Correct |
106 ms |
82892 KB |
Output is correct |
22 |
Correct |
121 ms |
82936 KB |
Output is correct |
23 |
Correct |
36 ms |
71644 KB |
Output is correct |
24 |
Correct |
109 ms |
82900 KB |
Output is correct |
25 |
Correct |
122 ms |
82928 KB |
Output is correct |
26 |
Correct |
104 ms |
82720 KB |
Output is correct |
27 |
Correct |
102 ms |
82592 KB |
Output is correct |
28 |
Correct |
107 ms |
83044 KB |
Output is correct |
29 |
Correct |
103 ms |
82748 KB |
Output is correct |
30 |
Correct |
109 ms |
82760 KB |
Output is correct |
31 |
Correct |
1526 ms |
225432 KB |
Output is correct |
32 |
Correct |
1523 ms |
231512 KB |
Output is correct |
33 |
Correct |
1409 ms |
223892 KB |
Output is correct |
34 |
Correct |
1279 ms |
227968 KB |
Output is correct |
35 |
Correct |
1616 ms |
221392 KB |
Output is correct |
36 |
Correct |
1628 ms |
223896 KB |
Output is correct |
37 |
Correct |
1616 ms |
233552 KB |
Output is correct |
38 |
Correct |
75 ms |
75388 KB |
Output is correct |
39 |
Correct |
113 ms |
83508 KB |
Output is correct |
40 |
Correct |
107 ms |
83032 KB |
Output is correct |
41 |
Correct |
145 ms |
83256 KB |
Output is correct |
42 |
Correct |
40 ms |
71560 KB |
Output is correct |
43 |
Correct |
106 ms |
83052 KB |
Output is correct |
44 |
Correct |
105 ms |
83012 KB |
Output is correct |
45 |
Correct |
108 ms |
83000 KB |
Output is correct |
46 |
Correct |
109 ms |
82872 KB |
Output is correct |
47 |
Correct |
112 ms |
83264 KB |
Output is correct |
48 |
Correct |
106 ms |
82996 KB |
Output is correct |
49 |
Correct |
107 ms |
83032 KB |
Output is correct |
50 |
Correct |
1548 ms |
225740 KB |
Output is correct |
51 |
Correct |
1506 ms |
231476 KB |
Output is correct |
52 |
Correct |
1588 ms |
224028 KB |
Output is correct |
53 |
Correct |
1290 ms |
228024 KB |
Output is correct |
54 |
Correct |
1604 ms |
221400 KB |
Output is correct |
55 |
Correct |
1627 ms |
224360 KB |
Output is correct |
56 |
Correct |
1594 ms |
233468 KB |
Output is correct |
57 |
Correct |
2106 ms |
242980 KB |
Output is correct |
58 |
Correct |
3336 ms |
304264 KB |
Output is correct |
59 |
Correct |
3112 ms |
286756 KB |
Output is correct |
60 |
Correct |
1976 ms |
226040 KB |
Output is correct |
61 |
Execution timed out |
6093 ms |
255036 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |