제출 #404142

#제출 시각아이디문제언어결과실행 시간메모리
404142opukittpceno_hhr구슬과 끈 (APIO14_beads)C++17
100 / 100
439 ms46556 KiB
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <algorithm> #include <string> #include <cmath> #include <cstdio> #include <iomanip> #include <fstream> #include <cassert> #include <cstring> #include <unordered_set> #include <unordered_map> #include <numeric> #include <ctime> #include <bitset> #include <complex> #include <chrono> #include <random> #include <functional> using namespace std; #define int long long const int INF = 1e18 + 239; const int N = 2e5 + 7; int rP[N]; int to[N]; int dp[N][2]; int udp[N][2]; vector<pair<int, int>> g[N]; void dfs(int cur, int p) { rP[cur] = p; int S = 0; int mx = -INF; for (auto [t, w] : g[cur]) { if (t != p) { to[t] = w; dfs(t, cur); S += max(dp[t][0], dp[t][1]); mx = max(mx, dp[t][0] + to[t] - max(dp[t][0], dp[t][1])); } } dp[cur][1] = S + mx + to[cur]; dp[cur][0] = S; } void dfs2(int cur, int p) { int S = 0; multiset<int> smx = {-INF}; if (p != -1) { S += max(udp[cur][0], udp[cur][1]); smx.insert(udp[cur][0] + to[cur] - max(udp[cur][0], udp[cur][1])); } for (auto [t, w] : g[cur]) { if (t != p) { S += max(dp[t][0], dp[t][1]); smx.insert(dp[t][0] + to[t] - max(dp[t][0], dp[t][1])); } } for (auto [t, w] : g[cur]) { if (t != p) { S -= max(dp[t][0], dp[t][1]); smx.erase(smx.find(dp[t][0] + to[t] - max(dp[t][0], dp[t][1]))); udp[t][0] = S; udp[t][1] = S + *smx.rbegin() + to[t]; S += max(dp[t][0], dp[t][1]); smx.insert(dp[t][0] + to[t] - max(dp[t][0], dp[t][1])); } } for (auto [t, w] : g[cur]) { if (t != p) { dfs2(t, cur); } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i + 1 < n; i++) { int u, v, w; cin >> u >> v >> w; u--; v--; g[u].push_back({v, w}); g[v].push_back({u, w}); } dfs(0, -1); dfs2(0, -1); int ans = 0; for (int i = 0; i < n; i++) { int c = 0; for (auto [j, w] : g[i]) { if (j == rP[i]) { c += max(udp[i][0], udp[i][1]); } else { c += max(dp[j][0], dp[j][1]); } } ans = max(ans, c); } cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...