제출 #742239

#제출 시각아이디문제언어결과실행 시간메모리
742239beaboss경주 (Race) (IOI11_race)C++14
100 / 100
493 ms107516 KiB
// #include <bits/stdc++.h> // using namespace std; // #define s second // #define f first // #define pb push_back // typedef long long ll; // typedef pair<ll, ll> pii; // typedef vector<pii> vpii; // typedef vector<ll> vi; // #define FOR(i, a, b) for (ll i = (a); i<b; i++) // const ll MN = 3e5 + 10; // vpii adj[MN]; // ll d[MN]; // from root in highways // ll d2[MN]; // from root in distance // ll k; // map<ll, ll> small[MN]; // void dfs(ll cur = 0, ll dist = 0, ll dist2 = 0, ll p = -1) { // // cout << cur << endl; // d[cur]=dist; // d2[cur]=dist2; // small[cur][dist2]=dist; // for (auto val: adj[cur]) { // if (val.f != p) { // dfs(val.f, dist + 1ll, dist2 + val.s, cur); // } // } // } // ll ans = 1e9; // void solve(ll cur, ll p=-1) { // ll target = k + 2*d2[cur]; // for (auto val: adj[cur]) { // if (val.f != p) { // solve(val.f, cur); // if (small[val.f].size() > small[cur].size()) swap(small[val.f], small[cur]); // for (pii nx: small[val.f]) { // if (small[cur].find(target - nx.f) != small[cur].end()) { // ans = min(ans, small[cur][target - nx.f] + nx.s - 2*d[cur]); // } // } // for (pii nx: small[val.f]) { // if (small[cur].find(nx.f) == small[cur].end()) small[cur].insert(nx); // else small[cur][nx.f]=min(small[cur][nx.f], nx.s); // } // } // } // } // int best_path(int N, int K, int H[][2], int L[]) // { // k = K; // FOR(i, 0, N-1) { // adj[H[i][0]].pb({H[i][1], L[i]}); // } // dfs(); // solve(0); // if (ans == 1e9) ans = -1; // return ans; // } #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pii; #define pb push_back #define f first #define s second const int maxn = 2e5 + 1; vector<pii> adj[maxn]; map<ll, ll> info[maxn]; ll dist[maxn], sum[maxn]; // define these relative to root int N, K; ll ret; void precomp(int u, int p, ll c, int h) { info[u][c] = h; sum[u] = c; dist[u] = h; for (auto n : adj[u]) { if (p == n.f) { continue; } precomp(n.f, u, c + n.s, h + 1); } } void small_to_large_merge(int u, int p) { ll target = K + 2 * sum[u]; for (auto n : adj[u]) { if (n.f == p) { continue; } small_to_large_merge(n.f, u); if (info[n.f].size() > info[u].size()) { swap(info[n.f], info[u]); } for (auto i : info[n.f]) { if (info[u].find(target - i.f) != info[u].end()) { ret = min(ret, info[u][target - i.f] + i.s - 2 * dist[u]); } } for (auto i : info[n.f]) { if (info[u].find(i.f) == info[u].end()) { info[u].insert(i); } else { info[u][i.f] = min(info[u][i.f], i.s); } } } } int best_path(int n, int k, int edges[][2], int weights[]) { if (k == 1) { return 0; } N = n; K = k; ret = INT_MAX; for (int i = 0; i < n - 1; i++) { int u = edges[i][0]; int v = edges[i][1]; adj[u].pb(pii(v, weights[i])); adj[v].pb(pii(u, weights[i])); } precomp(0, -1, 0, 0); small_to_large_merge(0, -1); return ret == INT_MAX ? -1 : ret; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...