Submission #372998

#TimeUsernameProblemLanguageResultExecution timeMemory
372998KanonStations (IOI20_stations)C++14
100 / 100
1037 ms1168 KiB
#include "stations.h" #include <bits/stdc++.h> using namespace std; vector<int> label(int n, int k, vector<int> u, vector<int> v) { vector<vector<int>> g(n); for(int i = 0; i < n - 1; i++) { int a = u[i], b = v[i]; g[a].push_back(b); g[b].push_back(a); } vector<int> ret(n, -1); vector<int> sub(n); vector<int> par(n); function<void(int, int)> dfs = [&](int v, int p) { par[v] = p; sub[v] = 1; for(int i : g[v]) { if(i == p) continue; dfs(i, v); sub[v] += sub[i]; } }; dfs(0, -1); function<void(int, int, int, bool)> solve = [&](int v, int L, int R, bool head) { ret[v] = head ? L : R; int st = L + head; for(int i : g[v]) { if(i == par[v]) continue; solve(i, st, st + sub[i] - 1, !head); st += sub[i]; } }; solve(0, 0, n - 1, true); return ret; } int find_next_station(int s, int t, vector<int> c) { if(c.size() == 1) return c[0]; int n = c.size(); int par = -1; for(int i : c) if(par == -1 || abs(s - i) > abs(s - par)) par = i; for(int i = 0; i < n; i++) if(c[i] == par) { c.erase(c.begin() + i); break; } c.push_back(s); sort(c.begin(), c.end()); int beg = c[0], fin = c[n - 1]; if(t < beg || t > fin) return par; if(beg == s) { return *lower_bound(c.begin(), c.end(), t); } else { return *prev(upper_bound(c.begin(), c.end(), 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...