Submission #818349

# Submission time Handle Problem Language Result Execution time Memory
818349 2023-08-10T04:27:21 Z pavement Highway Tolls (IOI18_highway) C++17
51 / 100
133 ms 14572 KB
#include "highway.h"
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define eb emplace_back
#define mt make_tuple

using ll = long long;
using ii = pair<int, int>;

vector<int> distU, distV, prvU, prvV;
vector<vector<ii> > adj;
queue<int> Q;

void bfs(int s, vector<int> &dist, vector<int> &prv) {
	for (int i = 0; i < (int)dist.size(); i++) {
		dist[i] = (int)1e9;
	}
	dist[s] = 0;
	Q.push(s);
	while (!Q.empty()) {
		int u = Q.front();
		Q.pop();
		for (auto [v, idx] : adj[u]) {
			if (dist[v] > dist[u] + 1) {
				dist[v] = dist[u] + 1;
				prv[v] = idx;
				Q.push(v);
			}
		}
	}
}

void find_pair(int N, vector<int> U, vector<int> V, int A, int B) {
	distU.resize(N);
	distV.resize(N);
	prvU.resize(N);
	prvV.resize(N);
	adj.resize(N);
	int M = (int)U.size(), S, T;
	for (int i = 0; i < M; i++) {
		adj[U[i]].eb(V[i], i);
		adj[V[i]].eb(U[i], i);
	}
	ll original = ask(vector<int>(M, 0));
	int lo = 0, hi = M - 1, first_edge = -1;
	while (lo <= hi) {
		int mid = (lo + hi) / 2;
		vector<int> w(M, 0);
		for (int i = 0; i <= mid; i++) {
			w[i] = 1;
		}
		if (ask(w) != original) {
			first_edge = mid;
			hi = mid - 1;
		} else {
			lo = mid + 1;
		}
	}
	assert(first_edge != -1);
	//~ cout << "FE " << first_edge << endl;
	bfs(U[first_edge], distU, prvU);
	bfs(V[first_edge], distV, prvV);
	vector<int> sU, sV, treeU, treeV;
	for (int i = 0; i < N; i++) {
		if (i == U[first_edge] || i == V[first_edge]) continue;
		if (distU[i] < distV[i]) {
			sU.pb(i);
			int prv_node = U[prvU[i]] ^ V[prvU[i]] ^ i;
			if (distU[prv_node] < distV[prv_node]) {
				treeU.pb(prvU[i]);
				//~ cout << "TU " << i << ' ' << prv_node << '\n';
			}
		} else {
			sV.pb(i);
			int prv_node = U[prvV[i]] ^ V[prvV[i]] ^ i;
			if (distU[prv_node] >= distV[prv_node]) {
				treeV.pb(prvV[i]);
				//~ cout << "TV " << i << ' ' << prv_node << '\n';
			}
		}
	}
	
	sort(treeU.begin(), treeU.end());
	assert(unique(treeU.begin(), treeU.end()) == treeU.end());
	sort(treeV.begin(), treeV.end());
	assert(unique(treeV.begin(), treeV.end()) == treeV.end());
	
	vector<int> w(M, 1);
	for (int i : treeV) {
		w[i] = 0;
	}
	w[first_edge] = 0;
	ll tmp = ask(w);
	int U_len = (tmp - original) / (B - A);
	int V_len = original / A - U_len - 1;
	
	vector<int> o_w(M, 1);
	for (int i : treeU) {
		o_w[i] = 0;
	}
	for (int i : treeV) {
		o_w[i] = 0;
	}
	o_w[first_edge] = 0;
	
	for (auto [dist, tree, prv, len, st, R] : {mt(distU, treeU, prvU, U_len, U[first_edge], &S), mt(distV, treeV, prvV, V_len, V[first_edge], &T)}) {
		vector<int> poss;
		if (len == 0 && tree.empty()) {
			poss.pb(st);
		}
		for (int i : tree) {
			if (dist[U[i]] == len) {
				poss.pb(U[i]);
			}
			if (dist[V[i]] == len) {
				poss.pb(V[i]);
			}
		}
		assert(!poss.empty());
		sort(poss.begin(), poss.end());
		poss.erase(unique(poss.begin(), poss.end()), poss.end());
		//~ for (int i : poss) {
			//~ cout << i << ' ';
		//~ }
		//~ cout << '\n';
		while ((int)poss.size() > 1) {
			vector<int> w = o_w;
			for (int i = 0; i < (int)poss.size() / 2; i++) {
				w[prv[poss[i]]] = 1;
			}
			if (ask(w) > original) {
				int new_sz = (int)poss.size() / 2;
				while ((int)poss.size() != new_sz) poss.pop_back();
			} else {
				int new_sz = ((int)poss.size() + 1) / 2;
				reverse(poss.begin(), poss.end());
				while ((int)poss.size() != new_sz) poss.pop_back();
				reverse(poss.begin(), poss.end());
			}
		}
		*R = poss[0];
	}
	answer(S, T);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 0 ms 208 KB Output is correct
7 Correct 0 ms 208 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 1 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 8 ms 1792 KB Output is correct
3 Correct 81 ms 13500 KB Output is correct
4 Correct 102 ms 13540 KB Output is correct
5 Correct 108 ms 13540 KB Output is correct
6 Correct 119 ms 13428 KB Output is correct
7 Correct 104 ms 13632 KB Output is correct
8 Correct 83 ms 13552 KB Output is correct
9 Correct 75 ms 13536 KB Output is correct
10 Correct 120 ms 13496 KB Output is correct
11 Correct 90 ms 12592 KB Output is correct
12 Correct 111 ms 12736 KB Output is correct
13 Correct 121 ms 12704 KB Output is correct
14 Correct 108 ms 12804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1592 KB Output is correct
2 Correct 13 ms 2944 KB Output is correct
3 Correct 26 ms 4232 KB Output is correct
4 Correct 74 ms 12348 KB Output is correct
5 Correct 90 ms 12260 KB Output is correct
6 Correct 92 ms 12660 KB Output is correct
7 Correct 63 ms 12388 KB Output is correct
8 Correct 78 ms 12304 KB Output is correct
9 Correct 60 ms 12544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 12 ms 1744 KB Output is correct
3 Correct 73 ms 10592 KB Output is correct
4 Correct 80 ms 13472 KB Output is correct
5 Correct 87 ms 13292 KB Output is correct
6 Correct 101 ms 13292 KB Output is correct
7 Correct 86 ms 13204 KB Output is correct
8 Correct 75 ms 13532 KB Output is correct
9 Correct 116 ms 13592 KB Output is correct
10 Correct 88 ms 13624 KB Output is correct
11 Correct 78 ms 12740 KB Output is correct
12 Correct 98 ms 12676 KB Output is correct
13 Correct 104 ms 12856 KB Output is correct
14 Correct 113 ms 12460 KB Output is correct
15 Correct 82 ms 13484 KB Output is correct
16 Correct 98 ms 13124 KB Output is correct
17 Correct 92 ms 12792 KB Output is correct
18 Correct 88 ms 12736 KB Output is correct
19 Correct 93 ms 13484 KB Output is correct
20 Correct 73 ms 12668 KB Output is correct
21 Correct 79 ms 14572 KB Output is correct
22 Correct 77 ms 14508 KB Output is correct
23 Correct 94 ms 13768 KB Output is correct
24 Correct 100 ms 13704 KB Output is correct
25 Correct 86 ms 13020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 1720 KB Output is correct
2 Correct 12 ms 1872 KB Output is correct
3 Correct 133 ms 13960 KB Output is correct
4 Incorrect 129 ms 14184 KB Output is incorrect: {s, t} is wrong.
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 15 ms 1744 KB Output is incorrect: {s, t} is wrong.
2 Halted 0 ms 0 KB -