Test instructions: The tree of a fee does not exceed the path of M, so that the total length as large as possible.
Ginseng First Tree Division, purple book on the idea of more clear, here no longer repeat.
When implemented, use a time-like thing to record the time of the node's first visit and save the node sequence.
When merging, use direct map. Almost like defense lines.
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 3e4+5;intn,m;intHD[MAXN], nx[maxn<<1], to[maxn<<1], dam[maxn<<1], len[maxn<<1], Ec;inlinevoidAddintUintVintDintL) {To[ec]=v; DAM[EC]=D; LEN[EC]=L; NX[EC]=Hd[u]; Hd[u]= ec++;}introot, best;intCT[MAXN];//Best = nvoidGetbarycenter (intUintf) {Ct[u]=1; intMx =0; for(inti = Hd[u]; ~i; i =Nx[i]) { if(To[i]! =f) {Getbarycenter (to[i], u); Ct[u]+=Ct[to[i]]; Mx=Max (Mx,ct[to[i]]); }} Mx= Max (mx,n-Ct[u]); if(Mx <Best ) { Best=Mx; Root=u; }}#defineMP Make_pair#defineFi first#defineSe SecondintC[MAXN], L[maxn];map<int,int>Mp;map<int,int>:: iterator it;intans;intPATH[MAXN];intPRE[MAXN], DFS_CLK;//dfs_clk = 0, ans = 0;voidDfsintU = root,intf =0) {PATH[DFS_CLK]=u; Pre[u]= dfs_clk++; C[u]=0; L[u] =0; for(inti = Hd[u]; ~i; i =Nx[i]) { if(To[i]! =f) { intv =To[i]; DFS (V,U); for(intj = Pre[v]; J < Dfs_clk; J + +){ intx =Path[j]; C[X]+=Dam[i]; L[X]+=Len[i]; } } } //Once the subtree has been accessed, it is uniformly processed to ensure no interference, so that the MP can be set as a global variablemp.clear (); for(inti = Hd[u]; ~i; i =Nx[i]) { intNEX =Nx[i]; intLim = ~nex?Pre[to[nex]]: DFS_CLK; if(To[i]! =f) { intv =To[i]; for(intj = Pre[v]; J < Lim; J + +){ intx =Path[j]; if(C[x] <=m) {ans=Max (ans,l[x]); It= Mp.upper_bound (m-c[x]);//find a key <= C[x] if(It! =Mp.begin ()) {ans= Max (ans, (--it)->second+l[x]); } } } for(intj = Pre[v]; J < Lim; J + +){ intx =Path[j]; if(C[x] <=m) {It=Mp.lower_bound (c[x]); BOOLSWC =false; if(It = = Mp.begin () | | (SWC =true, L[x] > (--it),se)) { if(SWC) it++; while(It! = Mp.end () && it->se <= l[x]) mp.erase (it++); if(It = = Mp.end () | | it->fi > C[X]) {//Lower_bound may make it point to key = = C[x] and Val >= L[x]Mp.insert (MP (c[x],l[x)); } } } } } }}//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif intT, KS =0; Cin>>T; while(t--) {scanf ("%d%d",&n,&m); memset (HD+1,-1,sizeof(int) *n); EC =0; for(inti = n; --i;) {inta,b,d,l; scanf"%d%d%d%d",&a,&b,&d,&L); Add (a,b,d,l); Add (b,a,d,l); } Best= n+1; Getbarycenter (1,0); DFS_CLK= ans =0; DFS (); printf ("Case %d:%d\n",++Ks,ans); } return 0;}
UVA 12161 Ironman Race in Treeland (tree Division)