Submission #390721

# Submission time Handle Problem Language Result Execution time Memory
390721 2021-04-16T19:01:46 Z JimmyZJX Highway Tolls (IOI18_highway) C++14
90 / 100
313 ms 12076 KB
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cassert>
#include <tuple>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <unordered_set>
#include <unordered_map>

#include "highway.h"

using namespace std;

typedef long long LL;
typedef vector<int> Vi;
typedef vector<bool> Vb;
typedef vector<vector<int>> Vii;

#define forR(i, n) for (int i = 0; i < (n); i++)

#define MAXMN 130003

vector<vector<pair<int, int>>> nbs(MAXMN); // {nb, edgeNo}
vector<int> parent(MAXMN);
vector<int> parentEdge(MAXMN);
Vi edgeSorted;

int parentOf(int n1, int n2) {
	if (parent[n1] == n2) return n2;
	return n1;
}

int childOf(int n1, int n2) {
	if (parent[n1] == n2) return n1;
	return n2;
}

void build_tree(int root) {
	Vb vis(MAXMN); Vb edgeVis(MAXMN);
	edgeSorted.clear();
	queue<pair<int, int>> q; q.push({ root, 0 }); // nodeNo, level
	vis[root] = true;
	while (!q.empty()) {
		auto p = q.front(); q.pop();
		int x = p.first, level = p.second;
		for (auto nbP : nbs[x]) {
			int nb = nbP.first, nbEdge = nbP.second;
			if (!vis[nb]) {
				vis[nb] = true;
				q.push({ nb, level + 1 });
				parent[nb] = x; parentEdge[nb] = nbEdge;
				edgeSorted.push_back(nbEdge);
				edgeVis[nbEdge] = true;
			} else if (!edgeVis[nbEdge]) {
				edgeSorted.push_back(nbEdge);
				edgeVis[nbEdge] = true;
			}
		}
	}
}

void find_pair(int N, Vi U, Vi V, int A, int B) {
	int m = U.size();

	forR(i, m) {
		nbs[U[i]].push_back({ V[i], i });
		nbs[V[i]].push_back({ U[i], i });
	}

	build_tree(0);

	LL minCost = ask(Vi(m));
	LL dist = minCost / A;
	// search for range of edges
	int l = 0, r = m - 1;
	while (l < r) {
		int mid = (l + r) / 2;
		Vi ws(m);
		for (int i = 0; i <= mid; i++) ws[edgeSorted[i]] = 1;
		if (ask(ws) == minCost) {
			l = mid + 1;
		} else {
			r = mid;
		}
	}
	int leftEdge = edgeSorted[l];
	int rootNode = parentOf(U[leftEdge], V[leftEdge]);

	build_tree(rootNode);

	l = 0, r = m - 1;
	while (l < r) {
		int mid = (l + r) / 2;
		Vi ws(m);
		for (int i = mid + 1; i <= m - 1; i++) ws[edgeSorted[i]] = 1;
		if (ask(ws) == minCost) {
			r = mid;
		} else {
			l = mid + 1;
		}
	}
	int rightRank = l;
	int rightEdge = edgeSorted[l];

	int rightChild = childOf(U[rightEdge], V[rightEdge]);

	LL distR = 0; Vi REdges;
	for (int n = rightChild; n != rootNode; n = parent[n]) {
		REdges.push_back(parentEdge[n]);
		distR++;
	}
	LL distL = dist - distR;
	if (distL == 0) {
		answer(rootNode, rightChild);
		return;
	}

	l = 0, r = rightRank;
	while (l < r) {
		int mid = (l + r) / 2;
		Vi ws(m);
		for (int i = mid + 1; i <= m - 1; i++) ws[edgeSorted[i]] = 1;
		for (int e : REdges) ws[e] = 0;
		if (ask(ws) == minCost) {
			r = mid;
		} else {
			l = mid + 1;
		}
	}
	int edgeLeft = edgeSorted[l];
	int leftChild = childOf(U[edgeLeft], V[edgeLeft]);

	answer(leftChild, rightChild);
}


# Verdict Execution time Memory Grader output
1 Correct 4 ms 4424 KB Output is correct
2 Correct 4 ms 4424 KB Output is correct
3 Correct 3 ms 4424 KB Output is correct
4 Correct 4 ms 4424 KB Output is correct
5 Correct 3 ms 4424 KB Output is correct
6 Correct 4 ms 4424 KB Output is correct
7 Correct 3 ms 4424 KB Output is correct
8 Correct 4 ms 4424 KB Output is correct
9 Correct 4 ms 4304 KB Output is correct
10 Correct 3 ms 4424 KB Output is correct
11 Correct 3 ms 4424 KB Output is correct
12 Correct 3 ms 4416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4424 KB Output is correct
2 Correct 15 ms 4936 KB Output is correct
3 Correct 162 ms 9988 KB Output is correct
4 Correct 145 ms 10068 KB Output is correct
5 Correct 193 ms 9996 KB Output is correct
6 Correct 165 ms 10060 KB Output is correct
7 Correct 140 ms 10068 KB Output is correct
8 Correct 187 ms 10096 KB Output is correct
9 Correct 137 ms 10076 KB Output is correct
10 Correct 193 ms 9996 KB Output is correct
11 Correct 141 ms 9404 KB Output is correct
12 Correct 156 ms 9404 KB Output is correct
13 Correct 182 ms 9400 KB Output is correct
14 Correct 146 ms 9388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 4936 KB Output is correct
2 Correct 35 ms 5536 KB Output is correct
3 Correct 48 ms 6040 KB Output is correct
4 Correct 151 ms 9348 KB Output is correct
5 Correct 162 ms 9404 KB Output is correct
6 Correct 155 ms 9436 KB Output is correct
7 Correct 116 ms 9408 KB Output is correct
8 Correct 153 ms 9428 KB Output is correct
9 Correct 131 ms 9400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4424 KB Output is correct
2 Correct 17 ms 5052 KB Output is correct
3 Correct 133 ms 8752 KB Output is correct
4 Correct 150 ms 9972 KB Output is correct
5 Correct 184 ms 9984 KB Output is correct
6 Correct 166 ms 10152 KB Output is correct
7 Correct 143 ms 10076 KB Output is correct
8 Correct 201 ms 10108 KB Output is correct
9 Correct 194 ms 10036 KB Output is correct
10 Correct 207 ms 10044 KB Output is correct
11 Correct 190 ms 9516 KB Output is correct
12 Correct 162 ms 9560 KB Output is correct
13 Correct 147 ms 9448 KB Output is correct
14 Correct 184 ms 9384 KB Output is correct
15 Correct 188 ms 10000 KB Output is correct
16 Correct 188 ms 9996 KB Output is correct
17 Correct 182 ms 9544 KB Output is correct
18 Correct 204 ms 9540 KB Output is correct
19 Correct 161 ms 9992 KB Output is correct
20 Correct 146 ms 9440 KB Output is correct
21 Correct 168 ms 10492 KB Output is correct
22 Correct 164 ms 10496 KB Output is correct
23 Correct 188 ms 10524 KB Output is correct
24 Correct 182 ms 10408 KB Output is correct
25 Correct 202 ms 10116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 4968 KB Output is correct
2 Correct 21 ms 5188 KB Output is correct
3 Correct 180 ms 10512 KB Output is correct
4 Correct 188 ms 11028 KB Output is correct
5 Correct 219 ms 11996 KB Output is correct
6 Correct 237 ms 11964 KB Output is correct
7 Correct 258 ms 11972 KB Output is correct
8 Correct 233 ms 11964 KB Output is correct
9 Correct 233 ms 10604 KB Output is correct
10 Correct 171 ms 11052 KB Output is correct
11 Correct 267 ms 10960 KB Output is correct
12 Correct 268 ms 11820 KB Output is correct
13 Correct 301 ms 11880 KB Output is correct
14 Correct 267 ms 11956 KB Output is correct
15 Correct 308 ms 11952 KB Output is correct
16 Correct 257 ms 11448 KB Output is correct
17 Correct 180 ms 10440 KB Output is correct
18 Correct 182 ms 10648 KB Output is correct
19 Correct 184 ms 10520 KB Output is correct
20 Correct 188 ms 10936 KB Output is correct
21 Correct 220 ms 11884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 4964 KB Output is correct
2 Correct 28 ms 5104 KB Output is correct
3 Correct 175 ms 10516 KB Output is correct
4 Correct 182 ms 10688 KB Output is correct
5 Correct 244 ms 10928 KB Output is correct
6 Correct 216 ms 12072 KB Output is correct
7 Correct 228 ms 10480 KB Output is correct
8 Correct 199 ms 10656 KB Output is correct
9 Correct 221 ms 11068 KB Output is correct
10 Correct 270 ms 11976 KB Output is correct
11 Correct 279 ms 12016 KB Output is correct
12 Correct 215 ms 12000 KB Output is correct
13 Correct 227 ms 10936 KB Output is correct
14 Correct 238 ms 11108 KB Output is correct
15 Correct 241 ms 10976 KB Output is correct
16 Correct 245 ms 11116 KB Output is correct
17 Correct 177 ms 10924 KB Output is correct
18 Correct 192 ms 11208 KB Output is correct
19 Correct 208 ms 11760 KB Output is correct
20 Correct 281 ms 11876 KB Output is correct
21 Partially correct 297 ms 11988 KB Output is partially correct
22 Correct 289 ms 11968 KB Output is correct
23 Partially correct 313 ms 11960 KB Output is partially correct
24 Correct 307 ms 11976 KB Output is correct
25 Correct 297 ms 12028 KB Output is correct
26 Correct 275 ms 12076 KB Output is correct
27 Correct 190 ms 10552 KB Output is correct
28 Partially correct 191 ms 10488 KB Output is partially correct
29 Correct 148 ms 10704 KB Output is correct
30 Correct 173 ms 10612 KB Output is correct
31 Partially correct 190 ms 10588 KB Output is partially correct
32 Partially correct 136 ms 10512 KB Output is partially correct
33 Correct 191 ms 10804 KB Output is correct
34 Correct 140 ms 10504 KB Output is correct
35 Correct 140 ms 10628 KB Output is correct
36 Partially correct 189 ms 10400 KB Output is partially correct
37 Correct 198 ms 10908 KB Output is correct
38 Correct 154 ms 10608 KB Output is correct
39 Correct 194 ms 11976 KB Output is correct
40 Correct 280 ms 12012 KB Output is correct
41 Partially correct 287 ms 11980 KB Output is partially correct
42 Correct 218 ms 11944 KB Output is correct