Submission #108103

# Submission time Handle Problem Language Result Execution time Memory
108103 2019-04-27T10:24:30 Z Noam527 Highway Tolls (IOI18_highway) C++17
51 / 100
314 ms 11660 KB
#include <bits/stdc++.h>
#define CHECK cout << "ok" << endl
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7;
const ll inf = 1e18;
const int OO = 0;
const int OOO = 1;
using namespace std;

void answer(int s, int t);

ll ask(const vector<int> &w);

struct edge {
	int to, i;
	edge() {}
	edge(int tt, int ii) {
		to = tt;
		i = ii;
	}
};

vector<int> w;
vector<int> ord;
vector<int> special;

int n, m;
ll d;
vector<vector<edge>> g;
vector<int> vis, dep;

int crit;

ll f(int x) {
	for (int i = 0; i < x; i++) w[ord[i]] = 0;
	for (int i = x; i < ord.size(); i++) w[ord[i]] = 1;
	return ask(w);
}

void bfs(int v) {
	ord.clear();
	vis.resize(n, 0);
	dep.resize(n, 0);
	vis[v] = 1;
	queue<int> q;
	q.push(v);
	while (!q.empty()) {
		int x = q.front();
		q.pop();
		for (const auto &i : g[x])
			if (!vis[i.to]) {
				dep[i.to] = dep[x] + 1;
				ord.push_back(i.i);
				vis[i.to] = 1;
				q.push(i.to);
			}
	}
}
void dfs(int v) {
	vis[v] = 1;
	for (const auto &i : g[v])
		if (!vis[i.to] && w[i.i] == 0) {
			special[i.i] = 1;
			dfs(i.to);
		}
}

void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B) {
	if (N == 2) {
		answer(0, 1);
		return;
	}
	n = N;
	g.resize(n);
	m = U.size();
	w.resize(m);
	for (int i = 0; i < m; i++) {
		g[U[i]].push_back(edge(V[i], i));
		g[V[i]].push_back(edge(U[i], i));
	}
	for (auto &i : w) i = 0;
	d = ask(w);
	if (OO) cout << "d = " << d << '\n';
	int lo, hi, mid;

	// 1: find edge on shortest path
	ord.resize(m);
	for (int i = 0; i < m; i++) ord[i] = i;
	lo = 0, hi = m - 1;
	while (lo < hi) {
		mid = (lo + hi + 1) / 2;
		if (f(mid) == d) hi = mid - 1;
		else lo = mid;
	}
	crit = lo;
	if (OO) cout << "critical edge index " << crit << ", (" << U[crit] << ", " << V[crit] << ")\n";
	// 2: root at 1 end of the edge, and bsearch in the subtree of the other for S/T
	for (auto &i : w) i = 1;
	bfs(U[crit]);
	for (auto &i : ord) w[i] = 0;

	// determine all edges in the subtree of the other end
	for (auto &i : vis) i = 0;
	vis[U[crit]] = 1;
	special.resize(m, 0);
	special[crit] = 1;
	dfs(V[crit]);
	vector<int> ord1, ord2;
	for (const auto &i : ord)
		if (special[i])
			ord1.push_back(i);
		else
			ord2.push_back(i);

	if (OO) {
		cout << "rooted at " << U[crit] << '\n';
		cout << "bfs: ";
		for (const auto &i : ord) cout << i << " "; cout << '\n';
		cout << "ord1: ";
		for (const auto &i : ord1) cout << i << " "; cout << '\n';
		cout << "ord2: ";
		for (const auto &i : ord2) cout << i << " "; cout << '\n';
		cout << "dep: ";
		for (const auto &i : dep) cout << i << " "; cout << '\n';
	}

	// bsearch in the subtree
	ord = ord1;
	lo = 0, hi = (int)ord.size() - 1;
	while (lo < hi) {
		mid = (lo + hi + 1) / 2;
		if (f(mid) == d) hi = mid - 1;
		else lo = mid;
	}
	int S, T;
	if (OO) {
		cout << "first bsearch yields edge " << ord[lo] << '\n';
	}
	if (dep[U[ord[lo]]] > dep[V[ord[lo]]]) S = U[ord[lo]];
	else S = V[ord[lo]];
	if (OO) cout << "resulting in S = " << S << '\n';

	if ((ll)A * dep[S] == d) {
		T = U[crit];
		answer(S, T);
		return;
	}

	// bsearch on everything else
	for (auto &i : ord1) w[i] = 0;
	ord = ord2;
	lo = 0, hi = (int)ord.size() - 1;
	while (lo < hi) {
		mid = (lo + hi + 1) / 2;
		if (f(mid) == d) hi = mid - 1;
		else lo = mid;
	}
	if (OO) {
		cout << "second bsearch yields edge " << ord[lo] << '\n';
	}
	if (dep[U[ord[lo]]] > dep[V[ord[lo]]]) T = U[ord[lo]];
	else T = V[ord[lo]];
	if (OO) cout << "resulting in T = " << T << '\n';

	answer(S, T);
}

Compilation message

highway.cpp: In function 'll f(int)':
highway.cpp:38:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = x; i < ord.size(); i++) w[ord[i]] = 1;
                  ~~^~~~~~~~~~~~
highway.cpp: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:120:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for (const auto &i : ord) cout << i << " "; cout << '\n';
   ^~~
highway.cpp:120:47: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   for (const auto &i : ord) cout << i << " "; cout << '\n';
                                               ^~~~
highway.cpp:122:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for (const auto &i : ord1) cout << i << " "; cout << '\n';
   ^~~
highway.cpp:122:48: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   for (const auto &i : ord1) cout << i << " "; cout << '\n';
                                                ^~~~
highway.cpp:124:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for (const auto &i : ord2) cout << i << " "; cout << '\n';
   ^~~
highway.cpp:124:48: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   for (const auto &i : ord2) cout << i << " "; cout << '\n';
                                                ^~~~
highway.cpp:126:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for (const auto &i : dep) cout << i << " "; cout << '\n';
   ^~~
highway.cpp:126:47: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   for (const auto &i : dep) cout << i << " "; cout << '\n';
                                               ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 408 KB Output is correct
2 Correct 2 ms 324 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 328 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 408 KB Output is correct
7 Correct 2 ms 248 KB Output is correct
8 Correct 2 ms 248 KB Output is correct
9 Correct 2 ms 248 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 292 KB Output is correct
12 Correct 2 ms 248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 396 KB Output is correct
2 Correct 12 ms 1316 KB Output is correct
3 Correct 293 ms 9604 KB Output is correct
4 Correct 215 ms 9592 KB Output is correct
5 Correct 210 ms 9548 KB Output is correct
6 Correct 189 ms 9576 KB Output is correct
7 Correct 176 ms 9596 KB Output is correct
8 Correct 187 ms 9612 KB Output is correct
9 Correct 245 ms 9624 KB Output is correct
10 Correct 181 ms 9612 KB Output is correct
11 Correct 189 ms 9612 KB Output is correct
12 Correct 211 ms 10064 KB Output is correct
13 Correct 211 ms 9128 KB Output is correct
14 Correct 201 ms 9176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 1576 KB Output is correct
2 Correct 40 ms 2584 KB Output is correct
3 Correct 55 ms 3392 KB Output is correct
4 Correct 152 ms 10336 KB Output is correct
5 Correct 154 ms 10784 KB Output is correct
6 Correct 174 ms 9548 KB Output is correct
7 Correct 165 ms 9180 KB Output is correct
8 Correct 151 ms 9880 KB Output is correct
9 Correct 138 ms 9756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 25 ms 1272 KB Output is correct
3 Correct 169 ms 7660 KB Output is correct
4 Correct 201 ms 9612 KB Output is correct
5 Correct 125 ms 9592 KB Output is correct
6 Correct 179 ms 9700 KB Output is correct
7 Correct 188 ms 9540 KB Output is correct
8 Correct 182 ms 9588 KB Output is correct
9 Correct 209 ms 9424 KB Output is correct
10 Correct 207 ms 9564 KB Output is correct
11 Correct 237 ms 9568 KB Output is correct
12 Correct 197 ms 9340 KB Output is correct
13 Correct 207 ms 9368 KB Output is correct
14 Correct 233 ms 9784 KB Output is correct
15 Correct 186 ms 9528 KB Output is correct
16 Correct 194 ms 9604 KB Output is correct
17 Correct 219 ms 9684 KB Output is correct
18 Correct 186 ms 9876 KB Output is correct
19 Correct 194 ms 9604 KB Output is correct
20 Correct 201 ms 10232 KB Output is correct
21 Correct 176 ms 10136 KB Output is correct
22 Correct 177 ms 10024 KB Output is correct
23 Correct 169 ms 9776 KB Output is correct
24 Correct 173 ms 9900 KB Output is correct
25 Correct 230 ms 9264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 1348 KB Output is correct
2 Correct 36 ms 1336 KB Output is correct
3 Correct 251 ms 10108 KB Output is correct
4 Correct 253 ms 10584 KB Output is correct
5 Correct 274 ms 11540 KB Output is correct
6 Correct 232 ms 11600 KB Output is correct
7 Correct 306 ms 11608 KB Output is correct
8 Correct 249 ms 11660 KB Output is correct
9 Incorrect 258 ms 8060 KB Output is incorrect: {s, t} is wrong.
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 28 ms 1348 KB Output is correct
2 Correct 23 ms 1488 KB Output is correct
3 Correct 197 ms 10024 KB Output is correct
4 Correct 260 ms 10188 KB Output is correct
5 Correct 236 ms 10464 KB Output is correct
6 Correct 296 ms 11612 KB Output is correct
7 Correct 228 ms 10076 KB Output is correct
8 Correct 279 ms 10380 KB Output is correct
9 Correct 238 ms 10560 KB Output is correct
10 Correct 278 ms 11592 KB Output is correct
11 Correct 314 ms 11604 KB Output is correct
12 Correct 283 ms 11624 KB Output is correct
13 Correct 251 ms 9100 KB Output is correct
14 Incorrect 258 ms 8348 KB Output is incorrect: {s, t} is wrong.
15 Halted 0 ms 0 KB -