Submission #942395

#TimeUsernameProblemLanguageResultExecution timeMemory
942395THXuanRace (IOI11_race)C++14
0 / 100
35 ms88664 KiB
#include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <queue> #include <set> #include <map> #define INF 1e18 using namespace std; typedef long long ll; int n, k; ll ans = INF; vector<pair<int ,int>>adj[200005]; bool visited[200005]; ll dp[200005], cnt[10000000]; vector<ll>v; int get_size(int s, int e) { dp[s] = 1; for (auto u : adj[s]) { if (u.first == e || visited[u.first]) continue; get_size(u.first, s); dp[s] += dp[u.first]; } return dp[s]; } int get_centroid(int s, int e, int n) { for (auto u : adj[s]) { if (u.first == e || visited[u.first]) continue; if (dp[u.first] * 2 > n)return get_centroid(u.first, s, n); } return s; } void dfs(int s, int e, bool flag, int sum, ll dep = 2) { if (sum > k) return; if (!flag) { ans = min(ans, dep + cnt[k - sum]); } else { cnt[sum] = min(cnt[sum], dep); v.push_back(sum); } for (auto u : adj[s]) { if (u.first == e || visited[u.first]) continue; dfs(u.first, s, flag, sum + u.second, dep + 1); } } void centroid_decomposition(int node) { int centroid = get_centroid(node, 0, get_size(node, 0)); visited[centroid] = true; for (auto u : adj[centroid]) { if (!visited[u.first]) { if (u.second <= k)ans = min(ans, 1 + cnt[k - u.second]); dfs(u.first, centroid, false, u.second); cnt[u.second] = min(cnt[u.second], 1*1ll); v.push_back(u.second); dfs(u.first, centroid, true, u.second); } } for (auto u : v) cnt[u] = INF; v.clear(); for (auto u : adj[centroid]) { if (!visited[u.first])centroid_decomposition(u.first); } } int best_path(int n, int k, int h[][2], int l[]) { for (int i = 0; i <= 10000000; i++) cnt[i] = INF; for (int i = 1; i <= n - 1; i++) { adj[h[i][0]].push_back({ h[i][1], l[i] }); adj[h[i][1]].push_back({ h[i][0],l[i]}); } centroid_decomposition(1); if (ans == INF) return -1; return ans; }

Compilation message (stderr)

race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:72:45: warning: iteration 10000000 invokes undefined behavior [-Waggressive-loop-optimizations]
   72 |  for (int i = 0; i <= 10000000; i++) cnt[i] = INF;
      |                                             ^
race.cpp:72:20: note: within this loop
   72 |  for (int i = 0; i <= 10000000; i++) cnt[i] = INF;
      |                  ~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...