#pragma region template #pragma GCC optimize("Ofast") #include using namespace std; using ll=long long; using ld=long double; using vi=vector; using vll=vector; using pi=pair; using pll=pair; #define overload2(a,b,c,...) c #define overload3(a,b,c,d,...) d #define overload4(a,b,c,d,e,...) e #define overload5(a,b,c,d,e,f,...) f #define TYPE1(T) template #define TYPE2(T,U) template #define TYPE(...) overload2(__VA_ARGS__,TYPE2,TYPE1)(__VA_ARGS__) #define TYPES1(T) template #define TYPES2(H,T) template #define TYPES(...) overload2(__VA_ARGS__,TYPES2,TYPES1)(__VA_ARGS__) #define REP4(i,s,n,d) for(int i=(s);i<(n);i+=(d)) #define REP3(i,s,n) REP4(i,s,n,1) #define REP2(i,n) REP3(i,0,n) #define REP1(n) REP2(tomato,n) #define REP(...) overload4(__VA_ARGS__,REP4,REP3,REP2,REP1)(__VA_ARGS__) #define RREP4(i,n,s,d) for(int i=(n)-1;i>=s;i-=d) #define RREP3(i,n,s) RREP4(i,n,s,1) #define RREP2(i,n) RREP3(i,n,0) #define RREP1(n) RREP2(tomato,n) #define RREP(...) overload4(__VA_ARGS__,RREP4,RREP3,RREP2,RREP1)(__VA_ARGS__) #define FOR4(a,b,c,d,v) for(auto [a,b,c,d]:v) #define FOR3(a,b,c,v) for(auto [a,b,c]:v) #define FOR2(a,b,v) for(auto [a,b]:v) #define FOR1(a,v) for(auto a:v) #define FOR(...) overload5(__VA_ARGS__,FOR4,FOR3,FOR2,FOR1)(__VA_ARGS__) #define AFOR4(a,b,c,d,v) for(auto&[a,b,c,d]:v) #define AFOR3(a,b,c,v) for(auto&[a,b,c]:v) #define AFOR2(a,b,v) for(auto&[a,b]:v) #define AFOR1(a,v) for(auto&a:v) #define AFOR(...) overload5(__VA_ARGS__,AFOR4,AFOR3,AFOR2,AFOR1)(__VA_ARGS__) #define CFOR4(a,b,c,d,v) for(const auto&[a,b,c,d]:v) #define CFOR3(a,b,c,v) for(const auto&[a,b,c]:v) #define CFOR2(a,b,v) for(const auto&[a,b]:v) #define CFOR1(a,v) for(const auto&a:v) #define CFOR(...) overload5(__VA_ARGS__,CFOR4,CFOR3,CFOR2,CFOR1)(__VA_ARGS__) #define ALL(v) v.begin(),v.end() #define RALL(v) v.rbegin(),v.rend() #define SORT(v) sort(ALL(v)) #define RSORT(v) sort(RALL(v)) #define REVERSE(v) reverse(ALL(v)) #define UNIQUE(v) SORT(v),v.erase(unique(ALL(v)),v.end()) TYPES(T) void input(T&... a){ (cin>>...>>a); } #define DECLARE(T,...) T __VA_ARGS__;input(__VA_ARGS__); #define INT(...) DECLARE(int,__VA_ARGS__) #define STR(...) DECLARE(string,__VA_ARGS__) #define LL(...) DECLARE(long long,__VA_ARGS__) #define CHR(...) DECLARE(char,__VA_ARGS__) #define DBL(...) DECLARE(double,__VA_ARGS__) #define VI(n,v) vi v(n);cin>>v; #define VLL(n,v) vll v(n);cin>>v; TYPE(T) istream&operator>>(istream&is,vector&v){ for(auto&a:v)cin>>a; return is; } TYPE(T) ostream&operator<<(ostream&os,const vector&v){ if(&os==&cerr)os<<"["; REP(i,v.size()){ os<>(istream&is,pair&p){ cin>>p.first>>p.second; return is; } #ifdef __LOCAL #include #else #define debug(...) void(0) #endif void print(){ cout << '\n'; } TYPES(T,Ts) void print(const T& a,const Ts&... b){ cout<; TYPE(T) using pqg=priority_queue,greater>; TYPE(T) T pick(queue& que){assert(que.size()); T a=que.front();que.pop();return a;} TYPE(T) T pick(pq& que){assert(que.size()); T a=que.top();que.pop();return a;} TYPE(T) T pick(pqg& que){assert(que.size()); T a=que.top();que.pop();return a;} TYPE(T) T pick(stack& sta){assert(sta.size()); T a=sta.top();sta.pop();return a;} string YES(bool f=true){return (f?"YES":"NO");} string Yes(bool f=true){return (f?"Yes":"No");} string yes(bool f=true){return (f?"yes":"no");} constexpr int INF=1e9+7; constexpr ll LINF=ll(1e18)+7; constexpr ld EPS=1e-10; vi iota(int n){vi a(n);iota(ALL(a),0);return a;} TYPE(T) vector> query_sort(const vector&v){ vector> res(v.size()); REP(i,v.size())res[i]={v[i],i}; SORT(res); return res; } TYPE(T) T rev(T a){ REVERSE(a);return a; } TYPE(T) void fin(T a){cout<b&&(a=b,true));} TYPES(T,Ns) auto make_vector(T x,int n,Ns ...ns){ if constexpr(sizeof...(ns)==0)return vector(n,x); else return vector(n,make_vector(x,ns...)); } bool in(const ll S,const int a){return (S>>a)&1;} int popcount(const ll S){return __builtin_popcountll(S);} int digit(char c){ return (c>='0' and c<='9' ? c-'0' : -1);} #pragma endregion template template struct WeightedEdge{ WeightedEdge()=default; WeightedEdge(int from,int to,T weight):from(from),to(to),weight(weight){} int from,to; T weight; }; template struct WeightedGraph{ int n; using weight_type=T; using edge_type=WeightedEdge; vector edges; protected: vector in_deg; bool prepared; class OutgoingEdges{ WeightedGraph* g; int l,r; public: OutgoingEdges(WeightedGraph* g,int l,int r):g(g),l(l),r(r){} edge_type* begin(){ return &(g->edges[l]); } edge_type* end(){ return &(g->edges[r]); } edge_type& operator[](int i){ return g->edges[l+i]; } int size()const{ return r-l; } }; public: OutgoingEdges operator[](int v){ assert(prepared); return { this,in_deg[v],in_deg[v+1] }; } bool is_prepared()const{ return prepared; } WeightedGraph():n(0),in_deg(1,0),prepared(false){} WeightedGraph(int n):n(n),in_deg(n+1,0),prepared(false){} WeightedGraph(int n,int m,bool directed=false,int indexed=1): n(n),in_deg(n+1,0),prepared(false){ scan(m,directed,indexed); } void resize(int n){n=n;} void add_arc(int from,int to,T weight){ assert(!prepared); assert(0<=from and from>u>>v;u-=indexed;v-=indexed; T weight;cin>>weight; if(directed)add_arc(u,v,weight); else add_edge(u,v,weight); } build(); } void build(){ assert(!prepared);prepared=true; for(int v=0;v new_edges(in_deg.back()); auto counter=in_deg; for(auto&&e:edges)new_edges[ counter[e.from]++ ]=e; edges=new_edges; } void graph_debug()const{ #ifndef __LOCAL return; #endif assert(prepared); for(int from=0;from struct WeightedTree:WeightedGraph{ using WeightedGraph::WeightedGraph; using edge_type=typename WeightedGraph::edge_type; using OutgoingEdges=typename WeightedGraph::OutgoingEdges; using WeightedGraph::n; using WeightedGraph::in_deg; int root=-1; vector DFS,BFS,depth; void scan_root(int indexed=1){ for(int i=1;i>p; T weight;cin>>weight; add_edge(p-indexed,i,weight); } build(); } void scan(int indexed=1){ WeightedGraph::scan(n-1,false,indexed); build(); } edge_type& parent(int v){ assert(~root and root!=v); return (*this)[v][0]; } OutgoingEdges son(int v){ assert(~root); if(v==root)return {this,in_deg[v],in_deg[v+1]}; return {this,in_deg[v]+1,in_deg[v+1]}; } private: void dfs(int v,int pre=-1){ for(auto&e:(*this)[v]){ if(e.to==pre)swap((*this)[v][0],e); else{ depth[e.to]=depth[v]+1; dfs(e.to,v); } } DFS.push_back(v); } public: void build(int r=0){ if(!WeightedGraph::is_prepared())WeightedGraph::build(); if(~root){ assert(r==root); return; } root=r; depth=vector(n,0); DFS.reserve(n);BFS.reserve(n); dfs(root); queue que; que.push(root); while(que.size()){ int p=que.front();que.pop(); BFS.push_back(p); for(const auto&e:son(p))que.push(e.to); } } }; template vector centroid_decomposition(TREE&T){ assert(T.is_prepared()); vector used(T.n,false); vector sz(T.n),pre(T.n),res; res.reserve(T.n); auto find_centroid=[&](int v){ vector S{v}; pre[v]=-1; for(int i=0;i que; que.push(0); while(que.size()){ int c=find_centroid(que.front());que.pop(); used[c]=true; res.push_back(c); for(const auto&e:T[c]) if(!used[e.to]) que.push(e.to); } return res; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); INT(n,k); WeightedTree T(n); T.scan(1); ll ans=0; vector used(n,false); auto C=centroid_decomposition(T); auto F=[&](auto F,int v,int pre,int c1,int c2,map,int>&mp)->void{ mp[minmax(c1,c2)]++; for(const auto&e:T[v]){ if(e.to==pre||used[e.to])continue; if(e.weight==c1||e.weight==c2)F(F,e.to,v,c1,c2,mp); else if(c2==-1)F(F,e.to,v,c1,e.weight,mp); } }; for(int v:C){ used[v]=true; map,int> C2; vector C1(k+1,0),C2_sum(k+1,0); int C1_sum=0; for(const auto&e:T[v]){ if(used[e.to])continue; map,int> mp; F(F,e.to,v,e.weight,-1,mp); CFOR(cc,val,mp){ const auto&[c1,c2]=cc; if(~c1){ ans+=val; ans+=C1[c1]*ll(val); ans+=C1[c2]*ll(val); if(C2.count(cc))ans+=C2[cc]*ll(val); } else{ ans+=C2_sum[c2]*ll(val); ans+=(C1_sum-C1[c2])*ll(val); } } CFOR(cc,val,mp){ const auto&[c1,c2]=cc; if(~c1){ C2[cc]+=val; C2_sum[c1]+=val; C2_sum[c2]+=val; } else{ C1[c2]+=val; C1_sum+=val; } } } } fin(ans); }