is a simple tree DP ~ But the question is, how many colors do you want?
I began to write 25 in the color of the hand, and found that very slow ah ... So I did a dead change to three kinds of handed up to unexpectedly past ... After the ... (To know 2 colors must be wrong ah ...) )
1 /**************************************************************2 problem:13693 User:rausen4 language:c++5 result:accepted6 Time:12 Ms7 memory:1820 KB8 ****************************************************************/9 Ten#include <cstdio> One#include <algorithm> A - using namespacestd; - Const intN = 1e4 +5; the Const intINF =1e9; - Const intC =3; - - structEdge { + intnext, to; - Edge () {} +Edgeint_n,int_t): Next (_n), to (_t) {} A} e[n <<1]; at - intN, ans; - intfirst[n], tot; - intf[n][ +]; - -InlineintRead () { in intx =0; - CharCH =GetChar (); to while(Ch <'0'||'9'<ch) +CH =GetChar (); - while('0'<= CH && Ch <='9') { thex = x *Ten+ CH-'0'; *CH =GetChar (); $ }Panax Notoginseng returnx; - } the + AInlinevoidAdd_edges (intXinty) { theE[++tot] = Edge (first[x], y), first[x] =tot; +E[++tot] = Edge (First[y], x), first[y] =tot; - } $ $ #defineY e[x].to - voidWorkintPintFA) { - intx, I, J, MN; the for(i =1; I <= C; ++i) F[p][i] =i; - for(x = first[p]; x; x =e[x].next)Wuyi if(Y! =fa) Work (Y, p); the for(i =1; I <= C; ++i) { - for(x = first[p]; x; x =e[x].next) Wu if(Y! =FA) { - for(j =1, MN = inf; J <= C; ++j) About if(I! = j) mn =min (mn, f[y][j]); $F[p][i] + =mn; - } - } - } A #undefY + the intMain () { - inti; $n =read (); the for(i =1; I < n; ++i) the add_edges (read (), read ()); theWork1,0); the for(i =1, ans = inf; I <= C; ++i) -ans = min (ans, f[1][i]); inprintf"%d\n", ans); the return 0; the}
View Code
BZOJ1369 [Baltic2003]gem