답안 #828864

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
828864 2023-08-17T17:54:15 Z QwertyPi From Hacks to Snitches (BOI21_watchmen) C++14
40 / 100
6000 ms 193356 KB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2.5e5 + 11;
const int MAXV = 2750 + 11;
const int MAXC = 1500 + 11;
const int MAXA = 2e6 + 11;
int cyc[MAXN], ci[MAXN], cl[MAXN], vi[MAXN];
vector<int> G[MAXN], H[MAXN];
int dp[MAXN], dp2[MAXV][MAXC];
bool vis[MAXN], vis2[MAXV][MAXC];
vector<pair<int, int>> ch[MAXA];
vector<int> cycles[MAXV];

struct edge{
	int u, v;
};

bool in_cycle(int v){
	return cyc[v] != 0;
}

int cycle_nxt(int v){
	assert(cyc[v] != 0);
	return cycles[cyc[v]][(ci[v] + 1) % cl[v]];
}

int cycle_prv(int v){
	assert(cyc[v] != 0);
	return cycles[cyc[v]][(ci[v] + cl[v] - 1) % cl[v]];
}

int32_t main(){
	cin.tie(0); cout.tie(0)->sync_with_stdio(false);
	int n, m; cin >> n >> m;
	vector<edge> E;
	for(int i = 0; i < m; i++){
		int u, v; cin >> u >> v;
		E.push_back({u, v});
	}
	int s_cyc = 0;
	int k; cin >> k;
	for(int i = 0; i < k; i++){
		vector<int> v;
		int l; cin >> l;
		for(int j = 0; j < l; j++){
			int u; cin >> u; v.push_back(u);
		}
		cycles[i + 1] = v;
		for(int j = 0; j < l; j++) cyc[v[j]] = i + 1, ci[v[j]] = j, cl[v[j]] = v.size(), vi[v[j]] = 1 + s_cyc + j; s_cyc += l;
	}

	for(int i = 0; i < m; i++){
		edge e = E[i];
		if(!in_cycle(e.u)) swap(e.u, e.v);
		if(in_cycle(e.u) && in_cycle(e.v)){
			if(e.u == cycle_nxt(e.v) || e.u == cycle_prv(e.v))
				continue;
			H[e.u].push_back(e.v);
			H[e.v].push_back(e.u);
		}else{
			G[e.u].push_back(e.v);
			G[e.v].push_back(e.u);
		}
	}
	// for(int i = 1; i <= n; i++) cout << cyc[i] << ' '; cout << endl;
	// for(int i = 1; i <= n; i++) cout << ci[i] << ' '; cout << endl;
	// for(int i = 1; i <= n; i++) cout << vi[i] << ' '; cout << endl;

	memset(dp, 0x3f, sizeof(dp));
	memset(dp2, 0x3f, sizeof(dp2));

	dp[1] = 0; ch[0].push_back({1, 0});
	for(int d = 0; d < MAXA; d++){
		for(auto [v, i] : ch[d]){
			// printf("dist[(%d %d)] = %d\n", v, i, d);
			if(!in_cycle(v)){
				assert(i == 0);
				if(vis[v]) continue; vis[v] = true;
				for(auto u : G[v]){
					if(!in_cycle(u)){
						if(dp[v] + 1 < dp[u]){
							dp[u] = dp[v] + 1;
							ch[d + 1].push_back({u, 0});
						}
					}else{
						for(int j = 0; j < cl[u]; j++){
							int dst = dp[v] + 1 + j;
							if(dst % cl[u] == ci[u]) continue;
							if(dst < dp2[vi[u]][dst % cl[u]]){
								dp2[vi[u]][dst % cl[u]] = dst;
								ch[dst].push_back({u, dst % cl[u]});
							}
						}
					}
				}
			}else{
				if(!vis[v]){
					for(int i = 0; i < cl[v]; i++){
						dp[v] = min(dp[v], dp2[vi[v]][i]);
					}
					// printf("dp[%d] = %d\n", v, dp[v]);
					vis[v] = true;
					for(auto u : G[v]){
						assert(!in_cycle(u));
						if(dp[v] + 1 < dp[u]){
							dp[u] = dp[v] + 1;
							ch[d + 1].push_back({u, 0});
						}
					}
				}

				assert(d % cl[v] != ci[v]);

				if(vis2[vi[v]][d % cl[v]]) continue; vis2[vi[v]][d % cl[v]] = true;
				// stationery
				if((d + 1) % cl[v] != ci[v] && d + 1 < dp2[vi[v]][(d + 1) % cl[v]]){
					dp2[vi[v]][(d + 1) % cl[v]] = d + 1;
					ch[d + 1].push_back({v, (d + 1) % cl[v]});
				}

				for(auto u : H[v]){
					for(int j = 0; j < cl[u]; j++){
						int dst = d + 1 + j * cl[v];
						if(dst % cl[u] != ci[u] && dst < dp2[vi[u]][dst % cl[u]]){
							dp2[vi[u]][dst % cl[u]] = dst;
							ch[dst].push_back({u, dst % cl[u]});
						}
					}
				}

				// forward
				int u = cycle_nxt(v);
				if(d + 1 < dp2[vi[u]][(d + 1) % cl[u]]){
					dp2[vi[u]][(d + 1) % cl[u]] = d + 1;
					ch[d + 1].push_back({u, (d + 1) % cl[u]});
				}
				
				// backward
				u = cycle_prv(v);
				if((d + 1) % cl[v] != ci[v] && (d + 2) % cl[v] != ci[v] && d + 1 < dp2[vi[u]][(d + 1) % cl[u]]){
					dp2[vi[u]][(d + 1) % cl[u]] = d + 1;
					ch[d + 1].push_back({u, (d + 1) % cl[u]});
				}
			}
		}
		ch[d].clear();
	}
	if(dp[n] == 0x3f3f3f3f){
		cout << "impossible" << endl;
	}else{
		cout << dp[n] << endl;
	}
}

Compilation message

watchmen.cpp: In function 'int32_t main()':
watchmen.cpp:51:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   51 |   for(int j = 0; j < l; j++) cyc[v[j]] = i + 1, ci[v[j]] = j, cl[v[j]] = v.size(), vi[v[j]] = 1 + s_cyc + j; s_cyc += l;
      |   ^~~
watchmen.cpp:51:110: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   51 |   for(int j = 0; j < l; j++) cyc[v[j]] = i + 1, ci[v[j]] = j, cl[v[j]] = v.size(), vi[v[j]] = 1 + s_cyc + j; s_cyc += l;
      |                                                                                                              ^~~~~
watchmen.cpp:76:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   76 |   for(auto [v, i] : ch[d]){
      |            ^
watchmen.cpp:80:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   80 |     if(vis[v]) continue; vis[v] = true;
      |     ^~
watchmen.cpp:80:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   80 |     if(vis[v]) continue; vis[v] = true;
      |                          ^~~
watchmen.cpp:116:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  116 |     if(vis2[vi[v]][d % cl[v]]) continue; vis2[vi[v]][d % cl[v]] = true;
      |     ^~
watchmen.cpp:116:42: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  116 |     if(vis2[vi[v]][d % cl[v]]) continue; vis2[vi[v]][d % cl[v]] = true;
      |                                          ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1291 ms 78296 KB Output is correct
2 Correct 79 ms 84984 KB Output is correct
3 Correct 78 ms 83784 KB Output is correct
4 Correct 1297 ms 83388 KB Output is correct
5 Correct 48 ms 76764 KB Output is correct
6 Correct 79 ms 83764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1282 ms 78300 KB Output is correct
2 Correct 83 ms 84916 KB Output is correct
3 Correct 83 ms 83792 KB Output is correct
4 Correct 1292 ms 83376 KB Output is correct
5 Correct 44 ms 76760 KB Output is correct
6 Correct 77 ms 83576 KB Output is correct
7 Correct 81 ms 83892 KB Output is correct
8 Correct 77 ms 83964 KB Output is correct
9 Correct 77 ms 84096 KB Output is correct
10 Correct 197 ms 82960 KB Output is correct
11 Correct 79 ms 82424 KB Output is correct
12 Correct 75 ms 83528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1282 ms 78300 KB Output is correct
2 Correct 83 ms 84916 KB Output is correct
3 Correct 83 ms 83792 KB Output is correct
4 Correct 1292 ms 83376 KB Output is correct
5 Correct 44 ms 76760 KB Output is correct
6 Correct 77 ms 83576 KB Output is correct
7 Correct 81 ms 83892 KB Output is correct
8 Correct 77 ms 83964 KB Output is correct
9 Correct 77 ms 84096 KB Output is correct
10 Correct 197 ms 82960 KB Output is correct
11 Correct 79 ms 82424 KB Output is correct
12 Correct 75 ms 83528 KB Output is correct
13 Correct 1281 ms 78304 KB Output is correct
14 Correct 102 ms 85000 KB Output is correct
15 Correct 77 ms 83776 KB Output is correct
16 Correct 1288 ms 83376 KB Output is correct
17 Correct 45 ms 76720 KB Output is correct
18 Correct 77 ms 83620 KB Output is correct
19 Correct 76 ms 83884 KB Output is correct
20 Correct 75 ms 83916 KB Output is correct
21 Correct 80 ms 84296 KB Output is correct
22 Correct 200 ms 83000 KB Output is correct
23 Correct 79 ms 82348 KB Output is correct
24 Correct 75 ms 83528 KB Output is correct
25 Correct 775 ms 148360 KB Output is correct
26 Correct 770 ms 193052 KB Output is correct
27 Correct 738 ms 188988 KB Output is correct
28 Correct 690 ms 193356 KB Output is correct
29 Execution timed out 6085 ms 179388 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1282 ms 78300 KB Output is correct
2 Correct 83 ms 84916 KB Output is correct
3 Correct 83 ms 83792 KB Output is correct
4 Correct 1292 ms 83376 KB Output is correct
5 Correct 44 ms 76760 KB Output is correct
6 Correct 77 ms 83576 KB Output is correct
7 Correct 81 ms 83892 KB Output is correct
8 Correct 77 ms 83964 KB Output is correct
9 Correct 77 ms 84096 KB Output is correct
10 Correct 197 ms 82960 KB Output is correct
11 Correct 79 ms 82424 KB Output is correct
12 Correct 75 ms 83528 KB Output is correct
13 Correct 1281 ms 78304 KB Output is correct
14 Correct 102 ms 85000 KB Output is correct
15 Correct 77 ms 83776 KB Output is correct
16 Correct 1288 ms 83376 KB Output is correct
17 Correct 45 ms 76720 KB Output is correct
18 Correct 77 ms 83620 KB Output is correct
19 Correct 76 ms 83884 KB Output is correct
20 Correct 75 ms 83916 KB Output is correct
21 Correct 80 ms 84296 KB Output is correct
22 Correct 200 ms 83000 KB Output is correct
23 Correct 79 ms 82348 KB Output is correct
24 Correct 75 ms 83528 KB Output is correct
25 Correct 775 ms 148360 KB Output is correct
26 Correct 770 ms 193052 KB Output is correct
27 Correct 738 ms 188988 KB Output is correct
28 Correct 690 ms 193356 KB Output is correct
29 Execution timed out 6085 ms 179388 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1291 ms 78296 KB Output is correct
2 Correct 79 ms 84984 KB Output is correct
3 Correct 78 ms 83784 KB Output is correct
4 Correct 1297 ms 83388 KB Output is correct
5 Correct 48 ms 76764 KB Output is correct
6 Correct 79 ms 83764 KB Output is correct
7 Correct 1282 ms 78300 KB Output is correct
8 Correct 83 ms 84916 KB Output is correct
9 Correct 83 ms 83792 KB Output is correct
10 Correct 1292 ms 83376 KB Output is correct
11 Correct 44 ms 76760 KB Output is correct
12 Correct 77 ms 83576 KB Output is correct
13 Correct 81 ms 83892 KB Output is correct
14 Correct 77 ms 83964 KB Output is correct
15 Correct 77 ms 84096 KB Output is correct
16 Correct 197 ms 82960 KB Output is correct
17 Correct 79 ms 82424 KB Output is correct
18 Correct 75 ms 83528 KB Output is correct
19 Correct 46 ms 76372 KB Output is correct
20 Correct 39 ms 76356 KB Output is correct
21 Correct 39 ms 76428 KB Output is correct
22 Correct 1270 ms 78304 KB Output is correct
23 Correct 98 ms 84976 KB Output is correct
24 Correct 76 ms 83796 KB Output is correct
25 Correct 1288 ms 83384 KB Output is correct
26 Correct 41 ms 76672 KB Output is correct
27 Correct 76 ms 83532 KB Output is correct
28 Correct 87 ms 84036 KB Output is correct
29 Correct 75 ms 83916 KB Output is correct
30 Correct 78 ms 84164 KB Output is correct
31 Correct 203 ms 83000 KB Output is correct
32 Correct 79 ms 82392 KB Output is correct
33 Correct 84 ms 83480 KB Output is correct
34 Correct 765 ms 147484 KB Output is correct
35 Correct 796 ms 181840 KB Output is correct
36 Correct 817 ms 181852 KB Output is correct
37 Correct 741 ms 188664 KB Output is correct
38 Correct 776 ms 185796 KB Output is correct
39 Correct 2913 ms 179880 KB Output is correct
40 Correct 1762 ms 180216 KB Output is correct
41 Correct 1098 ms 180060 KB Output is correct
42 Correct 828 ms 185752 KB Output is correct
43 Correct 822 ms 190464 KB Output is correct
44 Correct 795 ms 190512 KB Output is correct
45 Correct 837 ms 183668 KB Output is correct
46 Correct 778 ms 185852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1291 ms 78296 KB Output is correct
2 Correct 79 ms 84984 KB Output is correct
3 Correct 78 ms 83784 KB Output is correct
4 Correct 1297 ms 83388 KB Output is correct
5 Correct 48 ms 76764 KB Output is correct
6 Correct 79 ms 83764 KB Output is correct
7 Correct 1282 ms 78300 KB Output is correct
8 Correct 83 ms 84916 KB Output is correct
9 Correct 83 ms 83792 KB Output is correct
10 Correct 1292 ms 83376 KB Output is correct
11 Correct 44 ms 76760 KB Output is correct
12 Correct 77 ms 83576 KB Output is correct
13 Correct 81 ms 83892 KB Output is correct
14 Correct 77 ms 83964 KB Output is correct
15 Correct 77 ms 84096 KB Output is correct
16 Correct 197 ms 82960 KB Output is correct
17 Correct 79 ms 82424 KB Output is correct
18 Correct 75 ms 83528 KB Output is correct
19 Correct 1281 ms 78304 KB Output is correct
20 Correct 102 ms 85000 KB Output is correct
21 Correct 77 ms 83776 KB Output is correct
22 Correct 1288 ms 83376 KB Output is correct
23 Correct 45 ms 76720 KB Output is correct
24 Correct 77 ms 83620 KB Output is correct
25 Correct 76 ms 83884 KB Output is correct
26 Correct 75 ms 83916 KB Output is correct
27 Correct 80 ms 84296 KB Output is correct
28 Correct 200 ms 83000 KB Output is correct
29 Correct 79 ms 82348 KB Output is correct
30 Correct 75 ms 83528 KB Output is correct
31 Correct 775 ms 148360 KB Output is correct
32 Correct 770 ms 193052 KB Output is correct
33 Correct 738 ms 188988 KB Output is correct
34 Correct 690 ms 193356 KB Output is correct
35 Execution timed out 6085 ms 179388 KB Time limit exceeded
36 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1291 ms 78296 KB Output is correct
2 Correct 79 ms 84984 KB Output is correct
3 Correct 78 ms 83784 KB Output is correct
4 Correct 1297 ms 83388 KB Output is correct
5 Correct 48 ms 76764 KB Output is correct
6 Correct 79 ms 83764 KB Output is correct
7 Correct 1282 ms 78300 KB Output is correct
8 Correct 83 ms 84916 KB Output is correct
9 Correct 83 ms 83792 KB Output is correct
10 Correct 1292 ms 83376 KB Output is correct
11 Correct 44 ms 76760 KB Output is correct
12 Correct 77 ms 83576 KB Output is correct
13 Correct 81 ms 83892 KB Output is correct
14 Correct 77 ms 83964 KB Output is correct
15 Correct 77 ms 84096 KB Output is correct
16 Correct 197 ms 82960 KB Output is correct
17 Correct 79 ms 82424 KB Output is correct
18 Correct 75 ms 83528 KB Output is correct
19 Correct 1281 ms 78304 KB Output is correct
20 Correct 102 ms 85000 KB Output is correct
21 Correct 77 ms 83776 KB Output is correct
22 Correct 1288 ms 83376 KB Output is correct
23 Correct 45 ms 76720 KB Output is correct
24 Correct 77 ms 83620 KB Output is correct
25 Correct 76 ms 83884 KB Output is correct
26 Correct 75 ms 83916 KB Output is correct
27 Correct 80 ms 84296 KB Output is correct
28 Correct 200 ms 83000 KB Output is correct
29 Correct 79 ms 82348 KB Output is correct
30 Correct 75 ms 83528 KB Output is correct
31 Correct 775 ms 148360 KB Output is correct
32 Correct 770 ms 193052 KB Output is correct
33 Correct 738 ms 188988 KB Output is correct
34 Correct 690 ms 193356 KB Output is correct
35 Execution timed out 6085 ms 179388 KB Time limit exceeded
36 Halted 0 ms 0 KB -