#include using namespace std; using ll = long long; template using Pa = pair; template using vec = vector; template using vvec = vector>; struct edge{ int to; ll dist,col; edge(int to,ll dist,ll col):to(to),dist(dist),col(col){}; }; class CentroidDecomposition{ public: int N; vvec g; vec size; vec used; CentroidDecomposition(int N,vvec tree): N(N),g(tree){ size = used = vec(N,0); } int calc_size(int cur,int par){ int c = 1; for(auto& x:g[cur]){ if(x.to==par || used[x.to]) continue; c += calc_size(x.to,cur); } return size[cur] = c; } //tは連結成分の大きさ //cur以下のうち、削除して残る最大の部分木の大きさを返す Pa search_centroid(int cur,int par,int cmp_size){ Pa res = {1e9,-1}; int s = 1,ma = 0; for(auto& x:g[cur]){ if(x.to==par || used[x.to]) continue; res = min(res,search_centroid(x.to,cur,cmp_size)); ma = max(ma,size[x.to]); s += size[x.to]; } //子と親の部分木の大きい方 ma = max(ma,cmp_size-s); res = min(res,{ma,cur}); return res; } void dfs(int cur,int par,ll d){ for(auto& e:g[cur]) if(e.to!=par && !used[e.to]){ dfs(e.to,cur,d+e.dist); } } int build(int v){ calc_size(v,-1); int centroid = search_centroid(v,-1,size[v]).second; used[centroid] = true; return centroid; } void disable(int v){used[v] = true;} bool is_alive(int v){return !used[v];} }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N,K; cin >> N >> K; vvec g(N); for(int i=0;i> a >> b >> c; a--; b--; g[a].push_back({b,1,c}); g[b].push_back({a,1,c}); } CentroidDecomposition CD(N,g); queue Q; ll ans = 0; Q.push(0); while(!Q.empty()){ int c = CD.build(Q.front()); Q.pop(); map S,Sp; map,ll> D; int n = g[c].size(); vec> Sv(n),Spv(n); vec,ll>> Dv(n); ll cnt1 = 0; auto dfs = [&](auto&& self,int cur,int par,int id,int c1=-1,int c2=-1)->void{ assert(c1!=c2); cerr << "centroid : " << c << " cur : " << cur << "\n"; if(c1!=-1 && c2==-1){ S[c1]++; Sv[id][c1]++; cnt1++; } if(c1!=-1 && c2!=-1){ D[minmax({c1,c2})]++; Sp[c1]++; Sp[c2]++; Dv[id][minmax({c1,c2})]++; Spv[id][c1]++; Spv[id][c2]++; ans++; } for(auto& e:g[cur]) if(e.to!=par && CD.is_alive(e.to)){ if(c2==-1){ if(c1!=e.col) self(self,e.to,cur,id,c1,e.col); else self(self,e.to,cur,id,c1,c2); }else{ if(c1==e.col || c2==e.col) self(self,e.to,cur,id,c1,c2); } } }; for(int i=0;i