#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 num(N);//num[i]=iの部分木にある葉の数 vector 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;ivoid{ 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"<