设f[i][j]表示以i为根的子树,i与父亲之间的边染成j的最小代价

DP的过程中转移时相当于求一个最小权匹配,用费用流即可

感觉复杂度飞起来了…

#include<cstdio>
const int inf=~0U>>2,N=30000,M=1000110,MAXN=160;
int n,x,y,i,j;
int u[M],v[M],c[M],co[M],nxt[M],t,S,T,l,r,q[M],g[N],f[N],d[N];bool in[N];
int G[MAXN],NXT[MAXN<<1],V[MAXN<<1],ed,dp[MAXN][MAXN];
inline void ADD(int x,int y){V[++ed]=y;NXT[ed]=G[x];G[x]=ed;}
inline void add(int x,int y,int z,int zo){
u[++t]=x;v[t]=y;c[t]=z;co[t]=zo;nxt[t]=g[x];g[x]=t;
u[++t]=y;v[t]=x;c[t]=0;co[t]=-zo;nxt[t]=g[y];g[y]=t;
}
inline bool spfa(){
int x,i;
for(i=1;i<=T;i++)d[i]=inf;
d[S]=0;in[S]=1;l=r=M>>1;q[l]=S;
while(l<=r){
int x=q[l++];
if(x==T)continue;
for(i=g[x];i;i=nxt[i])if(c[i]&&co[i]+d[x]<d[v[i]]){
d[v[i]]=co[i]+d[x];f[v[i]]=i;
if(!in[v[i]]){
in[v[i]]=1;
if(d[v[i]]<d[q[l]])q[--l]=v[i];else q[++r]=v[i];
}
}
in[x]=0;
}
return d[T]<inf;
}
void solve(int x,int pre){
int i,j,k,now,cnt=0;
for(i=G[x];i;i=NXT[i])if(V[i]!=pre)solve(V[i],x),cnt++;
if(x==1){
if(!cnt)return;
t=1;S=0;T=n*(cnt+1);
for(i=0;i<=T;i++)g[i]=in[i]=0;
for(j=1;j<n;j++)add(j,T,j!=now,0);
for(j=n-1,i=G[x];i;j+=n-1,i=NXT[i])if(V[i]!=pre)for(add(S,++j,1,0),k=1;k<n;k++)add(j,j+k,1,dp[V[i]][k]),add(j+k,k,1,0);
while(spfa())for(i=T;i!=S;i=u[f[i]])--c[f[i]],++c[f[i]^1],dp[x][0]+=co[f[i]];
}else for(now=1;now<n;now++){
dp[x][now]=now;
if(!cnt)continue;
t=1;S=0;T=n*(cnt+1);
for(i=0;i<=T;i++)g[i]=in[i]=0;
for(j=1;j<n;j++)add(j,T,j!=now,0);
for(j=n-1,i=G[x];i;j+=n-1,i=NXT[i])if(V[i]!=pre)for(add(S,++j,1,0),k=1;k<n;k++)add(j,j+k,1,dp[V[i]][k]),add(j+k,k,1,0);
while(spfa())for(i=T;i!=S;i=u[f[i]])--c[f[i]],++c[f[i]^1],dp[x][now]+=co[f[i]];
}
}
int main(){
scanf("%d",&n);
for(i=1;i<n;i++)scanf("%d%d",&x,&y),ADD(x,y),ADD(y,x);
solve(1,0);
return printf("%d",dp[1][0]),0;
}

  

BZOJ3742 : Painting的更多相关文章

  1. CF448C Painting Fence (分治递归)

    Codeforces Round #256 (Div. 2) C C. Painting Fence time limit per test 1 second memory limit per tes ...

  2. [译]使用Continuous painting mode来分析页面的绘制状态

    Chrome Canary(Chrome “金丝雀版本”)目前已经支持Continuous painting mode,用于分析页面性能.这篇文章将会介绍怎么才能页面在绘制过程中找到问题和怎么利用这个 ...

  3. Codeforces Round #353 (Div. 2)Restoring Painting

    Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was sto ...

  4. hdu-4810 Wall Painting(组合数学)

    题目链接: Wall Painting Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. Codeforces Gym 100342C Problem C. Painting Cottages 转化题意

    Problem C. Painting CottagesTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  6. F面经:painting house

    There are a row of houses, each house can be painted with three colors red, blue and green. The cost ...

  7. HDU 3685 Rotational Painting(多边形质心+凸包)(2010 Asia Hangzhou Regional Contest)

    Problem Description Josh Lyman is a gifted painter. One of his great works is a glass painting. He c ...

  8. HDU 4810 Wall Painting

    Wall Painting Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. UVA 253 Cube painting(暴力打表)

    Cube painting Problem Description: We have a machine for painting cubes. It is supplied with three d ...

随机推荐

  1. Linux命令之exit - 退出当前shell【返回值状态】

    原文链接:http://codingstandards.iteye.com/blog/836625   (转载请注明出处) 用途说明 exit命令用于退出当前shell,在shell脚本中可以终止当前 ...

  2. order by 指定顺序 mysql

    LOCATE(substr,str), LOCATE(substr,str,pos) 第一个语法返回字符串str第一次出现的子串SUBSTR的位置.第二个语法返回第一次出现在字符串str的子串SUBS ...

  3. Dynamic Web Module 3.0 requires Java 1.6 or newer

    在maven工程的pom.xml文件中加入如下代码: 在<build>里面加入如下代码: <plugins> <plugin> <groupId>org ...

  4. 1.2 中国象棋将帅问题进一步讨论与扩展:如何用1个变量实现N重循环?[chinese chess]

    [题目] 假设在中国象棋中只剩下将帅两个棋子,国人都知道基本规则:将帅不能出九宫格,只能上下左右移动,不能斜向移动,同时将帅不能照面.问在这样条件下,所有可能将帅位置.要求在代码中只能使用一个字节存储 ...

  5. TestPointer

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  6. ReverseString

    [本文链接] http://www.cnblogs.com/hellogiser/p/reverse-string.html reverse string [代码]  C++ Code  123456 ...

  7. 快速排序模板qsort(转载)

     qsort  用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)); 各 ...

  8. HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  9. input框颜色修该

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. (*p)++和*(p++)和*p++的区别

    * 和++优先级是同一级别,同一级别按照从右往左的顺序计算 所以:  *p++等价于*(p++) #define debug(x) cout << #x << " a ...