Topic: https://www.acwing.com/problem/content/254/
Meaning of the questions: find a tree, path <= k of how many
Thinking: dotted rule, we solve the two pointers operator functions, firstly calculated the path of each row of a sequence, we ensure monotonic, then l from front to back, from the R & lt forward, if l + r <= m then (l, r-1) ( l, r-2) ... are possible, it can be directly coupled with the total number, and if not r--, satisfies l ++, this example can write their own understandable
#include <bits / STDC ++ H.> #define MAXN 100005 #define MOD 1,000,000,007 the using namespace STD; typedef Long Long LL; LL DA; Vector <pair <LL, LL>> MP [MAXN]; // save enough FIG BOOL VIS [MAXN]; // tag have used gravity LL MAXSIZE [MAXN], DIS [MAXN], D [MAXN]; // MAXSIZE largest subtree of the current node LL SIZ [MAXN], E [MAXN]; // dis the distance to the center of gravity d distance appeared LL n-, m, rt, SUM, QE; // distance rt representative of the number of the current node subtree e siz gravity current occurring void Find (X LL, LL F) { / / find the center of gravity of SIZ [X] = . 1 ; MAXSIZE [X] = 0 ; for ( int I = 0 ; I <MP [X] .size (); I ++ ) { pair <LL, LL> Q = MP [X] [I]; IF (= q.first VIS || F = [q.first]) Continue ; // VIS array tags have used gravity Find (q.first, X); SIZ [X] + = SIZ [q.first]; MAXSIZE [X] = max (MAXSIZE [X], SIZ [q.first]); } MAXSIZE [X] = max (MAXSIZE [X], SUM-SIZ [X]); // total number of nodes by subtracting the current number of the current subtree = is the root node of the tree the number of father idea IF (MAXSIZE [X] < MAXSIZE [RT]) { RT =x; } } void get_dis(ll x,ll f,ll len){ if(len<=1e7){ e[++qe]=len; } for(int i=0;i<mp[x].size();i++){ pair<ll,ll> q=mp[x][i]; if(q.first==f||vis[q.first]) continue; dis[q.first]=dis[x]+len; get_dis(q.first,x,len+q.second); } } ll solve(ll x,ll len){ ll ee=0; qe=0; dis[x]=len; get_dis(x,0,len); sort(e+1,e+qe+1); ll l=1,r=qe; while(l<r){ if(e[l]+e[r]<=m){ ee+=r-l; l++; } else{ r--; } } return ee; } void divide(ll x){ da+=solve(x,0); vis[x]=1; for(int i=0;i<mp[x].size();i++){ pair<ll,ll> q=mp[x][i]; if(vis[q.first]) continue; da-=solve(q.first,q.second); sum=siz[q.first]; rt=0; maxsize[rt]=mod; find(q.first,x); divide(rt); } } void init(){ da=0; for(int i=0;i<=n;i++) mp[i].clear(); memset(maxsize,0,sizeof(maxsize)); memset(vis,0,sizeof(vis)); } int main(){ while(cin>>n>>m) { if(n==0&&m==0) break; ll a,b,c; init(); for(int i=0;i<n-1;i++){ cinA >> B >> >> C; A ++ ; B ++ ; MP [A] .push_back (the make_pair (B, C)); MP [B] .push_back (the make_pair (A, C)); } SUM n-=; // current number of nodes RT = 0 ; MAXSIZE [ 0 ] = MOD; // set the initial value Find ( . 1 , 0 ); Divide (RT); the printf ( " % LLD \ n- " , DA); } }