Submission #265497

#TimeUsernameProblemLanguageResultExecution timeMemory
265497square1001Beads and wires (APIO14_beads)C++14
28 / 100
1050 ms768 KiB
#include <vector> #include <iostream> #include <algorithm> #include <functional> using namespace std; const long long inf = 1LL << 40; int main() { // step #1. read input cin.tie(0); ios_base::sync_with_stdio(false); int N; cin >> N; vector<int> ga(N - 1), gb(N - 1); vector<long long> gw(N - 1); for(int i = 0; i < N - 1; ++i) { cin >> ga[i] >> gb[i] >> gw[i]; --ga[i], --gb[i]; } // step #2. construct a graph vector<int> sep(N + 1); for(int i = 0; i < N - 1; ++i) { ++sep[ga[i] + 1]; ++sep[gb[i] + 1]; } for(int i = 0; i < N; ++i) { sep[i + 1] += sep[i]; } vector<int> ctr(sep); vector<int> to(2 * N - 2); vector<long long> cost(2 * N - 2); for(int i = 0; i < N - 1; ++i) { to[ctr[ga[i]]] = gb[i]; cost[ctr[ga[i]]++] = gw[i]; to[ctr[gb[i]]] = ga[i]; cost[ctr[gb[i]]++] = gw[i]; } // step #3. calculation function<pair<long long, long long>(int, int)> solve = [&](int pos, int pre) { // returns = (no mid-blue, one mid-blue) long long sumcost = 0, delta = -inf; for(int i = sep[pos]; i < sep[pos + 1]; ++i) { if(to[i] == pre) continue; pair<long long, long long> res = solve(to[i], pos); sumcost += max(res.first, res.second + cost[i]); delta = max(delta, cost[i] - max(res.second + cost[i] - res.first, 0LL)); } return make_pair(sumcost, sumcost + delta); }; long long ans = 0; for(int i = 0; i < N; ++i) { pair<long long, long long> res = solve(i, -1); ans = max(ans, res.first); } // step #4. print the answer 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...