#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
typedef pair<int, int> pii;
struct info{
int x, k, mod;
info(){}
info(int x, int k, int mod) : x(x), k(k), mod(mod) {}
};
int main(){
int n, m, a, b; scanf("%d%d", &n, &m);
vector<vector<int>> g(n+1);
for(int i = 0; i < m; ++i) scanf("%d%d", &a, &b), g[a].emplace_back(b), g[b].emplace_back(a);
int k; scanf("%d", &k);
vector<pii> ktcykl(n+1);
vector<int> sz(k+1); sz[0] = 1;
vector<vector<vector<int>>> dp(n+1);
for(int i = 1; i <= n; ++i) dp[i].resize(k+1);
for(int i = 1; i <= n; ++i) dp[i][0].emplace_back(2e09);
for(int i = 1; i <= k; ++i){
scanf("%d", &b), sz[i] = b;
for(int j = 1; j <= n; ++j) dp[j][i].resize(b, 2e09);
for(int j = 0; j < b; ++j) scanf("%d", &a), ktcykl[a] = pii(i, j);
}
queue<info> q;
for(int i = 0; i <= k; ++i) q.emplace(1, i, 0), dp[1][i][0] = 0;
while(!q.empty()){
info x = q.front(); q.pop();
int odl = dp[x.x][x.k][x.mod];
//printf("%d %d %d: %d\n", x.x, x.k, x.mod, dp[x.x][x.k][x.mod]);
if(!ktcykl[x.x].ff){
for(int u : g[x.x]){
//printf("%d %d\n", x.x, u);
if(!ktcykl[u].ff) for(int i = 0; i <= k; ++i){ if(dp[u][i][(odl+1)%sz[i]] > odl+1) q.emplace(u, i, (odl+1)%sz[i]), dp[u][i][(odl+1)%sz[i]] = odl+1;}
else if(ktcykl[u].ss != (odl+1)%sz[ktcykl[u].ff]) for(int i = 0; i <= k; ++i) if(dp[u][i][(odl+1)%sz[i]] > odl+1) q.emplace(u, i, (odl+1)%sz[i]), dp[u][i][(odl+1)%sz[i]] = odl+1;
}
if(dp[x.x][x.k][(x.mod+1)%sz[x.k]] > odl+1) q.emplace(x.x, x.k, (x.mod+1)%sz[x.k]), dp[x.x][x.k][(x.mod+1)%sz[x.k]] = odl+1;
}
else{
for(int u : g[x.x]){
if(ktcykl[x.x].ff != ktcykl[u].ff){
if(!ktcykl[u].ff) for(int i = 0; i <= k; ++i){ if(dp[u][i][(odl+1)%sz[i]] > odl+1) q.emplace(u, i, (odl+1)%sz[i]), dp[u][i][(odl+1)%sz[i]] = odl+1;}
else if(ktcykl[u].ss != (odl+1)%sz[ktcykl[u].ff]) for(int i = 0; i <= k; ++i) if(dp[u][i][(odl+1)%sz[i]] > odl+1) q.emplace(u, i, (odl+1)%sz[i]), dp[u][i][(odl+1)%sz[i]] = odl+1;
}
else{
if(ktcykl[u].ss == (odl+1)%sz[ktcykl[u].ff] || (ktcykl[x.x].ss == (odl+1)%sz[ktcykl[u].ff] && ktcykl[u].ss == odl%sz[ktcykl[u].ff])){/*printf("!!%d %d %d: %d %d %d\n", x.x, x.k, x.mod, u, ktcykl[x.x].ss, ktcykl[u].ss); */continue;}
else for(int i = 0; i <= k; ++i) if(dp[u][i][(odl+1)%sz[i]] > odl+1) q.emplace(u, i, (odl+1)%sz[i]), dp[u][i][(odl+1)%sz[i]] = odl+1;
}
}
if(ktcykl[x.x].ss != (odl+1)%sz[ktcykl[x.x].ff] && dp[x.x][x.k][(x.mod+1)%sz[x.k]] > odl+1) q.emplace(x.x, x.k, (x.mod+1)%sz[x.k]), dp[x.x][x.k][(x.mod+1)%sz[x.k]] = odl+1;
}
}
int wynik = 2e09;
for(int i = 0; i <= k; ++i)
for(int j = 0; j < sz[i]; ++j) wynik = min(wynik, dp[n][i][j]);
if(wynik == 2e09) printf("impossible\n");
else printf("%d\n", wynik);
return 0;
}
Compilation message
watchmen.cpp: In function 'int main()':
watchmen.cpp:12:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | int n, m, a, b; scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
watchmen.cpp:14:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | for(int i = 0; i < m; ++i) scanf("%d%d", &a, &b), g[a].emplace_back(b), g[b].emplace_back(a);
| ~~~~~^~~~~~~~~~~~~~~~
watchmen.cpp:15:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | int k; scanf("%d", &k);
| ~~~~~^~~~~~~~~~
watchmen.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%d", &b), sz[i] = b;
| ~~~~~^~~~~~~~~~
watchmen.cpp:24:37: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | for(int j = 0; j < b; ++j) scanf("%d", &a), ktcykl[a] = pii(i, j);
| ~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
1976 KB |
Output is correct |
2 |
Correct |
926 ms |
69552 KB |
Output is correct |
3 |
Correct |
1824 ms |
63144 KB |
Output is correct |
4 |
Correct |
2666 ms |
63492 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2109 ms |
63240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
271 ms |
1972 KB |
Output is correct |
2 |
Correct |
948 ms |
69580 KB |
Output is correct |
3 |
Correct |
1730 ms |
63144 KB |
Output is correct |
4 |
Correct |
2775 ms |
63588 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2056 ms |
63224 KB |
Output is correct |
7 |
Correct |
1326 ms |
64704 KB |
Output is correct |
8 |
Correct |
1349 ms |
69020 KB |
Output is correct |
9 |
Correct |
2077 ms |
87560 KB |
Output is correct |
10 |
Correct |
1854 ms |
65176 KB |
Output is correct |
11 |
Correct |
2297 ms |
88304 KB |
Output is correct |
12 |
Correct |
1822 ms |
66476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
271 ms |
1972 KB |
Output is correct |
2 |
Correct |
948 ms |
69580 KB |
Output is correct |
3 |
Correct |
1730 ms |
63144 KB |
Output is correct |
4 |
Correct |
2775 ms |
63588 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2056 ms |
63224 KB |
Output is correct |
7 |
Correct |
1326 ms |
64704 KB |
Output is correct |
8 |
Correct |
1349 ms |
69020 KB |
Output is correct |
9 |
Correct |
2077 ms |
87560 KB |
Output is correct |
10 |
Correct |
1854 ms |
65176 KB |
Output is correct |
11 |
Correct |
2297 ms |
88304 KB |
Output is correct |
12 |
Correct |
1822 ms |
66476 KB |
Output is correct |
13 |
Correct |
256 ms |
1876 KB |
Output is correct |
14 |
Correct |
967 ms |
69548 KB |
Output is correct |
15 |
Correct |
1767 ms |
63132 KB |
Output is correct |
16 |
Correct |
2585 ms |
63472 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2013 ms |
63348 KB |
Output is correct |
19 |
Correct |
1300 ms |
64704 KB |
Output is correct |
20 |
Correct |
1355 ms |
69188 KB |
Output is correct |
21 |
Correct |
2125 ms |
87556 KB |
Output is correct |
22 |
Correct |
1795 ms |
65176 KB |
Output is correct |
23 |
Correct |
2279 ms |
88308 KB |
Output is correct |
24 |
Correct |
1847 ms |
66504 KB |
Output is correct |
25 |
Execution timed out |
6030 ms |
316904 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
271 ms |
1972 KB |
Output is correct |
2 |
Correct |
948 ms |
69580 KB |
Output is correct |
3 |
Correct |
1730 ms |
63144 KB |
Output is correct |
4 |
Correct |
2775 ms |
63588 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2056 ms |
63224 KB |
Output is correct |
7 |
Correct |
1326 ms |
64704 KB |
Output is correct |
8 |
Correct |
1349 ms |
69020 KB |
Output is correct |
9 |
Correct |
2077 ms |
87560 KB |
Output is correct |
10 |
Correct |
1854 ms |
65176 KB |
Output is correct |
11 |
Correct |
2297 ms |
88304 KB |
Output is correct |
12 |
Correct |
1822 ms |
66476 KB |
Output is correct |
13 |
Correct |
256 ms |
1876 KB |
Output is correct |
14 |
Correct |
967 ms |
69548 KB |
Output is correct |
15 |
Correct |
1767 ms |
63132 KB |
Output is correct |
16 |
Correct |
2585 ms |
63472 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2013 ms |
63348 KB |
Output is correct |
19 |
Correct |
1300 ms |
64704 KB |
Output is correct |
20 |
Correct |
1355 ms |
69188 KB |
Output is correct |
21 |
Correct |
2125 ms |
87556 KB |
Output is correct |
22 |
Correct |
1795 ms |
65176 KB |
Output is correct |
23 |
Correct |
2279 ms |
88308 KB |
Output is correct |
24 |
Correct |
1847 ms |
66504 KB |
Output is correct |
25 |
Execution timed out |
6030 ms |
316904 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
1976 KB |
Output is correct |
2 |
Correct |
926 ms |
69552 KB |
Output is correct |
3 |
Correct |
1824 ms |
63144 KB |
Output is correct |
4 |
Correct |
2666 ms |
63492 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2109 ms |
63240 KB |
Output is correct |
7 |
Correct |
271 ms |
1972 KB |
Output is correct |
8 |
Correct |
948 ms |
69580 KB |
Output is correct |
9 |
Correct |
1730 ms |
63144 KB |
Output is correct |
10 |
Correct |
2775 ms |
63588 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2056 ms |
63224 KB |
Output is correct |
13 |
Correct |
1326 ms |
64704 KB |
Output is correct |
14 |
Correct |
1349 ms |
69020 KB |
Output is correct |
15 |
Correct |
2077 ms |
87560 KB |
Output is correct |
16 |
Correct |
1854 ms |
65176 KB |
Output is correct |
17 |
Correct |
2297 ms |
88304 KB |
Output is correct |
18 |
Correct |
1822 ms |
66476 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
248 ms |
1972 KB |
Output is correct |
23 |
Correct |
979 ms |
69552 KB |
Output is correct |
24 |
Correct |
1962 ms |
63192 KB |
Output is correct |
25 |
Correct |
2781 ms |
63488 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
2233 ms |
63268 KB |
Output is correct |
28 |
Correct |
1362 ms |
64696 KB |
Output is correct |
29 |
Correct |
1376 ms |
69020 KB |
Output is correct |
30 |
Correct |
2102 ms |
87560 KB |
Output is correct |
31 |
Correct |
1992 ms |
65300 KB |
Output is correct |
32 |
Correct |
2396 ms |
88408 KB |
Output is correct |
33 |
Correct |
1861 ms |
66472 KB |
Output is correct |
34 |
Execution timed out |
6052 ms |
329948 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
1976 KB |
Output is correct |
2 |
Correct |
926 ms |
69552 KB |
Output is correct |
3 |
Correct |
1824 ms |
63144 KB |
Output is correct |
4 |
Correct |
2666 ms |
63492 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2109 ms |
63240 KB |
Output is correct |
7 |
Correct |
271 ms |
1972 KB |
Output is correct |
8 |
Correct |
948 ms |
69580 KB |
Output is correct |
9 |
Correct |
1730 ms |
63144 KB |
Output is correct |
10 |
Correct |
2775 ms |
63588 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2056 ms |
63224 KB |
Output is correct |
13 |
Correct |
1326 ms |
64704 KB |
Output is correct |
14 |
Correct |
1349 ms |
69020 KB |
Output is correct |
15 |
Correct |
2077 ms |
87560 KB |
Output is correct |
16 |
Correct |
1854 ms |
65176 KB |
Output is correct |
17 |
Correct |
2297 ms |
88304 KB |
Output is correct |
18 |
Correct |
1822 ms |
66476 KB |
Output is correct |
19 |
Correct |
256 ms |
1876 KB |
Output is correct |
20 |
Correct |
967 ms |
69548 KB |
Output is correct |
21 |
Correct |
1767 ms |
63132 KB |
Output is correct |
22 |
Correct |
2585 ms |
63472 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2013 ms |
63348 KB |
Output is correct |
25 |
Correct |
1300 ms |
64704 KB |
Output is correct |
26 |
Correct |
1355 ms |
69188 KB |
Output is correct |
27 |
Correct |
2125 ms |
87556 KB |
Output is correct |
28 |
Correct |
1795 ms |
65176 KB |
Output is correct |
29 |
Correct |
2279 ms |
88308 KB |
Output is correct |
30 |
Correct |
1847 ms |
66504 KB |
Output is correct |
31 |
Execution timed out |
6030 ms |
316904 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
1976 KB |
Output is correct |
2 |
Correct |
926 ms |
69552 KB |
Output is correct |
3 |
Correct |
1824 ms |
63144 KB |
Output is correct |
4 |
Correct |
2666 ms |
63492 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2109 ms |
63240 KB |
Output is correct |
7 |
Correct |
271 ms |
1972 KB |
Output is correct |
8 |
Correct |
948 ms |
69580 KB |
Output is correct |
9 |
Correct |
1730 ms |
63144 KB |
Output is correct |
10 |
Correct |
2775 ms |
63588 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2056 ms |
63224 KB |
Output is correct |
13 |
Correct |
1326 ms |
64704 KB |
Output is correct |
14 |
Correct |
1349 ms |
69020 KB |
Output is correct |
15 |
Correct |
2077 ms |
87560 KB |
Output is correct |
16 |
Correct |
1854 ms |
65176 KB |
Output is correct |
17 |
Correct |
2297 ms |
88304 KB |
Output is correct |
18 |
Correct |
1822 ms |
66476 KB |
Output is correct |
19 |
Correct |
256 ms |
1876 KB |
Output is correct |
20 |
Correct |
967 ms |
69548 KB |
Output is correct |
21 |
Correct |
1767 ms |
63132 KB |
Output is correct |
22 |
Correct |
2585 ms |
63472 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2013 ms |
63348 KB |
Output is correct |
25 |
Correct |
1300 ms |
64704 KB |
Output is correct |
26 |
Correct |
1355 ms |
69188 KB |
Output is correct |
27 |
Correct |
2125 ms |
87556 KB |
Output is correct |
28 |
Correct |
1795 ms |
65176 KB |
Output is correct |
29 |
Correct |
2279 ms |
88308 KB |
Output is correct |
30 |
Correct |
1847 ms |
66504 KB |
Output is correct |
31 |
Execution timed out |
6030 ms |
316904 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |