Submission #312633

#TimeUsernameProblemLanguageResultExecution timeMemory
312633aZvezdaBeads and wires (APIO14_beads)C++14
0 / 100
2 ms2688 KiB
#include <bits/stdc++.h> using namespace std; //#pragma GCC optimize ("O3") //#pragma GCC target ("sse4") #define endl "\n" typedef long long ll; template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;} template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;} template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; } template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; } const ll mod = 1e9 + 7; #define out(x) "{" << (#x) << ": " << x << "} " const int MAX_N = 1e5 + 10; int dp[MAX_N][2]; int n; vector<pair<int, int> > g[MAX_N]; void dfs(int x, int p, int w) { vector<int> pos; int sum = 0; for(auto it : g[x]) { if(it.first == p) {continue;} dfs(it.first, x, it.second); int curr = max(dp[it.first][0], dp[it.first][1]); sum += curr; pos.push_back(dp[it.first][0] + it.second - curr); } dp[x][0] = sum; sort(pos.rbegin(), pos.rend()); for(int i = 0; i < (int)pos.size() - 1; i += 2) { int curr = pos[i] + pos[i + 1]; if(curr > 0) { dp[x][0] += curr; } break; } dp[x][1] = sum; pos.push_back(w); sort(pos.rbegin(), pos.rend()); for(int i = 0; i < (int)pos.size() - 1; i += 2) { int curr = pos[i] + pos[i + 1]; if(curr > 0) { dp[x][1] += curr; } break; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for(int i = 0; i < n - 1; i ++) { int a, b, c; cin >> a >> b >> c; g[a].push_back({b, c}); g[b].push_back({a, c}); } dfs(1, 0, mod); cout << dp[1][0] << endl; return 0; } /* 5 1 2 10 1 3 40 1 4 15 1 5 20 10 4 10 2 1 2 21 1 3 13 6 7 1 7 9 5 2 4 3 2 5 8 1 6 55 6 8 34 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...