結果

問題 No.872 All Tree Path
ユーザー ysystem7ysystem7
提出日時 2020-08-22 13:35:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 553 ms / 3,000 ms
コード長 1,550 bytes
コンパイル時間 2,160 ms
コンパイル使用メモリ 207,136 KB
実行使用メモリ 57,984 KB
最終ジャッジ日時 2024-04-23 08:01:46
合計ジャッジ時間 8,083 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 520 ms
43,356 KB
testcase_01 AC 539 ms
43,336 KB
testcase_02 AC 529 ms
43,392 KB
testcase_03 AC 249 ms
57,984 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 516 ms
43,392 KB
testcase_06 AC 547 ms
43,264 KB
testcase_07 AC 553 ms
43,344 KB
testcase_08 AC 27 ms
7,424 KB
testcase_09 AC 27 ms
7,424 KB
testcase_10 AC 27 ms
7,296 KB
testcase_11 AC 26 ms
7,296 KB
testcase_12 AC 26 ms
7,296 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<n;i++)
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using Graph = vector<vector<int>>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

const ll INF=1e18;

V<ll> cnt;
V<ll> par;
V<V<ll>> G;

void dfs(ll v,ll p){
    for(ll u:G[v]){
        if(u==p) continue;
        dfs(u,v);
        cnt[v]+=cnt[u];
    }
    cnt[v]++;
    par[v]=p;
}

struct edge{
    ll to;
    ll cost;
};

int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    cnt.resize(n);
    G.resize(n);
    par.resize(n);
    map<P,ll> mp;
    rep(i,n-1){
        ll u,v,w;
        cin>>u>>v>>w;
        u--;v--;
        G[u].push_back(v);
        G[v].push_back(u);
        mp[P(u,v)]=mp[P(v,u)]=w;
    }
    dfs(0,-1);
    ll ans=0;
    rep(i,n){
        if(cnt[i]==n) continue;
        ll v=par[i];
        ans+=2*cnt[i]*(n-cnt[i])*mp[P(i,v)];
    }
    cout<<ans<<endl;
}
0