제출 #363129

#제출 시각아이디문제언어결과실행 시간메모리
363129shivensinha4구슬과 끈 (APIO14_beads)C++17
28 / 100
1077 ms876 KiB
#include "bits/stdc++.h" using namespace std; #define for_(i, s, e) for (int i = s; i < (int) e; i++) #define for__(i, s, e) for (ll i = s; i < e; i++) typedef long long ll; typedef vector<int> vi; typedef pair<int, int> ii; //#define endl '\n' vector<vi> dp; vector<vector<array<int, 2>>> adj; vi cost; int n; void dfs(int p, int parent, int pe) { dp[p] = {0, 0}; int v = 0; for (auto i: adj[p]) if (i[0] != parent) { dfs(i[0], p, i[1]); v = max(v, -dp[i[0]][0] + dp[i[0]][1] + i[1] + pe); dp[p][1] += dp[i[0]][0]; } dp[p][0] = dp[p][1] + v; // cout << p << " " << dp[p][0] << " " << dp[p][1] << endl; } int main() { #ifdef mlocal freopen("test.in", "r", stdin); #endif ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; cost.resize(n); dp.resize(n, vi(2)); adj.resize(n); for_(i, 0, n-1) { int a, b, w; cin >> a >> b >> w; a -= 1; b -= 1; adj[a].push_back({b, w}); adj[b].push_back({a, w}); } int ans = 0; for_(i, 0, n) { dfs(i, i, 0); ans = max(ans, dp[i][1]); } cout << ans << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...