Submission #528098

# Submission time Handle Problem Language Result Execution time Memory
528098 2022-02-19T09:12:21 Z koioi.org-koosaga From Hacks to Snitches (BOI21_watchmen) C++17
70 / 100
6000 ms 373136 KB
#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++){
					for(int j = 0; j < vect[i]; j++){
						lint newDist = x.dist + j * sz(dist[v]);
						enq2(v, sum + newDist % vect[i], newDist);
					}
					sum += vect[i];
				}
			}
		}
		else{
			if(eist[v][w] != x.dist) continue;
			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]);
}
# Verdict Execution time Memory Grader output
1 Correct 89 ms 75192 KB Output is correct
2 Correct 125 ms 83404 KB Output is correct
3 Correct 121 ms 82816 KB Output is correct
4 Correct 168 ms 82936 KB Output is correct
5 Correct 50 ms 71648 KB Output is correct
6 Correct 119 ms 82856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 75192 KB Output is correct
2 Correct 128 ms 83620 KB Output is correct
3 Correct 135 ms 82896 KB Output is correct
4 Correct 137 ms 82844 KB Output is correct
5 Correct 50 ms 71648 KB Output is correct
6 Correct 121 ms 82840 KB Output is correct
7 Correct 133 ms 85088 KB Output is correct
8 Correct 129 ms 83740 KB Output is correct
9 Correct 107 ms 82868 KB Output is correct
10 Correct 137 ms 85388 KB Output is correct
11 Correct 118 ms 83952 KB Output is correct
12 Correct 137 ms 87056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 75192 KB Output is correct
2 Correct 128 ms 83620 KB Output is correct
3 Correct 135 ms 82896 KB Output is correct
4 Correct 137 ms 82844 KB Output is correct
5 Correct 50 ms 71648 KB Output is correct
6 Correct 121 ms 82840 KB Output is correct
7 Correct 133 ms 85088 KB Output is correct
8 Correct 129 ms 83740 KB Output is correct
9 Correct 107 ms 82868 KB Output is correct
10 Correct 137 ms 85388 KB Output is correct
11 Correct 118 ms 83952 KB Output is correct
12 Correct 137 ms 87056 KB Output is correct
13 Correct 87 ms 75196 KB Output is correct
14 Correct 139 ms 83408 KB Output is correct
15 Correct 150 ms 82888 KB Output is correct
16 Correct 147 ms 82824 KB Output is correct
17 Correct 53 ms 71712 KB Output is correct
18 Correct 130 ms 82872 KB Output is correct
19 Correct 135 ms 85144 KB Output is correct
20 Correct 132 ms 83696 KB Output is correct
21 Correct 136 ms 82888 KB Output is correct
22 Correct 132 ms 85396 KB Output is correct
23 Correct 116 ms 84020 KB Output is correct
24 Correct 144 ms 87060 KB Output is correct
25 Correct 1642 ms 225468 KB Output is correct
26 Correct 1776 ms 240024 KB Output is correct
27 Correct 1736 ms 240928 KB Output is correct
28 Correct 1405 ms 229524 KB Output is correct
29 Correct 1720 ms 221140 KB Output is correct
30 Correct 1952 ms 237924 KB Output is correct
31 Correct 3065 ms 353656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 75192 KB Output is correct
2 Correct 128 ms 83620 KB Output is correct
3 Correct 135 ms 82896 KB Output is correct
4 Correct 137 ms 82844 KB Output is correct
5 Correct 50 ms 71648 KB Output is correct
6 Correct 121 ms 82840 KB Output is correct
7 Correct 133 ms 85088 KB Output is correct
8 Correct 129 ms 83740 KB Output is correct
9 Correct 107 ms 82868 KB Output is correct
10 Correct 137 ms 85388 KB Output is correct
11 Correct 118 ms 83952 KB Output is correct
12 Correct 137 ms 87056 KB Output is correct
13 Correct 87 ms 75196 KB Output is correct
14 Correct 139 ms 83408 KB Output is correct
15 Correct 150 ms 82888 KB Output is correct
16 Correct 147 ms 82824 KB Output is correct
17 Correct 53 ms 71712 KB Output is correct
18 Correct 130 ms 82872 KB Output is correct
19 Correct 135 ms 85144 KB Output is correct
20 Correct 132 ms 83696 KB Output is correct
21 Correct 136 ms 82888 KB Output is correct
22 Correct 132 ms 85396 KB Output is correct
23 Correct 116 ms 84020 KB Output is correct
24 Correct 144 ms 87060 KB Output is correct
25 Correct 1642 ms 225468 KB Output is correct
26 Correct 1776 ms 240024 KB Output is correct
27 Correct 1736 ms 240928 KB Output is correct
28 Correct 1405 ms 229524 KB Output is correct
29 Correct 1720 ms 221140 KB Output is correct
30 Correct 1952 ms 237924 KB Output is correct
31 Correct 3065 ms 353656 KB Output is correct
32 Correct 89 ms 75708 KB Output is correct
33 Correct 167 ms 84516 KB Output is correct
34 Correct 121 ms 84000 KB Output is correct
35 Correct 141 ms 84088 KB Output is correct
36 Correct 53 ms 71620 KB Output is correct
37 Correct 132 ms 83928 KB Output is correct
38 Correct 124 ms 86252 KB Output is correct
39 Correct 124 ms 84900 KB Output is correct
40 Correct 109 ms 84012 KB Output is correct
41 Correct 135 ms 86628 KB Output is correct
42 Correct 141 ms 85108 KB Output is correct
43 Correct 134 ms 88196 KB Output is correct
44 Correct 1675 ms 227884 KB Output is correct
45 Correct 1752 ms 243256 KB Output is correct
46 Correct 1700 ms 243876 KB Output is correct
47 Correct 1447 ms 232240 KB Output is correct
48 Correct 1656 ms 223444 KB Output is correct
49 Correct 1890 ms 240232 KB Output is correct
50 Correct 2888 ms 353604 KB Output is correct
51 Execution timed out 6066 ms 242208 KB Time limit exceeded
52 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 89 ms 75192 KB Output is correct
2 Correct 125 ms 83404 KB Output is correct
3 Correct 121 ms 82816 KB Output is correct
4 Correct 168 ms 82936 KB Output is correct
5 Correct 50 ms 71648 KB Output is correct
6 Correct 119 ms 82856 KB Output is correct
7 Correct 87 ms 75192 KB Output is correct
8 Correct 128 ms 83620 KB Output is correct
9 Correct 135 ms 82896 KB Output is correct
10 Correct 137 ms 82844 KB Output is correct
11 Correct 50 ms 71648 KB Output is correct
12 Correct 121 ms 82840 KB Output is correct
13 Correct 133 ms 85088 KB Output is correct
14 Correct 129 ms 83740 KB Output is correct
15 Correct 107 ms 82868 KB Output is correct
16 Correct 137 ms 85388 KB Output is correct
17 Correct 118 ms 83952 KB Output is correct
18 Correct 137 ms 87056 KB Output is correct
19 Correct 38 ms 71456 KB Output is correct
20 Correct 35 ms 71504 KB Output is correct
21 Correct 36 ms 71496 KB Output is correct
22 Correct 84 ms 75192 KB Output is correct
23 Correct 126 ms 83500 KB Output is correct
24 Correct 119 ms 82884 KB Output is correct
25 Correct 133 ms 82824 KB Output is correct
26 Correct 48 ms 71584 KB Output is correct
27 Correct 116 ms 82900 KB Output is correct
28 Correct 129 ms 85196 KB Output is correct
29 Correct 118 ms 83740 KB Output is correct
30 Correct 105 ms 82800 KB Output is correct
31 Correct 125 ms 85428 KB Output is correct
32 Correct 116 ms 83952 KB Output is correct
33 Correct 140 ms 87076 KB Output is correct
34 Correct 1573 ms 227616 KB Output is correct
35 Correct 1730 ms 224368 KB Output is correct
36 Correct 1591 ms 224408 KB Output is correct
37 Correct 1471 ms 228972 KB Output is correct
38 Correct 1521 ms 227620 KB Output is correct
39 Correct 1580 ms 221720 KB Output is correct
40 Correct 1584 ms 222600 KB Output is correct
41 Correct 1550 ms 221812 KB Output is correct
42 Correct 1542 ms 225156 KB Output is correct
43 Correct 1542 ms 232488 KB Output is correct
44 Correct 1579 ms 233636 KB Output is correct
45 Correct 1664 ms 226952 KB Output is correct
46 Correct 1545 ms 225032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 75192 KB Output is correct
2 Correct 125 ms 83404 KB Output is correct
3 Correct 121 ms 82816 KB Output is correct
4 Correct 168 ms 82936 KB Output is correct
5 Correct 50 ms 71648 KB Output is correct
6 Correct 119 ms 82856 KB Output is correct
7 Correct 87 ms 75192 KB Output is correct
8 Correct 128 ms 83620 KB Output is correct
9 Correct 135 ms 82896 KB Output is correct
10 Correct 137 ms 82844 KB Output is correct
11 Correct 50 ms 71648 KB Output is correct
12 Correct 121 ms 82840 KB Output is correct
13 Correct 133 ms 85088 KB Output is correct
14 Correct 129 ms 83740 KB Output is correct
15 Correct 107 ms 82868 KB Output is correct
16 Correct 137 ms 85388 KB Output is correct
17 Correct 118 ms 83952 KB Output is correct
18 Correct 137 ms 87056 KB Output is correct
19 Correct 87 ms 75196 KB Output is correct
20 Correct 139 ms 83408 KB Output is correct
21 Correct 150 ms 82888 KB Output is correct
22 Correct 147 ms 82824 KB Output is correct
23 Correct 53 ms 71712 KB Output is correct
24 Correct 130 ms 82872 KB Output is correct
25 Correct 135 ms 85144 KB Output is correct
26 Correct 132 ms 83696 KB Output is correct
27 Correct 136 ms 82888 KB Output is correct
28 Correct 132 ms 85396 KB Output is correct
29 Correct 116 ms 84020 KB Output is correct
30 Correct 144 ms 87060 KB Output is correct
31 Correct 1642 ms 225468 KB Output is correct
32 Correct 1776 ms 240024 KB Output is correct
33 Correct 1736 ms 240928 KB Output is correct
34 Correct 1405 ms 229524 KB Output is correct
35 Correct 1720 ms 221140 KB Output is correct
36 Correct 1952 ms 237924 KB Output is correct
37 Correct 3065 ms 353656 KB Output is correct
38 Correct 38 ms 71456 KB Output is correct
39 Correct 35 ms 71504 KB Output is correct
40 Correct 36 ms 71496 KB Output is correct
41 Correct 84 ms 75192 KB Output is correct
42 Correct 126 ms 83500 KB Output is correct
43 Correct 119 ms 82884 KB Output is correct
44 Correct 133 ms 82824 KB Output is correct
45 Correct 48 ms 71584 KB Output is correct
46 Correct 116 ms 82900 KB Output is correct
47 Correct 129 ms 85196 KB Output is correct
48 Correct 118 ms 83740 KB Output is correct
49 Correct 105 ms 82800 KB Output is correct
50 Correct 125 ms 85428 KB Output is correct
51 Correct 116 ms 83952 KB Output is correct
52 Correct 140 ms 87076 KB Output is correct
53 Correct 1573 ms 227616 KB Output is correct
54 Correct 1730 ms 224368 KB Output is correct
55 Correct 1591 ms 224408 KB Output is correct
56 Correct 1471 ms 228972 KB Output is correct
57 Correct 1521 ms 227620 KB Output is correct
58 Correct 1580 ms 221720 KB Output is correct
59 Correct 1584 ms 222600 KB Output is correct
60 Correct 1550 ms 221812 KB Output is correct
61 Correct 1542 ms 225156 KB Output is correct
62 Correct 1542 ms 232488 KB Output is correct
63 Correct 1579 ms 233636 KB Output is correct
64 Correct 1664 ms 226952 KB Output is correct
65 Correct 1545 ms 225032 KB Output is correct
66 Correct 36 ms 71516 KB Output is correct
67 Correct 36 ms 71424 KB Output is correct
68 Correct 36 ms 71488 KB Output is correct
69 Correct 86 ms 75708 KB Output is correct
70 Correct 130 ms 84556 KB Output is correct
71 Correct 128 ms 83964 KB Output is correct
72 Correct 133 ms 84032 KB Output is correct
73 Correct 50 ms 71652 KB Output is correct
74 Correct 119 ms 84052 KB Output is correct
75 Correct 134 ms 86332 KB Output is correct
76 Correct 135 ms 84948 KB Output is correct
77 Correct 110 ms 84060 KB Output is correct
78 Correct 128 ms 86500 KB Output is correct
79 Correct 120 ms 85172 KB Output is correct
80 Correct 134 ms 88244 KB Output is correct
81 Correct 1592 ms 247232 KB Output is correct
82 Correct 1661 ms 261752 KB Output is correct
83 Correct 1648 ms 262736 KB Output is correct
84 Correct 1360 ms 251580 KB Output is correct
85 Correct 1704 ms 243092 KB Output is correct
86 Correct 2001 ms 259644 KB Output is correct
87 Correct 2869 ms 373136 KB Output is correct
88 Correct 1564 ms 265900 KB Output is correct
89 Correct 1612 ms 262800 KB Output is correct
90 Correct 1602 ms 262904 KB Output is correct
91 Correct 1491 ms 267320 KB Output is correct
92 Correct 1595 ms 266120 KB Output is correct
93 Correct 1601 ms 260264 KB Output is correct
94 Correct 1600 ms 260776 KB Output is correct
95 Correct 1573 ms 243704 KB Output is correct
96 Correct 1543 ms 263484 KB Output is correct
97 Correct 1526 ms 254384 KB Output is correct
98 Correct 1732 ms 272044 KB Output is correct
99 Correct 1615 ms 248716 KB Output is correct
100 Correct 1535 ms 263564 KB Output is correct
101 Correct 1574 ms 266212 KB Output is correct
102 Correct 1517 ms 274648 KB Output is correct
103 Correct 1775 ms 298700 KB Output is correct
104 Correct 2033 ms 291476 KB Output is correct
105 Correct 1619 ms 266044 KB Output is correct
106 Correct 1651 ms 265540 KB Output is correct
107 Correct 1539 ms 263716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 75192 KB Output is correct
2 Correct 125 ms 83404 KB Output is correct
3 Correct 121 ms 82816 KB Output is correct
4 Correct 168 ms 82936 KB Output is correct
5 Correct 50 ms 71648 KB Output is correct
6 Correct 119 ms 82856 KB Output is correct
7 Correct 87 ms 75192 KB Output is correct
8 Correct 128 ms 83620 KB Output is correct
9 Correct 135 ms 82896 KB Output is correct
10 Correct 137 ms 82844 KB Output is correct
11 Correct 50 ms 71648 KB Output is correct
12 Correct 121 ms 82840 KB Output is correct
13 Correct 133 ms 85088 KB Output is correct
14 Correct 129 ms 83740 KB Output is correct
15 Correct 107 ms 82868 KB Output is correct
16 Correct 137 ms 85388 KB Output is correct
17 Correct 118 ms 83952 KB Output is correct
18 Correct 137 ms 87056 KB Output is correct
19 Correct 87 ms 75196 KB Output is correct
20 Correct 139 ms 83408 KB Output is correct
21 Correct 150 ms 82888 KB Output is correct
22 Correct 147 ms 82824 KB Output is correct
23 Correct 53 ms 71712 KB Output is correct
24 Correct 130 ms 82872 KB Output is correct
25 Correct 135 ms 85144 KB Output is correct
26 Correct 132 ms 83696 KB Output is correct
27 Correct 136 ms 82888 KB Output is correct
28 Correct 132 ms 85396 KB Output is correct
29 Correct 116 ms 84020 KB Output is correct
30 Correct 144 ms 87060 KB Output is correct
31 Correct 1642 ms 225468 KB Output is correct
32 Correct 1776 ms 240024 KB Output is correct
33 Correct 1736 ms 240928 KB Output is correct
34 Correct 1405 ms 229524 KB Output is correct
35 Correct 1720 ms 221140 KB Output is correct
36 Correct 1952 ms 237924 KB Output is correct
37 Correct 3065 ms 353656 KB Output is correct
38 Correct 89 ms 75708 KB Output is correct
39 Correct 167 ms 84516 KB Output is correct
40 Correct 121 ms 84000 KB Output is correct
41 Correct 141 ms 84088 KB Output is correct
42 Correct 53 ms 71620 KB Output is correct
43 Correct 132 ms 83928 KB Output is correct
44 Correct 124 ms 86252 KB Output is correct
45 Correct 124 ms 84900 KB Output is correct
46 Correct 109 ms 84012 KB Output is correct
47 Correct 135 ms 86628 KB Output is correct
48 Correct 141 ms 85108 KB Output is correct
49 Correct 134 ms 88196 KB Output is correct
50 Correct 1675 ms 227884 KB Output is correct
51 Correct 1752 ms 243256 KB Output is correct
52 Correct 1700 ms 243876 KB Output is correct
53 Correct 1447 ms 232240 KB Output is correct
54 Correct 1656 ms 223444 KB Output is correct
55 Correct 1890 ms 240232 KB Output is correct
56 Correct 2888 ms 353604 KB Output is correct
57 Execution timed out 6066 ms 242208 KB Time limit exceeded
58 Halted 0 ms 0 KB -