제출 #636724

#제출 시각아이디문제언어결과실행 시간메모리
636724rainliofficial경주 (Race) (IOI11_race)C++17
9 / 100
124 ms33080 KiB
#include <bits/stdc++.h> #include "race.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } #define sz(x) (int)x.size() void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x); template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifdef DEBUG #define dbg(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl; #else #define dbg(x...) #endif const int MAXN = 2e5+5, INF = 1e9; int n, k, ans = INF, h[MAXN][2], l[MAXN], dep[MAXN]; vector<pii> arr[MAXN]; set<pii> vals[MAXN]; pii offset[MAXN]; void solve(int at, int parent, int cost, int d = 0){ // Find the largest subtree int big = at, bigCost = 0; dep[at] = d; for (pii i : arr[at]){ if (i.first != parent){ solve(i.first, at, i.second, d+1); if (vals[i.first].size() > vals[big].size()){ big = i.first; bigCost = i.second; } } } if (at != big){ swap(vals[at], vals[big]); // Copy the largest subtree offset[at] = offset[big]; offset[at].first += bigCost; offset[at].second++; } // Manually add the remaining elements for (pii i : arr[at]){ if (i.first != parent && i.first != big){ // Since vals[big] is empty now (we called swap() on it), we don't have to worry about it anymore for (pii x : vals[i.first]){ int costNeeded = k - (x.first + offset[i.first].first + i.second) - offset[at].first; int adjDist = x.second + offset[i.first].second + 1; auto it = vals[at].lower_bound({costNeeded, -INF}); if (it != vals[at].end() && (*it).first == costNeeded){ ckmin(ans, (*it).second + offset[at].second + adjDist); } } for (pii x : vals[i.first]){ int adjCost = (x.first + offset[i.first].first + i.second) - offset[at].first; int adjDist = x.second + offset[i.first].second - offset[at].second; vals[at].insert({adjCost, adjDist}); } } } auto it = vals[at].lower_bound({k - offset[at].first, -INF}); if (it != vals[at].end() && (*it).first == k - offset[at].first){ ckmin(ans, (*it).second + offset[at].second); // dbg(ans, at, offset[at].first, (*it), offset[at].second) } vals[at].insert({-offset[at].first, -offset[at].second}); } int best_path(int N, int K, int h[][2], int l[]){ n = N, k = K; for (int i=0; i<n-1; i++){ int a = h[i][0], b = h[i][1]; arr[a].push_back({b, l[i]}); arr[b].push_back({a, l[i]}); } solve(0, -1, 0); return ans != INF ? ans : -1; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...