結果

問題 No.1488 Max Score of the Tree
ユーザー HIcoderHIcoder
提出日時 2023-07-21 21:00:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,981 bytes
コンパイル時間 1,160 ms
コンパイル使用メモリ 100,056 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 20:51:08
合計ジャッジ時間 2,158 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 WA -
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 WA -
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 WA -
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<tuple>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;

struct edge{
    int from;
    int to;
    ll cost;
    int id;
    edge(int from_,int to_,ll cost_,int id_):from(from_),to(to_),cost(cost_),id(id_){};
};

int main(){
    int N,K;
    cin>>N>>K;
    vector<vector<edge>> G(N);

    vector<edge> edges;

    vector<int> A(N-1),B(N-1),C(N-1);
    for(int i=0;i<N-1;i++){
        
        cin>>A[i]>>B[i]>>C[i];
        A[i]--;
        B[i]--;

        G[A[i]].emplace_back(A[i],B[i],C[i],i);
        G[B[i]].emplace_back(B[i],A[i],C[i],i);
    }

    vector<int> num(N);//num[i]=iの部分木にある葉の数
    vector<int> depth(N);//0からどれだけ離れているか

    //部分木の葉の数を数える
    auto dfs=[&](auto f,int now,int pre,int d)->int{
        bool flag=true;
        int res=0;

        for(edge e:G[now]){
            if(e.to==pre) continue;
            flag=false;
            res+=f(f,e.to,now,d+1); //e.toの部分木にある葉の数
        }
        depth[now]=d;

        if(flag){
            //now自身が葉
            num[now]=1;
            return 1;
        }

        return num[now]=res;
        
    };

    dfs(dfs,0,-1,0);

    for(int i=0;i<N-1;i++){
        if(depth[A[i]]>depth[B[i]]) swap(A[i],B[i]);
        //depth[A[i]]<depth[B[i]];

        edges.emplace_back(A[i],B[i],C[i],i);

    }

    /*
    for(int i=0;i<N;i++){
        cout<<"depth["<<i<<"]="<<depth[i]<<endl;

        cout<<"num["<<i<<"]="<<num[i]<<endl;
    }
    */
    
    vector<pair<ll,ll>> dp;
 

    for(edge e:edges){

        //depth[e.from]<depth[e.to]

        //e.toの部分木にある頂点数
        dp.emplace_back(1LL*num[e.to]*e.cost,e.cost);


    }
    

    sort(dp.begin(),dp.end(),[&](const P&lhs, const P&rhs){
        if(lhs.first!= rhs.first){
            return lhs.first> rhs.first;
        }else{
            return lhs.second<rhs.second;
        }
    });

    //cout<<"te"<<endl;


    ll add=0;
    ll len=0;
    for(int i=0;i<dp.size();i++){
        //cout<<"dp[i].first="<<dp[i].first<<endl;
        //cout<<"dp[i].second="<<dp[i].second<<endl;
       
        if((len+dp[i].second)<=K){
            //cout<<"dp[i].second="<<dp[i].second<<endl;
        

            add += 1LL*dp[i].first;
            len+=dp[i].second;

        }
    }


    //cout<<"te"<<endl;

    ll tot=0;

    auto dfs2=[&](auto f,int now,int pre,ll depth)->void{
        bool flag=true;
        int res=0;
        for(edge e:G[now]){
            if(e.to==pre) continue;
            flag=false;
            f(f,e.to,now,depth+e.cost);
        }

        if(flag){
            //nowが根の場合のみ 足される
            tot+=depth;
        }
        return;
    };

    dfs2(dfs2,0,-1,0);

    
    //cout<<"te"<<endl;
    //cout<<"tot="<<tot<<endl;
    

    ll ans=add+tot;

    cout<<ans<<endl;


}
0