Submission #108743

#TimeUsernameProblemLanguageResultExecution timeMemory
108743turbatRace (IOI11_race)C++14
43 / 100
3037 ms36296 KiB
#include <bits/stdc++.h> #include "race.h" using namespace std; using vi = vector <int>; using pii = pair <int, int>; #define pb push_back #define F first #define S second #define N 200005 int n, k, ans = N, sz[N], mx[N], centroid; map <int, int> dist; bool used[N]; vector <pii> edg[N]; void get_size(int u, int p){ sz[u] = 1; mx[u] = 0; for (pii v : edg[u]) if (v.F != p && !used[v.F]){ get_size(v.F, u); mx[u] = max(mx[u], sz[v.F]); sz[u] += sz[v.F]; } mx[u] = max(mx[u], n - sz[u]); } void get_centroid(int u, int p){ for (pii v : edg[u]) if (v.F != p && !used[v.F]) get_centroid(v.F, u); if (mx[u] <= (n + 1) / 2) centroid = u; } void calc(int u, int p, int lenght, int distance){ if(distance > k) return; for (pii v : edg[u]) if (v.F != p && !used[v.F]) calc(v.F, u, lenght + 1, distance + v.S); if (dist.count(k - distance)) ans = min (ans, lenght + dist[k - distance]); } void add(int u, int p, int lenght, int distance){ if(distance > k) return; for (pii v : edg[u]) if (v.F != p && !used[v.F]) add(v.F, u, lenght + 1, distance + v.S); if (!dist.count(distance)) dist[distance] = lenght; else dist[distance] = min(dist[distance], lenght); } void solve(int u){ dist.clear(); dist[0] = 0; get_size(u, -1); n = sz[u]; get_centroid(u, -1); used[centroid] = 1; for (pii v : edg[centroid]) if (!used[v.F]){ calc(v.F, centroid, 1, v.S); add(v.F, centroid, 1, v.S); } for (pii v : edg[centroid]) if (!used[v.F]) solve(v.F); } int best_path(int s, int K, int H[][2], int L[]){ n = s; k = K; for (int i = 0;i < n - 1;i++){ edg[H[i][0]].pb({H[i][1], L[i]}); edg[H[i][1]].pb({H[i][0], L[i]}); } solve(0); return ans == N ? -1 : ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...