Submission #765928

#TimeUsernameProblemLanguageResultExecution timeMemory
765928t6twotwoHighway Tolls (IOI18_highway)C++17
51 / 100
132 ms10276 KiB
#include "highway.h" #include <bits/stdc++.h> using namespace std; using ll = long long; void find_pair(int N, vector<int> U, vector<int> V, int A, int B) { int M = U.size(), lo, hi; ll s = ask(vector<int>(M)); vector<vector<pair<int, int>>> adj(N); for (int i = 0; i < M; i++) { adj[U[i]].emplace_back(V[i], i); adj[V[i]].emplace_back(U[i], i); } vector<int> W(M); lo = 0, hi = M - 1; while (lo < hi) { int mi = (lo + hi) / 2; auto w = W; for (int i = 0; i <= mi; i++) { w[i] = 1; } if (ask(w) == s) { W = w; lo = mi + 1; } else { hi = mi; } } auto bfs = [&](int s) { vector<int> dis(N, -1), f(N, -1); dis[s] = 0; queue<int> q; q.push(s); while (!q.empty()) { int x = q.front(); q.pop(); for (auto [y, z] : adj[x]) { if (dis[y] == -1 && !W[z]) { dis[y] = dis[x] + 1; f[y] = z; q.push(y); } } } return make_pair(dis, f); }; int P = U[lo], Q = V[lo]; auto [d0, f0] = bfs(P); auto [d1, f1] = bfs(Q); f0[P] = lo; f1[Q] = lo; vector<int> w(M, 1); vector<bool> vis(N); vis[P] = 1; for (int i = 0; i < N; i++) { if (d0[i] < d1[i] && d0[i] != -1) { int x = i; while (!vis[x]) { vis[x] = 1; w[f0[x]] = 0; x ^= U[f0[x]] ^ V[f0[x]]; } } } int t = (s + (s / A) * (B - A) - ask(w)) / (B - A); vector<int> cand; for (int i = 0; i < N; i++) { if (d0[i] == t && d0[i] < d1[i] && d0[i] != -1) { cand.push_back(i); } } assert(ask(W) == s); lo = 0, hi = cand.size() - 1; while (lo < hi) { int mi = (lo + hi) / 2; vector<int> w = W; for (int i = 0; i <= mi; i++) { for (auto [y, z] : adj[cand[i]]) { if (d0[y] + 1 == d0[cand[i]]) { w[z] = 1; } } } if (ask(w) == s + B - A) { hi = mi; } else { lo = mi + 1; } } int S = cand[lo]; cand.clear(); for (int i = 0; i < N; i++) { if (d1[i] == s / A - t - 1 && d1[i] < d0[i] && d1[i] != -1) { cand.push_back(i); } } lo = 0, hi = cand.size() - 1; while (lo < hi) { int mi = (lo + hi) / 2; vector<int> w = W; for (int i = 0; i <= mi; i++) { for (auto [y, z] : adj[cand[i]]) { if (d1[y] + 1 == d1[cand[i]]) { w[z] = 1; } } } if (ask(w) == s + B - A) { hi = mi; } else { lo = mi + 1; } } answer(S, cand[lo]); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...