ferinの競プロ帳

競プロについてのメモ

FII Code Round #1 Sugarel in Love

問題ページ

解法

dp[i][j] = (頂点i以下の部分木で使用したパスにおいて頂点iの次数がjのときの最大値) としてdpする。(頂点vの部分木において)頂点vを端点とする辺を使用しない場合、子の部分木はどのような選び方をしていたとしても問題ないため遷移は dp[v][0] = sum(max(dp[child[v]][j])) となる。

頂点vの子の頂点aとの辺を利用したとすると頂点aの分の距離はdp[a][1]+(辺v-aの重み)で頂点a以外の他の子bの分はsum(max(dp[b][j]))となる。頂点vに隣接した辺を使わない場合から辺v-aを使った場合に変化させたとき、dpの値の変化はdp[a][1] + (辺v-aの重み) - max(dp[a][i])となる。この変化する値が大きい上位2つを保持することでつなげるべき辺を求めることができdpの更新を行うことができる。

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
// #define int ll
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()

template<typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }

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); }

template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')'; return out;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
  for(T& x: vec) {is >> x;} return is;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'['; for(T i: a) {out<<i<<',';} out<<']'; return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL
const int INF = 1<<30;
const ll LLINF = 1LL<<60;
const ll MOD = 998244353;

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n;
    cin >> n;
    vector<vector<PII>> g(n);
    REP(i, n-1) {
        ll x, y, z;
        cin >> x >> y >> z;
        x--, y--;
        g[x].push_back({y, z});
        g[y].push_back({x, z});
    }

    vector<vector<ll>> dp(n, vector<ll>(3));
    function<void(ll,ll)> dfs = [&](ll v, ll p) {
        ll ma1 = 0, ma2 = 0, su = 0;
        for(auto to: g[v]) {
            if(to.first == p) continue;
            dfs(to.first, v);
            ll mx = max({dp[to.first][0], dp[to.first][1], dp[to.first][2]});
            su += mx;
            if(ma1 < dp[to.first][1] + to.second - mx) {
                ma2 = ma1;
                ma1 = dp[to.first][1] + to.second - mx;
            } else if(ma2 < dp[to.first][1] + to.second - mx) {
                ma2 = dp[to.first][1] + to.second - mx;
            }
        }
        dp[v][0] = su;
        dp[v][1] = ma1 + su;
        dp[v][2] = ma1 + ma2 + su;
    };
    dfs(0, -1);

    cout << max({dp[0][0], dp[0][1], dp[0][2]}) << endl;

    return 0;
}