Submission #978898

#TimeUsernameProblemLanguageResultExecution timeMemory
978898happypotatoHighway Tolls (IOI18_highway)C++17
51 / 100
223 ms262144 KiB
#include "highway.h" #include <bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int, int> #define ff first #define ss second #define pb push_back const int mxN = 1e5; vector<pii> adj[mxN]; int par[mxN]; int paredge[mxN]; int depth[mxN]; void dfs1(int u = 0, int dep = 0, int pp = -1) { depth[u] = dep; for (pii v : adj[u]) { if (v.ff == pp) continue; paredge[v.ff] = v.ss; par[v.ff] = u; dfs1(v.ff, dep + 1, u); } } void find_pair(int n, vector<int> U, vector<int> V, int A, int B) { int m = U.size(); vector<pair<pii, int>> edges(m); for (int i = 0; i < m; i++) { edges[i] = {{U[i], V[i]}, i}; adj[U[i]].pb({V[i], i}); adj[V[i]].pb({U[i], i}); } vector<int> pass(m, 0); ll base = ask(pass); dfs1(); paredge[0] = -1; int S, T, LCA, ldep; // search for last element { vector<pii> v; for (int i = 1; i < n; i++) { v.pb({depth[i], i}); } sort(v.begin(), v.end(), greater<pii>()); int lb = 0, rb = (int)(v.size()) - 1; while (lb < rb) { int mid = (lb + rb + 1) >> 1; for (int i = 0; i < mid; i++) pass[paredge[v[i].ss]] = 1; ll ret = ask(pass); if (ret == base) lb = mid; else rb = mid - 1; for (int i = 0; i < mid; i++) pass[paredge[v[i].ss]] = 0; } S = v[lb].ss; } // search for depth of LCA { int lb = 0, rb = n; while (lb < rb) { int mid = (lb + rb + 1) >> 1; for (int i = 1; i < n; i++) { pass[paredge[i]] = (depth[i] <= mid); } ll ret = ask(pass); if (ret == base) lb = mid; else rb = mid - 1; for (int i = 0; i < m; i++) pass[i] = 0; } ldep = lb; } LCA = S; for (int i = depth[S]; i > ldep; i--) LCA = par[LCA]; if (1LL * (depth[S] - ldep) * A == base) return answer(LCA, S); int rdep = (base / A) - (depth[S] - ldep) + ldep; int avoid = S; for (int i = depth[S]; i > rdep; i--) avoid = par[avoid]; // search for remaining node { vector<int> v; for (int i = 0; i < n; i++) { if (depth[i] == rdep && i != avoid) v.pb(i); } int lb = 0, rb = (int)(v.size()) - 1; while (lb < rb) { int mid = (lb + rb) >> 1; for (int i = 0; i <= mid; i++) pass[paredge[v[i]]] = 1; ll ret = ask(pass); if (ret != base) rb = mid; else lb = mid + 1; for (int i = 0; i <= mid; i++) pass[paredge[v[i]]] = 0; } T = v[lb]; } answer(S, T); }
#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...