voiddfs(int s, int fa){ for (auto u : arr[s]) { if (u == fa) continue; dfs(u, s); if (dp[u] > 0) dp[s] += dp[u]; } }
intmain(){ std::ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int N; cin >> N; for (int i = 1; i <= N; i++) { cin >> dp[i]; } for (int i = 1; i <= N - 1; i++) { int u, v; cin >> u >> v; arr[u].emplace_back(v); arr[v].emplace_back(u); } dfs(1, 1); int ans = INT_MIN; for (int i = 1; i <= N; i++) ans = max(ans, dp[i]); cout << ans << endl; }
vector<int> arr[maxN]; int dp[maxN][maxN]; int arrW[maxN]; int arrV[maxN]; int N, M;
voiddfs(int s, int fa){ for (int i = M; i >= arrV[s]; i--) dp[s][i] = arrW[s]; for (auto x : arr[s]) { if (x == fa) continue; dfs(x, s); for (int i = M; i >= arrV[s]; i--) for (int j = 0; i - j >= arrV[s]; j++) dp[s][i] = max(dp[s][i], dp[s][i - j] + dp[x][j]); } }
intmain(){ std::ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> N >> M; for (int i = 1; i <= N; i++) { int k; cin >> k >> arrW[i]; arrV[i] = 1; arr[k].emplace_back(i); } arrV[0] = 0, arrW[0] = 0; memset(dp, 0, sizeof(dp)); dfs(0, 0); cout << dp[0][M] << endl; }
vector<int> arr[maxN]; int siz[maxN]; int dep[maxN]; longlong dp[maxN]; int N;
voiddfs1(int s, int pre){ siz[s] = 1; dep[s] = dep[pre] + 1; for (auto u : arr[s]) { if (u == pre) continue; dfs1(u, s); siz[s] += siz[u]; } }
voiddfs2(int s, int pre){ for (auto u : arr[s]) { if (u == pre) continue; dp[u] = dp[s] + N - 2 * siz[u]; dfs2(u, s); } }
intmain(){ std::ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> N; for (int i = 1; i < N; i++) { int u, v; cin >> u >> v; arr[u].emplace_back(v); arr[v].emplace_back(u); } dfs1(1, 1); dp[1] = 0; dep[1] = 0; for (int i = 1; i <= N; i++) dp[1] += dep[i]; dfs2(1, 1); longlong ans = -1; int index = -1; for (int i = 1; i <= N; i++) if (dp[i] > ans) ans = dp[i], index = i; cout << index << endl; }
structEdge { int to; int val; Edge(int to, int val) : to(to), val(val) {} };
vector<Edge> arr[maxN]; int dp[maxN][2];
voiddfs1(int s, int fa){ for (auto& e : arr[s]) { if (e.to == fa) continue; dfs1(e.to, s); if (arr[e.to].size() == 1) dp[s][0] += e.val; else dp[s][0] += min(e.val, dp[e.to][0]); } }
voiddfs2(int s, int fa){ for (auto& e : arr[s]) { if (e.to == fa) continue; if (arr[s].size() == 1) dp[e.to][1] = e.val; else dp[e.to][1] = min(e.val, dp[s][1] + dp[s][0] - min(e.val, dp[e.to][0])); dfs2(e.to, s); } }
intmain(){ int T; cin >> T; while (T--) { for (auto& a : arr) a.clear(); memset(dp, 0, sizeof(dp)); int N; cin >> N; for (int i = 1; i <= N - 1; i++) { int x, y, z; cin >> x >> y >> z; arr[x].emplace_back(y, z); arr[y].emplace_back(x, z); } dfs1(1, 1); dfs2(1, 1); int maxAns = 0; for (int i = 1; i <= N; i++) if (dp[i][0] + dp[i][1] > maxAns) maxAns = dp[i][0] + dp[i][1]; cout << maxAns << endl; } }
voiddfs1(int s, int fa){ for (auto& e : arr[s]) { if (e.to == fa) continue; dfs1(e.to, s); siz[s] += siz[e.to]; dp[s] += dp[e.to] + siz[e.to] * e.len; } }
voiddfs2(int s, int fa){ for (auto& e : arr[s]) { if (e.to == fa) continue; dp[e.to] = dp[s] - siz[e.to] * e.len + (sumSize - siz[e.to]) * e.len; dfs2(e.to, s); } }
intmain(){ std::ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); memset(dp, 0, sizeof(dp)); memset(siz, 0, sizeof(siz)); int N; cin >> N; for (int i = 1; i <= N; i++) cin >> siz[i], sumSize += siz[i]; for (int i = 1; i <= N - 1; i++) { int a, b, l; cin >> a >> b >> l; arr[a].emplace_back(b, l); arr[b].emplace_back(a, l); } dfs1(1, 1); dfs2(1, 1); longlong ans = LLONG_MAX; for (int i = 1; i <= N; i++) ans = min(ans, dp[i]); cout << ans << endl; }