Submission #36357

#TimeUsernameProblemLanguageResultExecution timeMemory
36357cheater2kFireworks (APIO16_fireworks)C++14
7 / 100
6 ms18580 KiB
#include <bits/stdc++.h> using namespace std; const int N = 300010; const long long inf = 1e18; int n, m; vector< pair<int,int> > G[N]; long long dp[N], tmp[N], dist[N]; int tin[N], tout[N], TIME; // leaf: n + 1 -> n + m bool inside(int fixed, int u) { return (tin[fixed] >= tin[u] && tin[fixed] <= tout[u]); } void dfs(int u) { tin[u] = ++TIME; for (auto e : G[u]) { int v = e.second, c = e.first; dist[v] = dist[u] + c; dfs(v); } tout[u] = TIME; } void solve(int u) { for (auto e : G[u]) { int v = e.second, c = e.first; solve(v); } for (int fixed = n + 1; fixed <= n + m; ++fixed) tmp[fixed] = 0; for (int fixed = n + 1; fixed <= n + m; ++fixed) { if (!inside(fixed, u)) continue; for (auto e : G[u]) { int v = e.second; long long mn = inf; for (int leaf = n + 1; leaf <= n + m; ++leaf) { if (!inside(leaf, v)) continue; long long cur = dp[leaf] + abs(dist[leaf] - dist[fixed]); mn = min(mn, cur); } tmp[fixed] += mn; } } for (int fixed = n + 1; fixed <= n + m; ++fixed) { if (inside(fixed, u)) dp[fixed] = tmp[fixed]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; int nNode = n + m; for (int i = 2; i <= nNode; ++i) { int p, c; cin >> p >> c; G[p].push_back(make_pair(c, i)); } dfs(1); solve(1); long long ans = inf; for (int fixed = n + 1; fixed <= n + m; ++fixed) { ans = min(ans, dp[fixed]); } cout << ans << endl; }

Compilation message (stderr)

fireworks.cpp: In function 'void solve(int)':
fireworks.cpp:30:21: warning: unused variable 'c' [-Wunused-variable]
   int v = e.second, c = e.first;
                     ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...