結果

問題 No.3113 The farthest point
ユーザー iomir
提出日時 2025-04-19 13:36:31
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 2,083 bytes
コンパイル時間 4,322 ms
コンパイル使用メモリ 289,076 KB
実行使用メモリ 19,632 KB
最終ジャッジ日時 2025-04-19 13:36:41
合計ジャッジ時間 7,808 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;

#define all(v) v.begin(),v.end()
using ll = long long;
using ull = unsigned long long;
using lll = __int128;
using vll=vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll,ll>;
using vp=vector<pair<ll, ll>>;
//using mint=modint1000000007;
//using mint=modint998244353;

const ll INF=1ll<<60;
ll mod10=1e9+7;
ll mod99=998244353;
const double PI = acos(-1);

#define rep(i,n) for (ll i=0;i<n;++i)
#define per(i,n) for(ll i=n-1;i>=0;--i)
#define rep2(i,a,n) for (ll i=a;i<n;++i)
#define per2(i,a,n) for (ll i=a;i>=n;--i)

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }

bool solve(){
    ll N1;cin>>N1;
    vector<vp> ab1(N1);
    rep(i,N1-1){
        ll a,b;cin>>a>>b;a--;b--;ll c;cin>>c;
        ab1[a].push_back({c,b});
        ab1[b].push_back({c,a});
    }
 

    vll dp1(N1);
    auto dfs1 = [&](auto&& dfs1,ll a,ll b,vector<vp>& ab,vll& dp) -> void {
        for(auto [c,to]:ab[a]){
            if(to==b) continue;
            dfs1(dfs1,to,a,ab,dp);
            chmax(dp[a],dp[to]+c);
        }
    };
    auto dfs2 = [&](auto&& dfs2,ll a,ll b,ll val,vector<vp>& ab,vll& dp) -> void {
        vector<P> d;
        d.push_back({0,-1});
        for(auto [c,to]:ab[a]){
            if(to==b) d.push_back({val+c,to});
            else d.push_back({c+dp[to],to});
        }
        sort(all(d));reverse(all(d));
        ll sum=d[0].first+d[1].first;
        for(auto [c,to]:ab[a]){
            if(to==b) continue;
            if(to==d[0].second) dfs2(dfs2,to,a,d[1].first,ab,dp);
            else dfs2(dfs2,to,a,d[0].first,ab,dp);
        }
        dp[a]=d[0].first;
    };
    dfs1(dfs1,0,0,ab1,dp1);
    dfs2(dfs2,0,0,0,ab1,dp1);
    ll ans=0;
    rep(i,N1) chmax(ans,dp1[i]);
    cout << ans << endl;
    
    return 0;
}


int main(){
   cin.tie(0);
   ios::sync_with_stdio(false);
   ll T=1;//cin>>T;
   rep(i,T) solve();
}
    
0