Submission #31600

#TimeUsernameProblemLanguageResultExecution timeMemory
31600minkankBeads and wires (APIO14_beads)C++14
0 / 100
3 ms1408 KiB
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> ii; const int N = 1e4 + 5; int n, dp[N][2], ans; vector<ii> a[N], b[N]; vector<int> l[N], r[N]; void dfs(int u, int p) { b[u].clear(); b[u].push_back(ii(0, 0)); for(int i = 0; i < a[u].size(); ++i) { int v = a[u][i].first; if(v == p) continue; dfs(v, u); b[u].push_back(a[u][i]); } l[u].assign(b[u].size() + 1, 0); r[u].assign(b[u].size() + 1, 0); for(int i = 1; i < b[u].size(); ++i) { int v = b[u][i].first, w = b[u][i].second; l[u][i] = l[u][i - 1] + max(dp[v][1] + w, dp[v][0]); } for(int i = b[u].size() - 1; i >= 1; --i) { int v = b[u][i].first, w = b[u][i].second; r[u][i] = r[u][i + 1] + max(dp[v][1] + w, dp[v][0]); } dp[u][0] = l[u][b[u].size() - 1]; for(int i = 1; i < b[u].size(); ++i) { int v = b[u][i].first, w = b[u][i].second; dp[u][1] = max(dp[u][1], l[u][i - 1] + r[u][i + 1] + dp[v][0] + w); } if(b[u].size() == 1) dp[u][1] = -1e9; } int main() { cin >> n; for(int i = 1; i < n; ++i) { int u, v, w; cin >> u >> v >> w; a[u].push_back(ii(v, w)); a[v].push_back(ii(u, w)); } for(int i = 1; i <= n; ++i) { memset(dp, 0, sizeof dp); dfs(i, i); ans = max(ans, dp[i][0]); ans = max(ans, dp[i][1]); } cout << ans; }

Compilation message (stderr)

beads.cpp: In function 'void dfs(int, int)':
beads.cpp:15:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < a[u].size(); ++i) {
                    ~~^~~~~~~~~~~~~
beads.cpp:22:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 1; i < b[u].size(); ++i) {
                    ~~^~~~~~~~~~~~~
beads.cpp:31:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 1; i < b[u].size(); ++i) {
                    ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...