Title Address: Spoj 1825
In addition to the problem of the tree divided into the template is the title of gold ... This question is a thesis question, think for a long time .... Finally,,,, pay attention to a pit point, if the weight is all negative, you can not choose any side, so the weight of 0 ... This means that the initial value should be set to 0 ...
The application of divide-and-conquer algorithm in the path problem of tree is concretely seen in this paper.
The code is as follows:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>#include <time.h>using namespace STD;#define LL __int64#define PI ACOs ( -1.0)//#pragma comment (linker, "/stack:1024000000")Const intMod=1e9+7;Const intinf=0x3f3f3f3f;Const Doubleeqs=1e-9;Const intmaxn=200000+Ten;intN, K, HEAD[MAXN], CNT, root, min1, ans;intF[MAXN];//f[i] Indicates the maximum length of the path of all the subtrees that were traversed before the subtree, no more than I black dotsintG[MAXN];//indicates the longest path with a strictly I black dotintSIZ[MAXN], COLOR[MAXN], VIS[MAXN], NUM[MAXN];structnode{intU, V, W, next;} edge[maxn<<1];structn{intV, NUM, W;} T[MAXN];BOOLCMP (n x, n y) {returnX.num<y.num;}voidAddintUintVintW) {edge[cnt].v=v; Edge[cnt].w=w; Edge[cnt].next=head[u]; head[u]=cnt++;}voidInit () {memset(head,-1,sizeof(head));memset(Color,0,sizeof(color));memset(Vis,0,sizeof(VIS)); Cnt=0;}voidGetroot (intUintFaints) {intI, max1=-1; for(i=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(v==fa| | VIS[V])Continue; Getroot (v,u,s); Max1=max (Max1,siz[v]); } max1=max (Max1,s-siz[u]);if(MIN1>MAX1) {min1=max1; Root=u; }}voidGetSize (intUintFA) {siz[u]=1; for(inti=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(v==fa| | VIS[V])Continue; GetSize (V,u); SIZ[U]+=SIZ[V]; }}voidGetnum (intUintFA) {Num[u]=color[u]; for(inti=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(v==fa| | VIS[V])Continue; Getnum (V,u); Num[u]=max (Num[u],num[v]+color[u]); }}voidGETG (intUintFaintDepintval) {G[dep]=max (g[dep],val); for(inti=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(v==fa| | VIS[V])Continue; GETG (V,U,DEP+COLOR[V],VAL+EDGE[I].W); }}voidWorkintu) {vis[u]=1;intI, J; for(i=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(Vis[v])Continue; GetSize (v,-1); Min1=inf; Getroot (v,-1, Siz[v]); Work (root); }inttot=0; for(i=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(Vis[v])Continue; Getnum (v,-1); T[tot].v=v; T[TOT].NUM=NUM[V]; T[TOT].W=EDGE[I].W; tot++; } sort (t,t+tot,cmp);intLim=k-color[u]; for(i=0; i<=t[tot-1].num;i++) F[i]=-inf; for(i=0; i<tot;i++) { for(j=0; j<=t[i].num;j++) G[j]=-inf; GETG (T[I].V,U,COLOR[T[I].V],T[I].W);if(i) { for(j=0; j<=t[i].num&&j<=lim;j++) {intTmp=min (lim-j,t[i-1].num);if(F[tmp]==-inf)Continue; Ans=max (Ans,f[tmp]+g[j]); } } for(j=0; j<=t[i].num&&j<=lim;j++) {F[j]=max (f[j],g[j]);if(j) F[j]=max (f[j],f[j-1]); Ans=max (Ans,f[j]); }} vis[u]=0;}intMain () {intM, I, U, V, W, X; while(scanf("%d%d%d", &n,&k,&m)!=eof) {init (); for(i=0; i<m;i++) {scanf("%d", &x); color[x]=1; } for(i=1; i<n;i++) {scanf("%d%d%d", &u,&v,&w); Add (U,V,W); Add (V,U,W); } ans=0; GetSize (1,-1); Min1=inf; Getroot (1,-1, n); Work (root);printf("%d\n", ans); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
SPOJ 1825 Ftour2-free Tour II (Point Division on tree)