#include #include #include #include #include #include #include using namespace std; typedef pair 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> G(N); vector edges; for(int i=0;i>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

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"< 1LL*rhs.first*(edges[rhs.second].cost); }); //cout<<"te"<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"<