結果

問題 No.1488 Max Score of the Tree
ユーザー HIcoder
提出日時 2023-07-21 15:40:32
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,331 bytes
コンパイル時間 1,593 ms
コンパイル使用メモリ 97,248 KB
最終ジャッジ日時 2025-02-15 16:07:52
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12 WA * 17
権限があれば一括ダウンロードができます

ソースコード

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;
for(int i=0;i<N-1;i++){
int a,b;
ll c;
cin>>a>>b>>c;
a--;b--;
//edges[i]=edge(a,b,c,i);
//edges[i]=(edge){a,b,c,i};
edges.emplace_back(a,b,c,i);
G[a].emplace_back(a,b,c,i);
G[b].emplace_back(b,a,c,i);
}
vector<P> dp(N);
//
auto dfs=[&](auto f,int now,int pre,int edgeid)->P{
bool flag=true;
int res=0;
for(edge e:G[now]){
if(e.to==pre) continue;
flag=false;
res+=f(f,e.to,now,e.id).first; //e.to
}
if(flag){
res+=1; //now
}
return dp[now]=make_pair(res,edgeid);
};
for(edge e:G[0]){
dfs(dfs,e.to,0,e.id);
}
//cout<<"te"<<endl;
sort(dp.begin(),dp.end(),[&](const P&lhs, const P&rhs){
return 1LL*lhs.first*(edges[lhs.second].cost) > 1LL*rhs.first*(edges[rhs.second].cost);
});
//cout<<"te"<<endl;
ll add=0;
ll len=0;
for(int i=0;i<N;i++){
if(len+edges[dp[i].second].cost<=K){
add += 1LL*dp[i].first*edges[dp[i].second].cost;
len+=edges[dp[i].second].cost;
//cout<<"edgeid="<<dp[i].second<<endl;
//cout<<"cnt="<<dp[i].first<<endl;
}
}
//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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0