답안 #767635

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
767635 2023-06-27T00:40:28 Z radoslav11 통행료 (IOI18_highway) C++17
100 / 100
209 ms 13844 KB
#include <bits/stdc++.h>
#include "highway.h"

using namespace std;

tuple<vector<int>, vector<int>, vector<int>> bfs(
	int N, int M, vector<vector<pair<int, int>>> &adj, vector<int> srcs
) {
	vector<int> dist(N, -1);
	queue<int> q;

	for(int u: srcs) {
		dist[u] = 0;
		q.push(u);
	}

	vector<int> tree_edges;
	vector<int> cross_edges_mask(M, true);

	while(!q.empty()) {
		int u = q.front();
		q.pop();

		for(auto [v, i]: adj[u]) {
			if(dist[v] == -1) {
				dist[v] = dist[u] + 1;
				cross_edges_mask[i] = false;
				tree_edges.push_back(i);
				q.push(v);
			}
		}
	}

	vector<int> cross_edges;
	for(int i = 0; i < M; i++) {
		if(cross_edges_mask[i]) {
			cross_edges.push_back(i);
		}
	}

	return {dist, tree_edges, cross_edges};
}

int find_first(
	vector<int> edges, vector<int> order, int l, int r, int m, int64_t dist_cmp,
	function<bool(int, int)> cmp
) {
	int found = -1;
	while(l <= r) {
		int mid = (l + r) >> 1;
		vector<int> mask(m, 1);

		for(int i = 0; i <= mid; i++) {
			if(edges[order[i]] != -1) {
				mask[edges[order[i]]] = 0;
			}
		}

		int64_t val = ask(mask);
		if(cmp(val, dist_cmp)) {
			r = mid - 1;
			if(val == dist_cmp) {
				found = order[mid];
			}
		} else {
			l = mid + 1;
		}
	}

	return found;
}

int find_first2(
	vector<int> edges, vector<int> order, int l, int r, int m, int64_t dist_cmp,
	function<bool(int, int)> cmp
) {
	int found = -1, start = l;
	while(l <= r) {
		int mid = (l + r) >> 1;
		vector<int> mask(m, 0);

		for(int i = start; i < mid; i++) {
			if(edges[order[i]] != -1) {
				mask[edges[order[i]]] = 1;
			}
		}

		int64_t val = ask(mask);
		if(cmp(val, dist_cmp)) {
			r = mid - 1;
			found = mid - 1;
		} else {
			l = mid + 1;
		}
	}

	return found;
}


void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B) {
	vector<vector<pair<int, int>>> adj;
	adj.assign(N, {});

	for(int i = 0; i < (int)U.size(); i++) {
		adj[U[i]].push_back({V[i], i});
		adj[V[i]].push_back({U[i], i});
	}

	vector<int> dist, tree_edges, cross_edges;
	tie(dist, tree_edges, cross_edges) = bfs(N, U.size(), adj, {0});

	// 1 query
	int64_t dist_heavy = ask(vector<int>(U.size(), 1));	
	int64_t dist_light = dist_heavy / B * A;

	// ceil(log2(M - N)) queries. Worst case 16 queries

	vector<int> order(cross_edges.size());
	iota(order.begin(), order.end(), 0);
	int found = find_first2(
		cross_edges, order, 0, (int)cross_edges.size(), U.size(),
		dist_light,
		[](int64_t val, int64_t dist) { return val != dist; }
	);

	if(found != -1) {
		// reroot needed to guarantee that shortest path is on bfs tree
		int cross_edge = cross_edges[found];
		cerr << U[cross_edge] << " " << V[cross_edge] << endl;
		tie(dist, tree_edges, cross_edges) =
			bfs(N, U.size(), adj, {U[cross_edge], V[cross_edge]});

		tree_edges.push_back(cross_edge);
	}

	vector<int> main_edge(N, -1);
	for(auto i: tree_edges) {
		int u = U[i], v = V[i];
		if(dist[u] > dist[v]) {
			swap(u, v);
		}
		main_edge[v] = i;
	}

	int root = -1;
	for(int i = 0; i < N; i++) {
		if(main_edge[i] == -1) {
			root = i;
		}
	}

	vector<int> order_s(N);
	iota(order_s.begin(), order_s.end(), 0);
	sort(order_s.begin(), order_s.end(), [&](int i, int j) {
		return dist[i] < dist[j];
	});

	int s = find_first(
		main_edge, order_s, 0, (int)order_s.size() - 1, U.size(), dist_light,
		[](int64_t val, int64_t dist) { return val == dist; }
	);

	if(s == -1) {
		s = root;
	}
	tie(dist, tree_edges, cross_edges) = bfs(N, U.size(), adj, {s});

	vector<int> order_t(N);
	iota(order_t.begin(), order_t.end(), 0);
	sort(order_t.begin(), order_t.end(), [&](int u, int v) {
		return dist[u] < dist[v];
	});

	main_edge.assign(N, -1);
	for(auto i: tree_edges) {
		int u = U[i], v = V[i];
		if(dist[u] > dist[v]) {
			swap(u, v);
		}
		main_edge[v] = i;
	}

	int l = -1;
	int r = 0;
	for(int i = 0; i < N; i++) {
		if(dist[order_t[i]] == dist_heavy / B) {
			if(l == -1) {
				l = i;
			}
			r = i;
		}
	}

	int t = find_first(
		main_edge, order_t, l, r, U.size(), dist_light,
		[](int64_t val, int64_t dist) { return val == dist; }
	);

	answer(s, t);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 0 ms 208 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 2 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 1 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 13 ms 1464 KB Output is correct
3 Correct 86 ms 10504 KB Output is correct
4 Correct 92 ms 10468 KB Output is correct
5 Correct 132 ms 10520 KB Output is correct
6 Correct 96 ms 10472 KB Output is correct
7 Correct 102 ms 10472 KB Output is correct
8 Correct 123 ms 10496 KB Output is correct
9 Correct 98 ms 10540 KB Output is correct
10 Correct 132 ms 10512 KB Output is correct
11 Correct 85 ms 9964 KB Output is correct
12 Correct 100 ms 9956 KB Output is correct
13 Correct 98 ms 9880 KB Output is correct
14 Correct 133 ms 9908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 1400 KB Output is correct
2 Correct 15 ms 2468 KB Output is correct
3 Correct 26 ms 3492 KB Output is correct
4 Correct 92 ms 10092 KB Output is correct
5 Correct 66 ms 9892 KB Output is correct
6 Correct 63 ms 9920 KB Output is correct
7 Correct 79 ms 9844 KB Output is correct
8 Correct 60 ms 9880 KB Output is correct
9 Correct 105 ms 9904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 15 ms 1400 KB Output is correct
3 Correct 78 ms 8392 KB Output is correct
4 Correct 120 ms 10536 KB Output is correct
5 Correct 104 ms 10544 KB Output is correct
6 Correct 103 ms 10540 KB Output is correct
7 Correct 94 ms 10432 KB Output is correct
8 Correct 83 ms 10432 KB Output is correct
9 Correct 112 ms 10488 KB Output is correct
10 Correct 94 ms 10440 KB Output is correct
11 Correct 114 ms 9948 KB Output is correct
12 Correct 94 ms 9896 KB Output is correct
13 Correct 130 ms 9952 KB Output is correct
14 Correct 105 ms 9964 KB Output is correct
15 Correct 97 ms 10500 KB Output is correct
16 Correct 102 ms 10592 KB Output is correct
17 Correct 100 ms 9856 KB Output is correct
18 Correct 119 ms 9916 KB Output is correct
19 Correct 90 ms 10456 KB Output is correct
20 Correct 86 ms 9880 KB Output is correct
21 Correct 101 ms 11120 KB Output is correct
22 Correct 100 ms 11124 KB Output is correct
23 Correct 115 ms 10644 KB Output is correct
24 Correct 128 ms 10564 KB Output is correct
25 Correct 125 ms 10028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 1524 KB Output is correct
2 Correct 16 ms 1548 KB Output is correct
3 Correct 100 ms 11952 KB Output is correct
4 Correct 125 ms 12384 KB Output is correct
5 Correct 155 ms 13544 KB Output is correct
6 Correct 162 ms 13568 KB Output is correct
7 Correct 130 ms 13536 KB Output is correct
8 Correct 196 ms 13564 KB Output is correct
9 Correct 113 ms 9124 KB Output is correct
10 Correct 148 ms 9604 KB Output is correct
11 Correct 116 ms 10424 KB Output is correct
12 Correct 166 ms 12276 KB Output is correct
13 Correct 127 ms 13064 KB Output is correct
14 Correct 186 ms 13540 KB Output is correct
15 Correct 134 ms 13564 KB Output is correct
16 Correct 122 ms 10928 KB Output is correct
17 Correct 86 ms 11036 KB Output is correct
18 Correct 79 ms 11320 KB Output is correct
19 Correct 103 ms 11312 KB Output is correct
20 Correct 101 ms 11236 KB Output is correct
21 Correct 171 ms 13768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 1616 KB Output is correct
2 Correct 12 ms 1656 KB Output is correct
3 Correct 92 ms 11864 KB Output is correct
4 Correct 150 ms 12060 KB Output is correct
5 Correct 136 ms 12372 KB Output is correct
6 Correct 209 ms 13524 KB Output is correct
7 Correct 130 ms 11856 KB Output is correct
8 Correct 127 ms 12012 KB Output is correct
9 Correct 118 ms 12392 KB Output is correct
10 Correct 174 ms 13544 KB Output is correct
11 Correct 196 ms 13540 KB Output is correct
12 Correct 170 ms 13524 KB Output is correct
13 Correct 135 ms 10324 KB Output is correct
14 Correct 97 ms 9440 KB Output is correct
15 Correct 100 ms 10600 KB Output is correct
16 Correct 96 ms 9560 KB Output is correct
17 Correct 116 ms 10432 KB Output is correct
18 Correct 106 ms 9620 KB Output is correct
19 Correct 145 ms 12340 KB Output is correct
20 Correct 137 ms 12944 KB Output is correct
21 Correct 180 ms 13600 KB Output is correct
22 Correct 154 ms 13548 KB Output is correct
23 Correct 195 ms 13676 KB Output is correct
24 Correct 185 ms 13592 KB Output is correct
25 Correct 141 ms 13548 KB Output is correct
26 Correct 168 ms 13560 KB Output is correct
27 Correct 77 ms 11156 KB Output is correct
28 Correct 82 ms 11168 KB Output is correct
29 Correct 140 ms 11292 KB Output is correct
30 Correct 118 ms 10944 KB Output is correct
31 Correct 93 ms 11200 KB Output is correct
32 Correct 87 ms 11148 KB Output is correct
33 Correct 134 ms 11488 KB Output is correct
34 Correct 114 ms 11128 KB Output is correct
35 Correct 79 ms 11140 KB Output is correct
36 Correct 120 ms 11152 KB Output is correct
37 Correct 99 ms 11356 KB Output is correct
38 Correct 88 ms 11064 KB Output is correct
39 Correct 181 ms 13844 KB Output is correct
40 Correct 158 ms 13776 KB Output is correct
41 Correct 196 ms 13760 KB Output is correct
42 Correct 193 ms 13076 KB Output is correct