#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
const int LIM = 1600;
struct dat{
int x, f, y;
dat(){}
dat(int x, int f, int y): x(x), f(f), y(y){}
bool operator<(const dat &r)const{
return y>r.y;
}
};
int n, m, k;
vector<int> link[250002];
int cycleLen[250002];
int cycleIdx[250002];
vector<int> dist[250002];
vector<bool> visited[250002];
queue<dat> pq[LIM+5];
inline void update(dat tmp){
if(visited[tmp.x][tmp.f]) return;
visited[tmp.x][tmp.f] = 1;
dist[tmp.x][tmp.f] = tmp.y;
pq[tmp.y%LIM].push(tmp);
}
int main(){
scanf("%d %d", &n, &m);
for(int i=1; i<=m; i++){
int x, y;
scanf("%d %d", &x, &y);
link[x].push_back(y);
link[y].push_back(x);
}
scanf("%d", &k);
while(k--){
int l;
scanf("%d", &l);
for(int j=0; j<l; j++){
int tmp;
scanf("%d", &tmp);
cycleLen[tmp] = l;
cycleIdx[tmp] = j;
}
}
for(int i=1; i<=n; i++){
if(!cycleLen[i]) cycleLen[i] = 1;
dist[i] = vector<int> (cycleLen[i], 1e9);
visited[i].resize(cycleLen[i]);
}
update(dat(1, 0, 0));
for(int d=0; ; d+=LIM){
bool exist = 0;
for(int e=0; e<LIM; e++){
while(!pq[e].empty()){
exist=1;
dat tmp = pq[e].front(); pq[e].pop();
int x = tmp.x, f = tmp.f, y = tmp.y;
assert(f < cycleLen[x]);
if(x==n){
printf("%d", y);
return 0;
}
if(cycleLen[x] == 1){
for(int nxt: link[x]){
if(cycleLen[nxt] == 1) update(dat(nxt, 0, y+1));
else{
if(cycleIdx[nxt] != (y+1)%cycleLen[nxt]) update(dat(nxt, (y+1)%cycleLen[nxt], y+1));
int tn = (y+1-cycleIdx[nxt]-1+cycleLen[nxt]-1) / cycleLen[nxt] * cycleLen[nxt] + cycleIdx[nxt]+1;
update(dat(nxt, tn%cycleLen[nxt], tn));
for(int i=0; i<(int)visited[nxt].size(); i++) visited[nxt][i] = 1;
}
}
}
else{
for(int nxt: link[x]){
if(cycleLen[nxt] == 1) update(dat(nxt, 0, y+1));
else{
int nf = (f+1)%cycleLen[nxt];
if(nf == cycleIdx[nxt]) continue;
if(nf == cycleIdx[x] && f%cycleLen[nxt] == cycleIdx[nxt]) continue;
update(dat(nxt, nf, y+1));
}
}
}
}
}
if(!exist) break;
}
printf("impossible");
}
Compilation message
watchmen.cpp: In function 'int main()':
watchmen.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
watchmen.cpp:41:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
watchmen.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d", &k);
| ~~~~~^~~~~~~~~~
watchmen.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d", &l);
| ~~~~~^~~~~~~~~~
watchmen.cpp:52:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | scanf("%d", &tmp);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24260 KB |
Output is correct |
2 |
Correct |
96 ms |
33452 KB |
Output is correct |
3 |
Correct |
81 ms |
32452 KB |
Output is correct |
4 |
Correct |
85 ms |
32548 KB |
Output is correct |
5 |
Correct |
13 ms |
22944 KB |
Output is correct |
6 |
Correct |
87 ms |
32580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24260 KB |
Output is correct |
2 |
Correct |
98 ms |
33420 KB |
Output is correct |
3 |
Correct |
83 ms |
32536 KB |
Output is correct |
4 |
Correct |
83 ms |
32580 KB |
Output is correct |
5 |
Correct |
12 ms |
22884 KB |
Output is correct |
6 |
Correct |
84 ms |
32480 KB |
Output is correct |
7 |
Correct |
85 ms |
32452 KB |
Output is correct |
8 |
Correct |
82 ms |
32428 KB |
Output is correct |
9 |
Correct |
90 ms |
32480 KB |
Output is correct |
10 |
Correct |
78 ms |
32580 KB |
Output is correct |
11 |
Correct |
84 ms |
32516 KB |
Output is correct |
12 |
Correct |
87 ms |
32496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24260 KB |
Output is correct |
2 |
Correct |
98 ms |
33420 KB |
Output is correct |
3 |
Correct |
83 ms |
32536 KB |
Output is correct |
4 |
Correct |
83 ms |
32580 KB |
Output is correct |
5 |
Correct |
12 ms |
22884 KB |
Output is correct |
6 |
Correct |
84 ms |
32480 KB |
Output is correct |
7 |
Correct |
85 ms |
32452 KB |
Output is correct |
8 |
Correct |
82 ms |
32428 KB |
Output is correct |
9 |
Correct |
90 ms |
32480 KB |
Output is correct |
10 |
Correct |
78 ms |
32580 KB |
Output is correct |
11 |
Correct |
84 ms |
32516 KB |
Output is correct |
12 |
Correct |
87 ms |
32496 KB |
Output is correct |
13 |
Correct |
32 ms |
24276 KB |
Output is correct |
14 |
Correct |
94 ms |
33364 KB |
Output is correct |
15 |
Correct |
84 ms |
32452 KB |
Output is correct |
16 |
Correct |
89 ms |
32580 KB |
Output is correct |
17 |
Correct |
13 ms |
22936 KB |
Output is correct |
18 |
Correct |
85 ms |
32520 KB |
Output is correct |
19 |
Correct |
86 ms |
32468 KB |
Output is correct |
20 |
Correct |
98 ms |
32484 KB |
Output is correct |
21 |
Correct |
86 ms |
32416 KB |
Output is correct |
22 |
Correct |
84 ms |
32644 KB |
Output is correct |
23 |
Correct |
81 ms |
32788 KB |
Output is correct |
24 |
Correct |
81 ms |
32468 KB |
Output is correct |
25 |
Correct |
1588 ms |
80556 KB |
Output is correct |
26 |
Correct |
1497 ms |
84884 KB |
Output is correct |
27 |
Correct |
1409 ms |
81364 KB |
Output is correct |
28 |
Correct |
1163 ms |
85148 KB |
Output is correct |
29 |
Correct |
2324 ms |
76660 KB |
Output is correct |
30 |
Correct |
2346 ms |
85356 KB |
Output is correct |
31 |
Correct |
1653 ms |
84376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24260 KB |
Output is correct |
2 |
Correct |
98 ms |
33420 KB |
Output is correct |
3 |
Correct |
83 ms |
32536 KB |
Output is correct |
4 |
Correct |
83 ms |
32580 KB |
Output is correct |
5 |
Correct |
12 ms |
22884 KB |
Output is correct |
6 |
Correct |
84 ms |
32480 KB |
Output is correct |
7 |
Correct |
85 ms |
32452 KB |
Output is correct |
8 |
Correct |
82 ms |
32428 KB |
Output is correct |
9 |
Correct |
90 ms |
32480 KB |
Output is correct |
10 |
Correct |
78 ms |
32580 KB |
Output is correct |
11 |
Correct |
84 ms |
32516 KB |
Output is correct |
12 |
Correct |
87 ms |
32496 KB |
Output is correct |
13 |
Correct |
32 ms |
24276 KB |
Output is correct |
14 |
Correct |
94 ms |
33364 KB |
Output is correct |
15 |
Correct |
84 ms |
32452 KB |
Output is correct |
16 |
Correct |
89 ms |
32580 KB |
Output is correct |
17 |
Correct |
13 ms |
22936 KB |
Output is correct |
18 |
Correct |
85 ms |
32520 KB |
Output is correct |
19 |
Correct |
86 ms |
32468 KB |
Output is correct |
20 |
Correct |
98 ms |
32484 KB |
Output is correct |
21 |
Correct |
86 ms |
32416 KB |
Output is correct |
22 |
Correct |
84 ms |
32644 KB |
Output is correct |
23 |
Correct |
81 ms |
32788 KB |
Output is correct |
24 |
Correct |
81 ms |
32468 KB |
Output is correct |
25 |
Correct |
1588 ms |
80556 KB |
Output is correct |
26 |
Correct |
1497 ms |
84884 KB |
Output is correct |
27 |
Correct |
1409 ms |
81364 KB |
Output is correct |
28 |
Correct |
1163 ms |
85148 KB |
Output is correct |
29 |
Correct |
2324 ms |
76660 KB |
Output is correct |
30 |
Correct |
2346 ms |
85356 KB |
Output is correct |
31 |
Correct |
1653 ms |
84376 KB |
Output is correct |
32 |
Correct |
32 ms |
24388 KB |
Output is correct |
33 |
Correct |
96 ms |
34020 KB |
Output is correct |
34 |
Correct |
84 ms |
33180 KB |
Output is correct |
35 |
Correct |
82 ms |
33288 KB |
Output is correct |
36 |
Correct |
12 ms |
22960 KB |
Output is correct |
37 |
Correct |
82 ms |
33204 KB |
Output is correct |
38 |
Correct |
83 ms |
33092 KB |
Output is correct |
39 |
Correct |
93 ms |
33092 KB |
Output is correct |
40 |
Correct |
85 ms |
33140 KB |
Output is correct |
41 |
Correct |
78 ms |
33296 KB |
Output is correct |
42 |
Correct |
77 ms |
33176 KB |
Output is correct |
43 |
Correct |
81 ms |
33092 KB |
Output is correct |
44 |
Correct |
1583 ms |
80668 KB |
Output is correct |
45 |
Correct |
1549 ms |
85144 KB |
Output is correct |
46 |
Correct |
1424 ms |
81188 KB |
Output is correct |
47 |
Correct |
1182 ms |
84804 KB |
Output is correct |
48 |
Correct |
2277 ms |
76124 KB |
Output is correct |
49 |
Correct |
2299 ms |
78068 KB |
Output is correct |
50 |
Correct |
1611 ms |
84120 KB |
Output is correct |
51 |
Correct |
1846 ms |
89968 KB |
Output is correct |
52 |
Correct |
1859 ms |
100236 KB |
Output is correct |
53 |
Correct |
1450 ms |
89716 KB |
Output is correct |
54 |
Correct |
1190 ms |
114072 KB |
Output is correct |
55 |
Correct |
5392 ms |
128292 KB |
Output is correct |
56 |
Correct |
4355 ms |
120384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24260 KB |
Output is correct |
2 |
Correct |
96 ms |
33452 KB |
Output is correct |
3 |
Correct |
81 ms |
32452 KB |
Output is correct |
4 |
Correct |
85 ms |
32548 KB |
Output is correct |
5 |
Correct |
13 ms |
22944 KB |
Output is correct |
6 |
Correct |
87 ms |
32580 KB |
Output is correct |
7 |
Correct |
32 ms |
24260 KB |
Output is correct |
8 |
Correct |
98 ms |
33420 KB |
Output is correct |
9 |
Correct |
83 ms |
32536 KB |
Output is correct |
10 |
Correct |
83 ms |
32580 KB |
Output is correct |
11 |
Correct |
12 ms |
22884 KB |
Output is correct |
12 |
Correct |
84 ms |
32480 KB |
Output is correct |
13 |
Correct |
85 ms |
32452 KB |
Output is correct |
14 |
Correct |
82 ms |
32428 KB |
Output is correct |
15 |
Correct |
90 ms |
32480 KB |
Output is correct |
16 |
Correct |
78 ms |
32580 KB |
Output is correct |
17 |
Correct |
84 ms |
32516 KB |
Output is correct |
18 |
Correct |
87 ms |
32496 KB |
Output is correct |
19 |
Correct |
12 ms |
22880 KB |
Output is correct |
20 |
Correct |
12 ms |
22780 KB |
Output is correct |
21 |
Correct |
12 ms |
22860 KB |
Output is correct |
22 |
Correct |
34 ms |
24052 KB |
Output is correct |
23 |
Correct |
102 ms |
33344 KB |
Output is correct |
24 |
Correct |
87 ms |
32324 KB |
Output is correct |
25 |
Correct |
87 ms |
32340 KB |
Output is correct |
26 |
Correct |
13 ms |
22860 KB |
Output is correct |
27 |
Correct |
83 ms |
32324 KB |
Output is correct |
28 |
Correct |
91 ms |
32240 KB |
Output is correct |
29 |
Correct |
91 ms |
32232 KB |
Output is correct |
30 |
Correct |
88 ms |
32300 KB |
Output is correct |
31 |
Correct |
79 ms |
32324 KB |
Output is correct |
32 |
Correct |
83 ms |
32332 KB |
Output is correct |
33 |
Correct |
82 ms |
32268 KB |
Output is correct |
34 |
Correct |
1602 ms |
80476 KB |
Output is correct |
35 |
Correct |
1654 ms |
76900 KB |
Output is correct |
36 |
Correct |
1675 ms |
76928 KB |
Output is correct |
37 |
Incorrect |
1420 ms |
82416 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24260 KB |
Output is correct |
2 |
Correct |
96 ms |
33452 KB |
Output is correct |
3 |
Correct |
81 ms |
32452 KB |
Output is correct |
4 |
Correct |
85 ms |
32548 KB |
Output is correct |
5 |
Correct |
13 ms |
22944 KB |
Output is correct |
6 |
Correct |
87 ms |
32580 KB |
Output is correct |
7 |
Correct |
32 ms |
24260 KB |
Output is correct |
8 |
Correct |
98 ms |
33420 KB |
Output is correct |
9 |
Correct |
83 ms |
32536 KB |
Output is correct |
10 |
Correct |
83 ms |
32580 KB |
Output is correct |
11 |
Correct |
12 ms |
22884 KB |
Output is correct |
12 |
Correct |
84 ms |
32480 KB |
Output is correct |
13 |
Correct |
85 ms |
32452 KB |
Output is correct |
14 |
Correct |
82 ms |
32428 KB |
Output is correct |
15 |
Correct |
90 ms |
32480 KB |
Output is correct |
16 |
Correct |
78 ms |
32580 KB |
Output is correct |
17 |
Correct |
84 ms |
32516 KB |
Output is correct |
18 |
Correct |
87 ms |
32496 KB |
Output is correct |
19 |
Correct |
32 ms |
24276 KB |
Output is correct |
20 |
Correct |
94 ms |
33364 KB |
Output is correct |
21 |
Correct |
84 ms |
32452 KB |
Output is correct |
22 |
Correct |
89 ms |
32580 KB |
Output is correct |
23 |
Correct |
13 ms |
22936 KB |
Output is correct |
24 |
Correct |
85 ms |
32520 KB |
Output is correct |
25 |
Correct |
86 ms |
32468 KB |
Output is correct |
26 |
Correct |
98 ms |
32484 KB |
Output is correct |
27 |
Correct |
86 ms |
32416 KB |
Output is correct |
28 |
Correct |
84 ms |
32644 KB |
Output is correct |
29 |
Correct |
81 ms |
32788 KB |
Output is correct |
30 |
Correct |
81 ms |
32468 KB |
Output is correct |
31 |
Correct |
1588 ms |
80556 KB |
Output is correct |
32 |
Correct |
1497 ms |
84884 KB |
Output is correct |
33 |
Correct |
1409 ms |
81364 KB |
Output is correct |
34 |
Correct |
1163 ms |
85148 KB |
Output is correct |
35 |
Correct |
2324 ms |
76660 KB |
Output is correct |
36 |
Correct |
2346 ms |
85356 KB |
Output is correct |
37 |
Correct |
1653 ms |
84376 KB |
Output is correct |
38 |
Correct |
12 ms |
22880 KB |
Output is correct |
39 |
Correct |
12 ms |
22780 KB |
Output is correct |
40 |
Correct |
12 ms |
22860 KB |
Output is correct |
41 |
Correct |
34 ms |
24052 KB |
Output is correct |
42 |
Correct |
102 ms |
33344 KB |
Output is correct |
43 |
Correct |
87 ms |
32324 KB |
Output is correct |
44 |
Correct |
87 ms |
32340 KB |
Output is correct |
45 |
Correct |
13 ms |
22860 KB |
Output is correct |
46 |
Correct |
83 ms |
32324 KB |
Output is correct |
47 |
Correct |
91 ms |
32240 KB |
Output is correct |
48 |
Correct |
91 ms |
32232 KB |
Output is correct |
49 |
Correct |
88 ms |
32300 KB |
Output is correct |
50 |
Correct |
79 ms |
32324 KB |
Output is correct |
51 |
Correct |
83 ms |
32332 KB |
Output is correct |
52 |
Correct |
82 ms |
32268 KB |
Output is correct |
53 |
Correct |
1602 ms |
80476 KB |
Output is correct |
54 |
Correct |
1654 ms |
76900 KB |
Output is correct |
55 |
Correct |
1675 ms |
76928 KB |
Output is correct |
56 |
Incorrect |
1420 ms |
82416 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24260 KB |
Output is correct |
2 |
Correct |
96 ms |
33452 KB |
Output is correct |
3 |
Correct |
81 ms |
32452 KB |
Output is correct |
4 |
Correct |
85 ms |
32548 KB |
Output is correct |
5 |
Correct |
13 ms |
22944 KB |
Output is correct |
6 |
Correct |
87 ms |
32580 KB |
Output is correct |
7 |
Correct |
32 ms |
24260 KB |
Output is correct |
8 |
Correct |
98 ms |
33420 KB |
Output is correct |
9 |
Correct |
83 ms |
32536 KB |
Output is correct |
10 |
Correct |
83 ms |
32580 KB |
Output is correct |
11 |
Correct |
12 ms |
22884 KB |
Output is correct |
12 |
Correct |
84 ms |
32480 KB |
Output is correct |
13 |
Correct |
85 ms |
32452 KB |
Output is correct |
14 |
Correct |
82 ms |
32428 KB |
Output is correct |
15 |
Correct |
90 ms |
32480 KB |
Output is correct |
16 |
Correct |
78 ms |
32580 KB |
Output is correct |
17 |
Correct |
84 ms |
32516 KB |
Output is correct |
18 |
Correct |
87 ms |
32496 KB |
Output is correct |
19 |
Correct |
32 ms |
24276 KB |
Output is correct |
20 |
Correct |
94 ms |
33364 KB |
Output is correct |
21 |
Correct |
84 ms |
32452 KB |
Output is correct |
22 |
Correct |
89 ms |
32580 KB |
Output is correct |
23 |
Correct |
13 ms |
22936 KB |
Output is correct |
24 |
Correct |
85 ms |
32520 KB |
Output is correct |
25 |
Correct |
86 ms |
32468 KB |
Output is correct |
26 |
Correct |
98 ms |
32484 KB |
Output is correct |
27 |
Correct |
86 ms |
32416 KB |
Output is correct |
28 |
Correct |
84 ms |
32644 KB |
Output is correct |
29 |
Correct |
81 ms |
32788 KB |
Output is correct |
30 |
Correct |
81 ms |
32468 KB |
Output is correct |
31 |
Correct |
1588 ms |
80556 KB |
Output is correct |
32 |
Correct |
1497 ms |
84884 KB |
Output is correct |
33 |
Correct |
1409 ms |
81364 KB |
Output is correct |
34 |
Correct |
1163 ms |
85148 KB |
Output is correct |
35 |
Correct |
2324 ms |
76660 KB |
Output is correct |
36 |
Correct |
2346 ms |
85356 KB |
Output is correct |
37 |
Correct |
1653 ms |
84376 KB |
Output is correct |
38 |
Correct |
32 ms |
24388 KB |
Output is correct |
39 |
Correct |
96 ms |
34020 KB |
Output is correct |
40 |
Correct |
84 ms |
33180 KB |
Output is correct |
41 |
Correct |
82 ms |
33288 KB |
Output is correct |
42 |
Correct |
12 ms |
22960 KB |
Output is correct |
43 |
Correct |
82 ms |
33204 KB |
Output is correct |
44 |
Correct |
83 ms |
33092 KB |
Output is correct |
45 |
Correct |
93 ms |
33092 KB |
Output is correct |
46 |
Correct |
85 ms |
33140 KB |
Output is correct |
47 |
Correct |
78 ms |
33296 KB |
Output is correct |
48 |
Correct |
77 ms |
33176 KB |
Output is correct |
49 |
Correct |
81 ms |
33092 KB |
Output is correct |
50 |
Correct |
1583 ms |
80668 KB |
Output is correct |
51 |
Correct |
1549 ms |
85144 KB |
Output is correct |
52 |
Correct |
1424 ms |
81188 KB |
Output is correct |
53 |
Correct |
1182 ms |
84804 KB |
Output is correct |
54 |
Correct |
2277 ms |
76124 KB |
Output is correct |
55 |
Correct |
2299 ms |
78068 KB |
Output is correct |
56 |
Correct |
1611 ms |
84120 KB |
Output is correct |
57 |
Correct |
1846 ms |
89968 KB |
Output is correct |
58 |
Correct |
1859 ms |
100236 KB |
Output is correct |
59 |
Correct |
1450 ms |
89716 KB |
Output is correct |
60 |
Correct |
1190 ms |
114072 KB |
Output is correct |
61 |
Correct |
5392 ms |
128292 KB |
Output is correct |
62 |
Correct |
4355 ms |
120384 KB |
Output is correct |
63 |
Correct |
12 ms |
22880 KB |
Output is correct |
64 |
Correct |
12 ms |
22780 KB |
Output is correct |
65 |
Correct |
12 ms |
22860 KB |
Output is correct |
66 |
Correct |
34 ms |
24052 KB |
Output is correct |
67 |
Correct |
102 ms |
33344 KB |
Output is correct |
68 |
Correct |
87 ms |
32324 KB |
Output is correct |
69 |
Correct |
87 ms |
32340 KB |
Output is correct |
70 |
Correct |
13 ms |
22860 KB |
Output is correct |
71 |
Correct |
83 ms |
32324 KB |
Output is correct |
72 |
Correct |
91 ms |
32240 KB |
Output is correct |
73 |
Correct |
91 ms |
32232 KB |
Output is correct |
74 |
Correct |
88 ms |
32300 KB |
Output is correct |
75 |
Correct |
79 ms |
32324 KB |
Output is correct |
76 |
Correct |
83 ms |
32332 KB |
Output is correct |
77 |
Correct |
82 ms |
32268 KB |
Output is correct |
78 |
Correct |
1602 ms |
80476 KB |
Output is correct |
79 |
Correct |
1654 ms |
76900 KB |
Output is correct |
80 |
Correct |
1675 ms |
76928 KB |
Output is correct |
81 |
Incorrect |
1420 ms |
82416 KB |
Output isn't correct |
82 |
Halted |
0 ms |
0 KB |
- |