Submission #804729

# Submission time Handle Problem Language Result Execution time Memory
804729 2023-08-03T11:04:14 Z IvanJ Highway Tolls (IOI18_highway) C++17
100 / 100
232 ms 15388 KB
#include "highway.h"
#include<bits/stdc++.h>

#define pb push_back
#define x first
#define y second
#define all(a) (a).begin(), (a).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 2e5 + 5;

int n, m, len, A, B, pos;
vector<ii> E;
int dist[2][maxn];
int vis[maxn];
vector<ii> adj[maxn];

int get_len() {
	vector<int> w(m, 0);
	ll l = ask(w);
	return (l / A);
}

void bfs(int x, int f) {
	for(int i = 0;i < n;i++) 
		dist[f][i] = 1e9;
	dist[f][x] = 0;
	queue<int> q;
	q.push(x);
	while(q.size()) {
		x = q.front();q.pop();
		for(ii p : adj[x]) {
			if(dist[f][p.x] != 1e9) continue;
			dist[f][p.x] = dist[f][x] + 1;
			q.push(p.x);
		}
	}
}

vector<int> get_tree(int X, int f) {
	queue<int> q;
	q.push(X), vis[X] = 1;
	vector<int> ret;
	while(q.size()) {
		int x = q.front();q.pop();
		for(ii p : adj[x]) {
			if(dist[f][p.x] >= dist[!f][p.x]) continue;
			if(vis[p.x]) continue;
			vis[p.x] = 1;
			ret.pb(p.y), q.push(p.x);
		}
	}
	return ret;
}

int get_first() {
	int lo = 0, hi = m - 1, ans = -1;
	while(lo <= hi) {
		int mid = (lo + hi) / 2;
		vector<int> w(m, 0);
		for(int i = 0;i <= mid;i++) w[i] = 1;
		ll ret = ask(w);
		if(ret == (ll)len * A) lo = mid + 1;
		else ans = mid, hi = mid - 1;
	}
	return ans;
}

int get_last(vector<int> a, vector<int> b) {
	if(a.size() == 0) return -1;
	int lo = -1, hi = (int)a.size() - 1, ans = -1;
	while(lo <= hi) {
		int mid = (lo + hi) / 2;
		vector<int> w(m, 1);
		for(int i = 0;i <= mid;i++) w[a[i]] = 0;
		for(int i : b) w[i] = 0;
		w[pos] = 0;
		ll ret = ask(w);
		if(ret == (ll)len * A) 
			ans = mid, hi = mid - 1;
		else lo = mid + 1;
	}
	if(ans == -1) return -1;
	return a[ans];
}

void find_pair(int N, vector<int> U, vector<int> V, int A, int B) {
	::A = A, ::B = B;
	n = N, m = (int)U.size();
	for(int i = 0;i < m;i++) {
		E.pb({U[i], V[i]});
		adj[U[i]].pb({V[i], i}), adj[V[i]].pb({U[i], i});
	}
	len = get_len();
	
	pos = get_first();
	ii e = E[pos];
	//cout << e.x << " " << e.y << " first\n";
	bfs(e.x, 0);
	bfs(e.y, 1);
	
	vector<int> a = get_tree(e.x, 0);
	vector<int> b = get_tree(e.y, 1);
	//for(int i : a) cout << E[i].x << " " << E[i].y << " a\n";
	//for(int i : b) cout << E[i].x << " " << E[i].y << " b\n";
	int x = get_last(a, b);
	int y = get_last(b, a);
	//cout << x << " " << y << "\n";
	
	int s, t;
	if(x == -1) s = e.x;
	else {
		if(dist[0][E[x].x] > dist[0][E[x].y])
			s = E[x].x;
		else s = E[x].y;
	}
	
	if(y == -1) t = e.y;
	else {
		if(dist[1][E[y].x] > dist[1][E[y].y])
			t = E[y].x;
		else t = E[y].y;
	}
	answer(s, t);
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 3 ms 5012 KB Output is correct
4 Correct 4 ms 5016 KB Output is correct
5 Correct 3 ms 4944 KB Output is correct
6 Correct 3 ms 4944 KB Output is correct
7 Correct 2 ms 5024 KB Output is correct
8 Correct 4 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 4 ms 4948 KB Output is correct
11 Correct 4 ms 5020 KB Output is correct
12 Correct 3 ms 5016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5080 KB Output is correct
2 Correct 14 ms 5852 KB Output is correct
3 Correct 89 ms 13220 KB Output is correct
4 Correct 101 ms 13208 KB Output is correct
5 Correct 123 ms 13220 KB Output is correct
6 Correct 98 ms 13220 KB Output is correct
7 Correct 115 ms 13216 KB Output is correct
8 Correct 134 ms 13220 KB Output is correct
9 Correct 108 ms 13256 KB Output is correct
10 Correct 107 ms 13232 KB Output is correct
11 Correct 129 ms 12276 KB Output is correct
12 Correct 150 ms 12252 KB Output is correct
13 Correct 102 ms 12664 KB Output is correct
14 Correct 91 ms 12312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 5740 KB Output is correct
2 Correct 22 ms 6660 KB Output is correct
3 Correct 29 ms 7416 KB Output is correct
4 Correct 81 ms 12284 KB Output is correct
5 Correct 83 ms 12284 KB Output is correct
6 Correct 93 ms 12508 KB Output is correct
7 Correct 78 ms 12300 KB Output is correct
8 Correct 90 ms 12328 KB Output is correct
9 Correct 84 ms 12240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 14 ms 5900 KB Output is correct
3 Correct 78 ms 11316 KB Output is correct
4 Correct 103 ms 13212 KB Output is correct
5 Correct 122 ms 13216 KB Output is correct
6 Correct 108 ms 13228 KB Output is correct
7 Correct 141 ms 13228 KB Output is correct
8 Correct 106 ms 13216 KB Output is correct
9 Correct 108 ms 13168 KB Output is correct
10 Correct 136 ms 13216 KB Output is correct
11 Correct 120 ms 12312 KB Output is correct
12 Correct 110 ms 12584 KB Output is correct
13 Correct 100 ms 12244 KB Output is correct
14 Correct 106 ms 12172 KB Output is correct
15 Correct 104 ms 13236 KB Output is correct
16 Correct 87 ms 13232 KB Output is correct
17 Correct 105 ms 12284 KB Output is correct
18 Correct 87 ms 12312 KB Output is correct
19 Correct 98 ms 13228 KB Output is correct
20 Correct 106 ms 12624 KB Output is correct
21 Correct 83 ms 13388 KB Output is correct
22 Correct 103 ms 13384 KB Output is correct
23 Correct 102 ms 13128 KB Output is correct
24 Correct 123 ms 13000 KB Output is correct
25 Correct 122 ms 12392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 5852 KB Output is correct
2 Correct 19 ms 5888 KB Output is correct
3 Correct 107 ms 13648 KB Output is correct
4 Correct 177 ms 13752 KB Output is correct
5 Correct 199 ms 15016 KB Output is correct
6 Correct 232 ms 14728 KB Output is correct
7 Correct 150 ms 14876 KB Output is correct
8 Correct 147 ms 14768 KB Output is correct
9 Correct 108 ms 12344 KB Output is correct
10 Correct 94 ms 12904 KB Output is correct
11 Correct 137 ms 13352 KB Output is correct
12 Correct 157 ms 14140 KB Output is correct
13 Correct 125 ms 14488 KB Output is correct
14 Correct 164 ms 14644 KB Output is correct
15 Correct 156 ms 14724 KB Output is correct
16 Correct 157 ms 13312 KB Output is correct
17 Correct 93 ms 13072 KB Output is correct
18 Correct 123 ms 13216 KB Output is correct
19 Correct 87 ms 13216 KB Output is correct
20 Correct 96 ms 13640 KB Output is correct
21 Correct 175 ms 15104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 5840 KB Output is correct
2 Correct 14 ms 5988 KB Output is correct
3 Correct 140 ms 13280 KB Output is correct
4 Correct 105 ms 13564 KB Output is correct
5 Correct 141 ms 13796 KB Output is correct
6 Correct 133 ms 14736 KB Output is correct
7 Correct 146 ms 13740 KB Output is correct
8 Correct 138 ms 13884 KB Output is correct
9 Correct 157 ms 13604 KB Output is correct
10 Correct 197 ms 14844 KB Output is correct
11 Correct 186 ms 14720 KB Output is correct
12 Correct 152 ms 14760 KB Output is correct
13 Correct 118 ms 13368 KB Output is correct
14 Correct 118 ms 12908 KB Output is correct
15 Correct 124 ms 13396 KB Output is correct
16 Correct 98 ms 12892 KB Output is correct
17 Correct 121 ms 13340 KB Output is correct
18 Correct 129 ms 12900 KB Output is correct
19 Correct 136 ms 14148 KB Output is correct
20 Correct 138 ms 14488 KB Output is correct
21 Correct 173 ms 14668 KB Output is correct
22 Correct 185 ms 14800 KB Output is correct
23 Correct 165 ms 14868 KB Output is correct
24 Correct 144 ms 14816 KB Output is correct
25 Correct 124 ms 15052 KB Output is correct
26 Correct 169 ms 14896 KB Output is correct
27 Correct 98 ms 13236 KB Output is correct
28 Correct 124 ms 13492 KB Output is correct
29 Correct 76 ms 13400 KB Output is correct
30 Correct 105 ms 13628 KB Output is correct
31 Correct 103 ms 13192 KB Output is correct
32 Correct 97 ms 13156 KB Output is correct
33 Correct 97 ms 13768 KB Output is correct
34 Correct 79 ms 13176 KB Output is correct
35 Correct 109 ms 13200 KB Output is correct
36 Correct 92 ms 13452 KB Output is correct
37 Correct 113 ms 13312 KB Output is correct
38 Correct 77 ms 13340 KB Output is correct
39 Correct 188 ms 15388 KB Output is correct
40 Correct 143 ms 15120 KB Output is correct
41 Correct 149 ms 15144 KB Output is correct
42 Correct 158 ms 14796 KB Output is correct