제출 #110110

#제출 시각아이디문제언어결과실행 시간메모리
110110TAISA_구슬과 끈 (APIO14_beads)C++14
0 / 100
2 ms384 KiB
#include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() using namespace std; using ll = long long; using P = pair<ll, ll>; constexpr ll INF = (1LL << 40) - 1LL; constexpr ll LINF = (1LL << 60) - 1LL; constexpr double eps = 1e-9; constexpr ll MOD = 1000000007LL; template <typename T> bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }; template <typename T> bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }; template <typename T> ostream& operator<<(ostream& os, vector<T> v) { for(int i = 0; i < v.size(); i++) { os << v[i] << (i + 1 == v.size() ? "\n" : " "); } return os; } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T& t, const V& v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T& t, const V& v) { for(auto& e : t) { fill_v(e, v); } }; int n; vector<vector<P>> G; vector<vector<ll>> dp; ll dfs(int i, int f, int p) { if(dp[i][f] != -1) { return dp[i][f]; } ll sum = 0, m21 = -LINF, m22 = -LINF; for(auto pe : G[i]) { int e = pe.first; ll c = pe.second; if(e == p) { continue; } dfs(e, 0, i); dfs(e, 1, i); dfs(e, 2, i); ll t = max(dp[e][1] + c, dp[e][0]); sum += t; if(m21 < dp[e][2] + c - t) { m22 = m21; m21 = dp[e][2] + c - t; } else if(m22 < dp[e][2] + c - t) { m22 = dp[e][2] + c - t; } } ll res = -LINF; if(f == 0) { chmax(res, sum + m21 + m22); chmax(res, sum); } else if(f == 1) { chmax(res, sum + m21); } else { chmax(res, sum); chmax(res, sum + m21 + m22); } return dp[i][f] = res; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; G.resize(n); dp.resize(n); for(int i = 0; i < n; i++) { dp[i].resize(3, -1); } for(int i = 0; i < n - 1; i++) { int u, v, c; cin >> u >> v >> c; --u; --v; G[u].push_back(P(v, c)); G[v].push_back(P(u, c)); } cout << dfs(0, 0, -1) << 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...