Conclusion question ... The number of colors used in a tree does not exceed Logn.
F[I][J] denotes a subtree with I as the root, the color of I is the scheme number of J.
G[I][J] = Max{f[i][k]}, (k!=j
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intmaxn=10233, inf=1e9+233;7 structzs{intToo,pre;} e[maxn<<1];intTOT,LAST[MAXN];8 intf[maxn][ at],g[maxn][ at];9 inti,j,k,n,m;Ten One intRaCharRx; AInlineintRead () { -Rx=getchar (), ra=0; - while(rx<'0'|| Rx>'9') rx=GetChar (); the while(rx>='0'&&rx<='9') ra*=Ten, ra+=rx- -, Rx=getchar ();returnRA; - } - - voidDfsintXintFA) {//printf ("%d-->%d\n", fa,x); + inti,j,mn=0, nd=0; - for(I=last[x];i;i=e[i].pre)if(e[i].too!=FA) { + DFS (e[i].too,x); A for(j=1;j< at; j + +) f[x][j]+=G[e[i].too][j]; at } -f[x][0]=inf; - for(i=1;i< at; i++){ -f[x][i]+=i; - if(F[x][i]<f[x][mn]) nd=mn,mn=i; - Else if(F[x][i]<f[x][nd]) nd=i; in } - for(i=1;i< at; i++)if(I!=MN) g[x][i]=f[x][mn];Elseg[x][i]=F[x][nd]; to } +InlinevoidInsertintAintb) { -e[++tot].too=b,e[tot].pre=last[a],last[a]=tot, thee[++tot].too=a,e[tot].pre=last[b],last[b]=tot; * } $ Panax Notoginseng intMain () { -n=read (); the for(i=1; i<n;i++) Insert (read (), read ()); +Dfs1,0); A intAns=min (g[1][1],f[1][1]); theprintf"%d\n", ans); + return 0; - } $
View Code
[bzoj1369] [Baltic2003] Gem