#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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];
priority_queue<dat> pq;
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.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));
while(!pq.empty()){
dat tmp = pq.top(); pq.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));
}
}
}
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));
}
}
}
}
printf("impossible");
}
Compilation message
watchmen.cpp: In function 'int main()':
watchmen.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
watchmen.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
watchmen.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d", &k);
| ~~~~~^~~~~~~~~~
watchmen.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d", &l);
| ~~~~~^~~~~~~~~~
watchmen.cpp:47:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d", &tmp);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
23236 KB |
Output is correct |
2 |
Correct |
93 ms |
32972 KB |
Output is correct |
3 |
Correct |
91 ms |
32192 KB |
Output is correct |
4 |
Correct |
166 ms |
32312 KB |
Output is correct |
5 |
Correct |
12 ms |
21836 KB |
Output is correct |
6 |
Correct |
124 ms |
32204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
23308 KB |
Output is correct |
2 |
Correct |
93 ms |
33060 KB |
Output is correct |
3 |
Correct |
106 ms |
32288 KB |
Output is correct |
4 |
Correct |
168 ms |
32400 KB |
Output is correct |
5 |
Correct |
12 ms |
21836 KB |
Output is correct |
6 |
Correct |
106 ms |
32376 KB |
Output is correct |
7 |
Correct |
88 ms |
32136 KB |
Output is correct |
8 |
Correct |
95 ms |
32232 KB |
Output is correct |
9 |
Correct |
90 ms |
32132 KB |
Output is correct |
10 |
Correct |
139 ms |
32372 KB |
Output is correct |
11 |
Correct |
106 ms |
32176 KB |
Output is correct |
12 |
Correct |
96 ms |
32196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
23308 KB |
Output is correct |
2 |
Correct |
93 ms |
33060 KB |
Output is correct |
3 |
Correct |
106 ms |
32288 KB |
Output is correct |
4 |
Correct |
168 ms |
32400 KB |
Output is correct |
5 |
Correct |
12 ms |
21836 KB |
Output is correct |
6 |
Correct |
106 ms |
32376 KB |
Output is correct |
7 |
Correct |
88 ms |
32136 KB |
Output is correct |
8 |
Correct |
95 ms |
32232 KB |
Output is correct |
9 |
Correct |
90 ms |
32132 KB |
Output is correct |
10 |
Correct |
139 ms |
32372 KB |
Output is correct |
11 |
Correct |
106 ms |
32176 KB |
Output is correct |
12 |
Correct |
96 ms |
32196 KB |
Output is correct |
13 |
Correct |
35 ms |
23244 KB |
Output is correct |
14 |
Correct |
96 ms |
33064 KB |
Output is correct |
15 |
Correct |
99 ms |
32248 KB |
Output is correct |
16 |
Correct |
168 ms |
32460 KB |
Output is correct |
17 |
Correct |
12 ms |
21800 KB |
Output is correct |
18 |
Correct |
94 ms |
32260 KB |
Output is correct |
19 |
Correct |
90 ms |
32220 KB |
Output is correct |
20 |
Correct |
100 ms |
32192 KB |
Output is correct |
21 |
Correct |
87 ms |
32184 KB |
Output is correct |
22 |
Correct |
146 ms |
32332 KB |
Output is correct |
23 |
Correct |
99 ms |
32248 KB |
Output is correct |
24 |
Correct |
92 ms |
32176 KB |
Output is correct |
25 |
Correct |
2673 ms |
81548 KB |
Output is correct |
26 |
Correct |
2253 ms |
86000 KB |
Output is correct |
27 |
Correct |
2061 ms |
81596 KB |
Output is correct |
28 |
Correct |
1555 ms |
85840 KB |
Output is correct |
29 |
Execution timed out |
6085 ms |
76852 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
23308 KB |
Output is correct |
2 |
Correct |
93 ms |
33060 KB |
Output is correct |
3 |
Correct |
106 ms |
32288 KB |
Output is correct |
4 |
Correct |
168 ms |
32400 KB |
Output is correct |
5 |
Correct |
12 ms |
21836 KB |
Output is correct |
6 |
Correct |
106 ms |
32376 KB |
Output is correct |
7 |
Correct |
88 ms |
32136 KB |
Output is correct |
8 |
Correct |
95 ms |
32232 KB |
Output is correct |
9 |
Correct |
90 ms |
32132 KB |
Output is correct |
10 |
Correct |
139 ms |
32372 KB |
Output is correct |
11 |
Correct |
106 ms |
32176 KB |
Output is correct |
12 |
Correct |
96 ms |
32196 KB |
Output is correct |
13 |
Correct |
35 ms |
23244 KB |
Output is correct |
14 |
Correct |
96 ms |
33064 KB |
Output is correct |
15 |
Correct |
99 ms |
32248 KB |
Output is correct |
16 |
Correct |
168 ms |
32460 KB |
Output is correct |
17 |
Correct |
12 ms |
21800 KB |
Output is correct |
18 |
Correct |
94 ms |
32260 KB |
Output is correct |
19 |
Correct |
90 ms |
32220 KB |
Output is correct |
20 |
Correct |
100 ms |
32192 KB |
Output is correct |
21 |
Correct |
87 ms |
32184 KB |
Output is correct |
22 |
Correct |
146 ms |
32332 KB |
Output is correct |
23 |
Correct |
99 ms |
32248 KB |
Output is correct |
24 |
Correct |
92 ms |
32176 KB |
Output is correct |
25 |
Correct |
2673 ms |
81548 KB |
Output is correct |
26 |
Correct |
2253 ms |
86000 KB |
Output is correct |
27 |
Correct |
2061 ms |
81596 KB |
Output is correct |
28 |
Correct |
1555 ms |
85840 KB |
Output is correct |
29 |
Execution timed out |
6085 ms |
76852 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
23236 KB |
Output is correct |
2 |
Correct |
93 ms |
32972 KB |
Output is correct |
3 |
Correct |
91 ms |
32192 KB |
Output is correct |
4 |
Correct |
166 ms |
32312 KB |
Output is correct |
5 |
Correct |
12 ms |
21836 KB |
Output is correct |
6 |
Correct |
124 ms |
32204 KB |
Output is correct |
7 |
Correct |
33 ms |
23308 KB |
Output is correct |
8 |
Correct |
93 ms |
33060 KB |
Output is correct |
9 |
Correct |
106 ms |
32288 KB |
Output is correct |
10 |
Correct |
168 ms |
32400 KB |
Output is correct |
11 |
Correct |
12 ms |
21836 KB |
Output is correct |
12 |
Correct |
106 ms |
32376 KB |
Output is correct |
13 |
Correct |
88 ms |
32136 KB |
Output is correct |
14 |
Correct |
95 ms |
32232 KB |
Output is correct |
15 |
Correct |
90 ms |
32132 KB |
Output is correct |
16 |
Correct |
139 ms |
32372 KB |
Output is correct |
17 |
Correct |
106 ms |
32176 KB |
Output is correct |
18 |
Correct |
96 ms |
32196 KB |
Output is correct |
19 |
Correct |
11 ms |
21808 KB |
Output is correct |
20 |
Correct |
10 ms |
21832 KB |
Output is correct |
21 |
Correct |
11 ms |
21784 KB |
Output is correct |
22 |
Correct |
28 ms |
23368 KB |
Output is correct |
23 |
Correct |
92 ms |
32968 KB |
Output is correct |
24 |
Correct |
96 ms |
32272 KB |
Output is correct |
25 |
Correct |
181 ms |
32400 KB |
Output is correct |
26 |
Correct |
15 ms |
21836 KB |
Output is correct |
27 |
Correct |
102 ms |
32244 KB |
Output is correct |
28 |
Correct |
107 ms |
32140 KB |
Output is correct |
29 |
Correct |
87 ms |
32160 KB |
Output is correct |
30 |
Correct |
99 ms |
32148 KB |
Output is correct |
31 |
Correct |
155 ms |
32448 KB |
Output is correct |
32 |
Correct |
96 ms |
32252 KB |
Output is correct |
33 |
Correct |
92 ms |
32192 KB |
Output is correct |
34 |
Correct |
2696 ms |
81072 KB |
Output is correct |
35 |
Correct |
2670 ms |
77440 KB |
Output is correct |
36 |
Correct |
2653 ms |
77232 KB |
Output is correct |
37 |
Incorrect |
2067 ms |
82564 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
23236 KB |
Output is correct |
2 |
Correct |
93 ms |
32972 KB |
Output is correct |
3 |
Correct |
91 ms |
32192 KB |
Output is correct |
4 |
Correct |
166 ms |
32312 KB |
Output is correct |
5 |
Correct |
12 ms |
21836 KB |
Output is correct |
6 |
Correct |
124 ms |
32204 KB |
Output is correct |
7 |
Correct |
33 ms |
23308 KB |
Output is correct |
8 |
Correct |
93 ms |
33060 KB |
Output is correct |
9 |
Correct |
106 ms |
32288 KB |
Output is correct |
10 |
Correct |
168 ms |
32400 KB |
Output is correct |
11 |
Correct |
12 ms |
21836 KB |
Output is correct |
12 |
Correct |
106 ms |
32376 KB |
Output is correct |
13 |
Correct |
88 ms |
32136 KB |
Output is correct |
14 |
Correct |
95 ms |
32232 KB |
Output is correct |
15 |
Correct |
90 ms |
32132 KB |
Output is correct |
16 |
Correct |
139 ms |
32372 KB |
Output is correct |
17 |
Correct |
106 ms |
32176 KB |
Output is correct |
18 |
Correct |
96 ms |
32196 KB |
Output is correct |
19 |
Correct |
35 ms |
23244 KB |
Output is correct |
20 |
Correct |
96 ms |
33064 KB |
Output is correct |
21 |
Correct |
99 ms |
32248 KB |
Output is correct |
22 |
Correct |
168 ms |
32460 KB |
Output is correct |
23 |
Correct |
12 ms |
21800 KB |
Output is correct |
24 |
Correct |
94 ms |
32260 KB |
Output is correct |
25 |
Correct |
90 ms |
32220 KB |
Output is correct |
26 |
Correct |
100 ms |
32192 KB |
Output is correct |
27 |
Correct |
87 ms |
32184 KB |
Output is correct |
28 |
Correct |
146 ms |
32332 KB |
Output is correct |
29 |
Correct |
99 ms |
32248 KB |
Output is correct |
30 |
Correct |
92 ms |
32176 KB |
Output is correct |
31 |
Correct |
2673 ms |
81548 KB |
Output is correct |
32 |
Correct |
2253 ms |
86000 KB |
Output is correct |
33 |
Correct |
2061 ms |
81596 KB |
Output is correct |
34 |
Correct |
1555 ms |
85840 KB |
Output is correct |
35 |
Execution timed out |
6085 ms |
76852 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
23236 KB |
Output is correct |
2 |
Correct |
93 ms |
32972 KB |
Output is correct |
3 |
Correct |
91 ms |
32192 KB |
Output is correct |
4 |
Correct |
166 ms |
32312 KB |
Output is correct |
5 |
Correct |
12 ms |
21836 KB |
Output is correct |
6 |
Correct |
124 ms |
32204 KB |
Output is correct |
7 |
Correct |
33 ms |
23308 KB |
Output is correct |
8 |
Correct |
93 ms |
33060 KB |
Output is correct |
9 |
Correct |
106 ms |
32288 KB |
Output is correct |
10 |
Correct |
168 ms |
32400 KB |
Output is correct |
11 |
Correct |
12 ms |
21836 KB |
Output is correct |
12 |
Correct |
106 ms |
32376 KB |
Output is correct |
13 |
Correct |
88 ms |
32136 KB |
Output is correct |
14 |
Correct |
95 ms |
32232 KB |
Output is correct |
15 |
Correct |
90 ms |
32132 KB |
Output is correct |
16 |
Correct |
139 ms |
32372 KB |
Output is correct |
17 |
Correct |
106 ms |
32176 KB |
Output is correct |
18 |
Correct |
96 ms |
32196 KB |
Output is correct |
19 |
Correct |
35 ms |
23244 KB |
Output is correct |
20 |
Correct |
96 ms |
33064 KB |
Output is correct |
21 |
Correct |
99 ms |
32248 KB |
Output is correct |
22 |
Correct |
168 ms |
32460 KB |
Output is correct |
23 |
Correct |
12 ms |
21800 KB |
Output is correct |
24 |
Correct |
94 ms |
32260 KB |
Output is correct |
25 |
Correct |
90 ms |
32220 KB |
Output is correct |
26 |
Correct |
100 ms |
32192 KB |
Output is correct |
27 |
Correct |
87 ms |
32184 KB |
Output is correct |
28 |
Correct |
146 ms |
32332 KB |
Output is correct |
29 |
Correct |
99 ms |
32248 KB |
Output is correct |
30 |
Correct |
92 ms |
32176 KB |
Output is correct |
31 |
Correct |
2673 ms |
81548 KB |
Output is correct |
32 |
Correct |
2253 ms |
86000 KB |
Output is correct |
33 |
Correct |
2061 ms |
81596 KB |
Output is correct |
34 |
Correct |
1555 ms |
85840 KB |
Output is correct |
35 |
Execution timed out |
6085 ms |
76852 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |