Submission #569742

#TimeUsernameProblemLanguageResultExecution timeMemory
569742SSRSBeads and wires (APIO14_beads)C++14
0 / 100
1 ms300 KiB
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; const int INF = 1000000000; int main(){ int n; cin >> n; vector<vector<pair<int, int>>> E(n); for (int i = 0; i < n - 1; i++){ int a, b, c; cin >> a >> b >> c; a--; b--; E[a].push_back(make_pair(c, b)); E[b].push_back(make_pair(c, a)); } vector<int> p(n, -1); vector<vector<pair<int, int>>> c(n); queue<int> Q; Q.push(0); vector<int> bfs; while (!Q.empty()){ int v = Q.front(); Q.pop(); bfs.push_back(v); for (auto P : E[v]){ int w = P.second; if (w != p[v]){ p[w] = v; c[v].push_back(P); Q.push(w); } } } reverse(bfs.begin(), bfs.end()); vector<int> dp1(n, 0); vector<int> dp2(n, -INF); for (int v : bfs){ int cnt = c[v].size(); vector<vector<int>> dp3(3, vector<int>(cnt + 1, -INF)); dp3[0][0] = 0; for (int i = 0; i < cnt; i++){ int l = c[v][i].first; int w = c[v][i].second; for (int j = 0; j < 3; j++){ dp3[j][i + 1] = max(dp3[j][i + 1], dp3[j][i] + max(dp1[w], dp2[w] + l)); if (j < 2){ dp3[j + 1][i + 1] = max(dp3[j + 1][i + 1], dp3[j][i] + dp1[w] + l); } } } dp1[v] = max(dp3[0][cnt], dp3[2][cnt]); dp2[v] = dp3[1][cnt]; } cout << dp1[0] << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...