Submission #1117444

#TimeUsernameProblemLanguageResultExecution timeMemory
1117444vjudge1Race (IOI11_race)C++17
100 / 100
630 ms42232 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long int const int N = 2e5 + 1; int n, k, max_depth; int sz[N]; bool vis[N]; vector<pair<int, int>> adj[N]; int res = INT_MAX; map<int, int> cnt; int get_subtree_size(int u, int p) { sz[u] = 1; for (auto [v, w] : adj[u]) { if (v != p and !vis[v]) sz[u] += get_subtree_size(v, u); } return sz[u]; } int get_centroid(int u, int p, int tree_size) { for (auto [v, w] : adj[u]) { if (v != p and !vis[v] and (sz[v] * 2 > tree_size)) return get_centroid(v, u, tree_size); } return u; } void calc(int u, int p, int fill, int weight, int depth = 1) { if (fill == 1) { if (cnt.count(weight)) { cnt[weight] = min(cnt[weight], depth); } else { cnt[weight] = depth; } } else if ((weight <= k) and (cnt.count(k - weight))) { res = min(res, depth + cnt[k - weight]); } for (auto [v, w] : adj[u]) { if (!vis[v] and (v != p)) calc(v, u, fill, weight + w, depth + 1); } } void decompose(int u, int p) { int tree_size = get_subtree_size(u, p); int centroid = get_centroid(u, p, tree_size); vis[centroid] = true; cnt.clear(); cnt[0] = 0; for (auto [v, w] : adj[centroid]) { if (!vis[v] and v != p) calc(v, centroid, 0, w), calc(v, centroid, 1, w); } for (auto [v, w] : adj[centroid]) { if (!vis[v] and v != p) decompose(v, centroid); } } int best_path(int N, int K, int edges[][2], int weight[2]) { n = N; k = K; for (int i = 0; i < n - 1; ++i) { int u, v, w; u = edges[i][0]; v = edges[i][1]; w = weight[i]; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } decompose(0, -1); if (res == INT_MAX) return -1; return res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...