Look for the tree on the length of the K-point pair, the partition code on the tree and this almost, change the judgment of the good
#include <iostream>#include<algorithm>#include<cstdio>#include<vector>#include<string.h>using namespacestd;Const intmaxn=10004;inth[maxn],nx[maxn*2],to[maxn*2],numofe,dist[maxn*2];voidAddintUintVintd) {Numofe++; Dist[numofe]=D; To[numofe]=v; Nx[numofe]=H[u]; H[u]=Numofe;}voidInitintN) {Numofe=0; memset (H,0,sizeof(H));}intans;intsubnum[maxn],q[maxn+Ten],FA[MAXN];BOOLCENTER[MAXN];intn,k;intSearchRoot (intcur) { intRear=0, root=cur; Q[rear++]=cur;fa[cur]=0; for(intI=0; i<rear; i++) { intx=Q[i]; for(intJ=H[X]; J j=Nx[j])if(to[j]!=fa[x]&¢er[to[j]]==false) Q[rear++]=to[j],fa[to[j]]=x; } intmin=MAXN; for(inti=rear-1; i>=0; i--) { intx=P[i]; SUBNUM[X]=1; intMa=0; for(intJ=H[X]; J; j=Nx[j])if(to[j]!=fa[x]&¢er[to[j]]==false) MA=max (Ma,subnum[to[j]), subnum[x]+=subnum[To[j]]; MA=max (ma,rear-subnum[x]); if(MIN>MA) min=ma,root=x; } returnRoot;}intP[MAXN];intCount_pair (intSintt) { intGe=0, l=t,r=T; for(intI=s; i<t; i++) { while(r>s&&p[i]+p[r-1]>K) r--; while(l>s&&p[i]+p[l-1]>=K) l--; if(I>=l&&i<r) ge+=r-l-1; Elsege+=r-l; } returnge/2;}voidUPDATEEDG (intSintCurintd) { intRear=0; Q[rear++]=cur;fa[cur]=0; p[s]=D; for(intI=0; i<rear; i++) { intx=Q[i]; for(intJ=h[x];j; j=Nx[j]) { inttto=To[j]; if(center[tto]| | TTO==FA[X])Continue; P[s+rear]=p[s+i]+dist[j],q[rear++]=tto,fa[tto]=x; }} sort (P+s,p+s+rear);}intDfsintSintCurintd) { introot,tot=1; Root=searchroot (cur); Center[root]=true; for(intI=h[root]; i; i=Nx[i]) { inttto=To[i]; if(Center[tto])Continue; intN=dfs (s+Tot,tto,dist[i]); if(ans>0) {Center[root]=false;return 0; } ans-=count_pair (s+tot,s+tot+N); Tot+=N; } P[s]=0; Sort (P+s,p+s+tot); Ans+=count_pair (s,s+tot); if(ans>0) {Center[root]=false;return 0; } Center[root]=false; UPDATEEDG (S,CUR,D); returntot;}intMain () { for(;;) {scanf ("%d", &n);if(n==0) Break; Init (N); for(intI=1;i<=n; i++) { intD; for(;;) {scanf ("%d", &d);if(d==0) Break; intC scanf"%d",&c); Add (i,d,c); add (d,i,c); } } for(;;) {scanf ("%d", &k);if(k==0) Break; Ans=0; DFS (0,1,0); if(ans>0) puts ("AYE"); ElsePuts"NAY"); } puts ("."); } return 0;}/*1 3 1 4 1 005 1 6 1 00002*/
View Code
poj2114 look for a tree with a length of K-point pair, a partition on a tree