Submission #528110

# Submission time Handle Problem Language Result Execution time Memory
528110 2022-02-19T09:41:15 Z koioi.org-koosaga From Hacks to Snitches (BOI21_watchmen) C++17
70 / 100
6000 ms 331756 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));
	vector<int> sum = {0};
	for(auto &i : vect){
		int nxt = i + sum.back(); sum.push_back(nxt);
	}
	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){
				for(int i = 0; i < sz(vect); i++){
					lint newDist = x.dist;
					enq2(v, sum[i] + newDist % vect[i], newDist);
				}
			}
		}
		else{
			if(eist[v][w] != x.dist) continue;
			int pos = cycs[w].first;
			enq2(v, sum[pos] + (x.dist + sz(dist[v])) % vect[pos], x.dist + sz(dist[v]));
			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 75 ms 75204 KB Output is correct
2 Correct 109 ms 83420 KB Output is correct
3 Correct 109 ms 82780 KB Output is correct
4 Correct 129 ms 82936 KB Output is correct
5 Correct 41 ms 71648 KB Output is correct
6 Correct 109 ms 82788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 75204 KB Output is correct
2 Correct 107 ms 83408 KB Output is correct
3 Correct 109 ms 82832 KB Output is correct
4 Correct 126 ms 82912 KB Output is correct
5 Correct 37 ms 71620 KB Output is correct
6 Correct 108 ms 82872 KB Output is correct
7 Correct 104 ms 82800 KB Output is correct
8 Correct 123 ms 82784 KB Output is correct
9 Correct 105 ms 82740 KB Output is correct
10 Correct 179 ms 83132 KB Output is correct
11 Correct 107 ms 82812 KB Output is correct
12 Correct 106 ms 82716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 75204 KB Output is correct
2 Correct 107 ms 83408 KB Output is correct
3 Correct 109 ms 82832 KB Output is correct
4 Correct 126 ms 82912 KB Output is correct
5 Correct 37 ms 71620 KB Output is correct
6 Correct 108 ms 82872 KB Output is correct
7 Correct 104 ms 82800 KB Output is correct
8 Correct 123 ms 82784 KB Output is correct
9 Correct 105 ms 82740 KB Output is correct
10 Correct 179 ms 83132 KB Output is correct
11 Correct 107 ms 82812 KB Output is correct
12 Correct 106 ms 82716 KB Output is correct
13 Correct 81 ms 75188 KB Output is correct
14 Correct 114 ms 83344 KB Output is correct
15 Correct 108 ms 82852 KB Output is correct
16 Correct 143 ms 82872 KB Output is correct
17 Correct 40 ms 71664 KB Output is correct
18 Correct 112 ms 83000 KB Output is correct
19 Correct 104 ms 82740 KB Output is correct
20 Correct 104 ms 82720 KB Output is correct
21 Correct 101 ms 82588 KB Output is correct
22 Correct 112 ms 83032 KB Output is correct
23 Correct 118 ms 82744 KB Output is correct
24 Correct 106 ms 82728 KB Output is correct
25 Correct 1600 ms 225404 KB Output is correct
26 Correct 1573 ms 231364 KB Output is correct
27 Correct 1441 ms 224516 KB Output is correct
28 Correct 1361 ms 227736 KB Output is correct
29 Correct 1625 ms 221116 KB Output is correct
30 Correct 1680 ms 223856 KB Output is correct
31 Correct 1600 ms 233236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 75204 KB Output is correct
2 Correct 107 ms 83408 KB Output is correct
3 Correct 109 ms 82832 KB Output is correct
4 Correct 126 ms 82912 KB Output is correct
5 Correct 37 ms 71620 KB Output is correct
6 Correct 108 ms 82872 KB Output is correct
7 Correct 104 ms 82800 KB Output is correct
8 Correct 123 ms 82784 KB Output is correct
9 Correct 105 ms 82740 KB Output is correct
10 Correct 179 ms 83132 KB Output is correct
11 Correct 107 ms 82812 KB Output is correct
12 Correct 106 ms 82716 KB Output is correct
13 Correct 81 ms 75188 KB Output is correct
14 Correct 114 ms 83344 KB Output is correct
15 Correct 108 ms 82852 KB Output is correct
16 Correct 143 ms 82872 KB Output is correct
17 Correct 40 ms 71664 KB Output is correct
18 Correct 112 ms 83000 KB Output is correct
19 Correct 104 ms 82740 KB Output is correct
20 Correct 104 ms 82720 KB Output is correct
21 Correct 101 ms 82588 KB Output is correct
22 Correct 112 ms 83032 KB Output is correct
23 Correct 118 ms 82744 KB Output is correct
24 Correct 106 ms 82728 KB Output is correct
25 Correct 1600 ms 225404 KB Output is correct
26 Correct 1573 ms 231364 KB Output is correct
27 Correct 1441 ms 224516 KB Output is correct
28 Correct 1361 ms 227736 KB Output is correct
29 Correct 1625 ms 221116 KB Output is correct
30 Correct 1680 ms 223856 KB Output is correct
31 Correct 1600 ms 233236 KB Output is correct
32 Correct 76 ms 75192 KB Output is correct
33 Correct 115 ms 83368 KB Output is correct
34 Correct 107 ms 82812 KB Output is correct
35 Correct 123 ms 82872 KB Output is correct
36 Correct 38 ms 71552 KB Output is correct
37 Correct 109 ms 82872 KB Output is correct
38 Correct 103 ms 82752 KB Output is correct
39 Correct 120 ms 82756 KB Output is correct
40 Correct 108 ms 82592 KB Output is correct
41 Correct 111 ms 83080 KB Output is correct
42 Correct 111 ms 82876 KB Output is correct
43 Correct 108 ms 82720 KB Output is correct
44 Correct 1569 ms 225512 KB Output is correct
45 Correct 1503 ms 231344 KB Output is correct
46 Correct 1464 ms 224552 KB Output is correct
47 Correct 1351 ms 227928 KB Output is correct
48 Correct 1630 ms 221220 KB Output is correct
49 Correct 1663 ms 223896 KB Output is correct
50 Correct 1620 ms 233232 KB Output is correct
51 Correct 2099 ms 242640 KB Output is correct
52 Correct 3862 ms 331756 KB Output is correct
53 Correct 3617 ms 314656 KB Output is correct
54 Correct 2619 ms 228836 KB Output is correct
55 Execution timed out 6072 ms 246508 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 75 ms 75204 KB Output is correct
2 Correct 109 ms 83420 KB Output is correct
3 Correct 109 ms 82780 KB Output is correct
4 Correct 129 ms 82936 KB Output is correct
5 Correct 41 ms 71648 KB Output is correct
6 Correct 109 ms 82788 KB Output is correct
7 Correct 75 ms 75204 KB Output is correct
8 Correct 107 ms 83408 KB Output is correct
9 Correct 109 ms 82832 KB Output is correct
10 Correct 126 ms 82912 KB Output is correct
11 Correct 37 ms 71620 KB Output is correct
12 Correct 108 ms 82872 KB Output is correct
13 Correct 104 ms 82800 KB Output is correct
14 Correct 123 ms 82784 KB Output is correct
15 Correct 105 ms 82740 KB Output is correct
16 Correct 179 ms 83132 KB Output is correct
17 Correct 107 ms 82812 KB Output is correct
18 Correct 106 ms 82716 KB Output is correct
19 Correct 40 ms 71492 KB Output is correct
20 Correct 35 ms 71424 KB Output is correct
21 Correct 40 ms 71464 KB Output is correct
22 Correct 81 ms 75204 KB Output is correct
23 Correct 108 ms 83328 KB Output is correct
24 Correct 109 ms 82824 KB Output is correct
25 Correct 129 ms 82888 KB Output is correct
26 Correct 39 ms 71748 KB Output is correct
27 Correct 110 ms 82844 KB Output is correct
28 Correct 122 ms 82796 KB Output is correct
29 Correct 107 ms 82704 KB Output is correct
30 Correct 106 ms 82664 KB Output is correct
31 Correct 114 ms 83044 KB Output is correct
32 Correct 108 ms 82744 KB Output is correct
33 Correct 104 ms 82756 KB Output is correct
34 Correct 1575 ms 227640 KB Output is correct
35 Correct 1632 ms 224244 KB Output is correct
36 Correct 1633 ms 224288 KB Output is correct
37 Correct 1449 ms 228804 KB Output is correct
38 Correct 1537 ms 226108 KB Output is correct
39 Correct 1601 ms 222780 KB Output is correct
40 Correct 1584 ms 222764 KB Output is correct
41 Correct 1569 ms 221884 KB Output is correct
42 Correct 1713 ms 225324 KB Output is correct
43 Correct 1577 ms 232576 KB Output is correct
44 Correct 1591 ms 232580 KB Output is correct
45 Correct 1637 ms 226916 KB Output is correct
46 Correct 1516 ms 225212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 75204 KB Output is correct
2 Correct 109 ms 83420 KB Output is correct
3 Correct 109 ms 82780 KB Output is correct
4 Correct 129 ms 82936 KB Output is correct
5 Correct 41 ms 71648 KB Output is correct
6 Correct 109 ms 82788 KB Output is correct
7 Correct 75 ms 75204 KB Output is correct
8 Correct 107 ms 83408 KB Output is correct
9 Correct 109 ms 82832 KB Output is correct
10 Correct 126 ms 82912 KB Output is correct
11 Correct 37 ms 71620 KB Output is correct
12 Correct 108 ms 82872 KB Output is correct
13 Correct 104 ms 82800 KB Output is correct
14 Correct 123 ms 82784 KB Output is correct
15 Correct 105 ms 82740 KB Output is correct
16 Correct 179 ms 83132 KB Output is correct
17 Correct 107 ms 82812 KB Output is correct
18 Correct 106 ms 82716 KB Output is correct
19 Correct 81 ms 75188 KB Output is correct
20 Correct 114 ms 83344 KB Output is correct
21 Correct 108 ms 82852 KB Output is correct
22 Correct 143 ms 82872 KB Output is correct
23 Correct 40 ms 71664 KB Output is correct
24 Correct 112 ms 83000 KB Output is correct
25 Correct 104 ms 82740 KB Output is correct
26 Correct 104 ms 82720 KB Output is correct
27 Correct 101 ms 82588 KB Output is correct
28 Correct 112 ms 83032 KB Output is correct
29 Correct 118 ms 82744 KB Output is correct
30 Correct 106 ms 82728 KB Output is correct
31 Correct 1600 ms 225404 KB Output is correct
32 Correct 1573 ms 231364 KB Output is correct
33 Correct 1441 ms 224516 KB Output is correct
34 Correct 1361 ms 227736 KB Output is correct
35 Correct 1625 ms 221116 KB Output is correct
36 Correct 1680 ms 223856 KB Output is correct
37 Correct 1600 ms 233236 KB Output is correct
38 Correct 40 ms 71492 KB Output is correct
39 Correct 35 ms 71424 KB Output is correct
40 Correct 40 ms 71464 KB Output is correct
41 Correct 81 ms 75204 KB Output is correct
42 Correct 108 ms 83328 KB Output is correct
43 Correct 109 ms 82824 KB Output is correct
44 Correct 129 ms 82888 KB Output is correct
45 Correct 39 ms 71748 KB Output is correct
46 Correct 110 ms 82844 KB Output is correct
47 Correct 122 ms 82796 KB Output is correct
48 Correct 107 ms 82704 KB Output is correct
49 Correct 106 ms 82664 KB Output is correct
50 Correct 114 ms 83044 KB Output is correct
51 Correct 108 ms 82744 KB Output is correct
52 Correct 104 ms 82756 KB Output is correct
53 Correct 1575 ms 227640 KB Output is correct
54 Correct 1632 ms 224244 KB Output is correct
55 Correct 1633 ms 224288 KB Output is correct
56 Correct 1449 ms 228804 KB Output is correct
57 Correct 1537 ms 226108 KB Output is correct
58 Correct 1601 ms 222780 KB Output is correct
59 Correct 1584 ms 222764 KB Output is correct
60 Correct 1569 ms 221884 KB Output is correct
61 Correct 1713 ms 225324 KB Output is correct
62 Correct 1577 ms 232576 KB Output is correct
63 Correct 1591 ms 232580 KB Output is correct
64 Correct 1637 ms 226916 KB Output is correct
65 Correct 1516 ms 225212 KB Output is correct
66 Correct 36 ms 71516 KB Output is correct
67 Correct 35 ms 71404 KB Output is correct
68 Correct 41 ms 71460 KB Output is correct
69 Correct 78 ms 75828 KB Output is correct
70 Correct 113 ms 84236 KB Output is correct
71 Correct 110 ms 83596 KB Output is correct
72 Correct 125 ms 83740 KB Output is correct
73 Correct 37 ms 71620 KB Output is correct
74 Correct 113 ms 83784 KB Output is correct
75 Correct 105 ms 83516 KB Output is correct
76 Correct 109 ms 83568 KB Output is correct
77 Correct 103 ms 83444 KB Output is correct
78 Correct 110 ms 83884 KB Output is correct
79 Correct 107 ms 83544 KB Output is correct
80 Correct 112 ms 83540 KB Output is correct
81 Correct 1537 ms 225420 KB Output is correct
82 Correct 1491 ms 231300 KB Output is correct
83 Correct 1465 ms 224488 KB Output is correct
84 Correct 1333 ms 227920 KB Output is correct
85 Correct 1621 ms 221108 KB Output is correct
86 Correct 1668 ms 224044 KB Output is correct
87 Correct 1586 ms 233100 KB Output is correct
88 Correct 1555 ms 227592 KB Output is correct
89 Correct 1588 ms 224428 KB Output is correct
90 Correct 1642 ms 224372 KB Output is correct
91 Correct 1480 ms 228784 KB Output is correct
92 Correct 1534 ms 225324 KB Output is correct
93 Correct 1561 ms 222800 KB Output is correct
94 Correct 1692 ms 223636 KB Output is correct
95 Correct 1585 ms 221908 KB Output is correct
96 Correct 1540 ms 225744 KB Output is correct
97 Correct 1585 ms 232624 KB Output is correct
98 Correct 1566 ms 232608 KB Output is correct
99 Correct 1644 ms 227092 KB Output is correct
100 Correct 1569 ms 225524 KB Output is correct
101 Correct 1603 ms 228512 KB Output is correct
102 Correct 1497 ms 228944 KB Output is correct
103 Correct 1581 ms 228324 KB Output is correct
104 Correct 1679 ms 226120 KB Output is correct
105 Correct 1639 ms 225576 KB Output is correct
106 Correct 1655 ms 226960 KB Output is correct
107 Correct 1549 ms 225436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 75204 KB Output is correct
2 Correct 109 ms 83420 KB Output is correct
3 Correct 109 ms 82780 KB Output is correct
4 Correct 129 ms 82936 KB Output is correct
5 Correct 41 ms 71648 KB Output is correct
6 Correct 109 ms 82788 KB Output is correct
7 Correct 75 ms 75204 KB Output is correct
8 Correct 107 ms 83408 KB Output is correct
9 Correct 109 ms 82832 KB Output is correct
10 Correct 126 ms 82912 KB Output is correct
11 Correct 37 ms 71620 KB Output is correct
12 Correct 108 ms 82872 KB Output is correct
13 Correct 104 ms 82800 KB Output is correct
14 Correct 123 ms 82784 KB Output is correct
15 Correct 105 ms 82740 KB Output is correct
16 Correct 179 ms 83132 KB Output is correct
17 Correct 107 ms 82812 KB Output is correct
18 Correct 106 ms 82716 KB Output is correct
19 Correct 81 ms 75188 KB Output is correct
20 Correct 114 ms 83344 KB Output is correct
21 Correct 108 ms 82852 KB Output is correct
22 Correct 143 ms 82872 KB Output is correct
23 Correct 40 ms 71664 KB Output is correct
24 Correct 112 ms 83000 KB Output is correct
25 Correct 104 ms 82740 KB Output is correct
26 Correct 104 ms 82720 KB Output is correct
27 Correct 101 ms 82588 KB Output is correct
28 Correct 112 ms 83032 KB Output is correct
29 Correct 118 ms 82744 KB Output is correct
30 Correct 106 ms 82728 KB Output is correct
31 Correct 1600 ms 225404 KB Output is correct
32 Correct 1573 ms 231364 KB Output is correct
33 Correct 1441 ms 224516 KB Output is correct
34 Correct 1361 ms 227736 KB Output is correct
35 Correct 1625 ms 221116 KB Output is correct
36 Correct 1680 ms 223856 KB Output is correct
37 Correct 1600 ms 233236 KB Output is correct
38 Correct 76 ms 75192 KB Output is correct
39 Correct 115 ms 83368 KB Output is correct
40 Correct 107 ms 82812 KB Output is correct
41 Correct 123 ms 82872 KB Output is correct
42 Correct 38 ms 71552 KB Output is correct
43 Correct 109 ms 82872 KB Output is correct
44 Correct 103 ms 82752 KB Output is correct
45 Correct 120 ms 82756 KB Output is correct
46 Correct 108 ms 82592 KB Output is correct
47 Correct 111 ms 83080 KB Output is correct
48 Correct 111 ms 82876 KB Output is correct
49 Correct 108 ms 82720 KB Output is correct
50 Correct 1569 ms 225512 KB Output is correct
51 Correct 1503 ms 231344 KB Output is correct
52 Correct 1464 ms 224552 KB Output is correct
53 Correct 1351 ms 227928 KB Output is correct
54 Correct 1630 ms 221220 KB Output is correct
55 Correct 1663 ms 223896 KB Output is correct
56 Correct 1620 ms 233232 KB Output is correct
57 Correct 2099 ms 242640 KB Output is correct
58 Correct 3862 ms 331756 KB Output is correct
59 Correct 3617 ms 314656 KB Output is correct
60 Correct 2619 ms 228836 KB Output is correct
61 Execution timed out 6072 ms 246508 KB Time limit exceeded
62 Halted 0 ms 0 KB -