• codeforces581F. Zublicanes and Mumocrates


    传送门:http://codeforces.com/problemset/problem/581/F

    F. Zublicanes and Mumocrates
    time limit per test
    3 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The election campaigns of both parties include numerous demonstrations on n main squares of the capital of Berland. Each of the n squares certainly can have demonstrations of only one party, otherwise it could lead to riots. On the other hand, both parties have applied to host a huge number of demonstrations, so that on all squares demonstrations must be held. Now the capital management will distribute the area between the two parties.

    Some pairs of squares are connected by (n - 1) bidirectional roads such that between any pair of squares there is a unique way to get from one square to another. Some squares are on the outskirts of the capital meaning that they are connected by a road with only one other square, such squares are called dead end squares.

    The mayor of the capital instructed to distribute all the squares between the parties so that the dead end squares had the same number of demonstrations of the first and the second party. It is guaranteed that the number of dead end squares of the city is even.

    To prevent possible conflicts between the zublicanes and the mumocrates it was decided to minimize the number of roads connecting the squares with the distinct parties. You, as a developer of the department of distributing squares, should determine this smallest number.

    Input

    The first line of the input contains a single integer n (2 ≤ n ≤ 5000) — the number of squares in the capital of Berland.

    Next n - 1 lines contain the pairs of integers x, y (1 ≤ x, y ≤ n, x ≠ y) — the numbers of the squares connected by the road. All squares are numbered with integers from 1 to n. It is guaranteed that the number of dead end squares of the city is even.

    Output

    Print a single number — the minimum number of roads connecting the squares with demonstrations of different parties.

    Sample test(s)
    input
    8
    1 4
    2 4
    3 4
    6 5
    7 5
    8 5
    4 5
    
    output
    1
    
    input
    5
    1 2
    1 3
    1 4
    1 5
    
    output
    2

    思路:设f[i][cnt][col]表示i的子树中,黑节点为cnt,i的颜色为col时,最小的端点颜色不同的边的数量。

    转移就是 f[x][cnt][col]=min(f[son[x]][i][col_son]+f[x][cnt-i][col]+(col!=col_son));

    看起来这是O(n^3)的,实际上这是O(n^2)的

    对于一个点x,对x操作的复杂度就是


    观察一下就会发现,每对叶子节点(x,y)只会在lca(x,y)处对复杂度有一次贡献

    于是复杂度就是O(n^2)的了。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    const int maxn=5010,maxm=10010;
    using namespace std;
    int n,m,pre[maxm],now[maxn],son[maxm],val[maxm],f[maxn][maxn][2],g[maxn][2],siz[maxn],tot,in[maxn];bool isl[maxn];
    void add(int a,int b){pre[++tot]=now[a],now[a]=tot,son[tot]=b,in[b]++;}//0 white 1 black
    
    void dfs(int x,int fa){
    	
    	if (isl[x]){siz[x]=1,f[x][0][0]=f[x][1][1]=0;return;}
    	f[x][0][0]=f[x][0][1]=0;
    	for (int y=now[x];y;y=pre[y])if (son[y]!=fa){
    		dfs(son[y],x);
    		//printf("fuckpp%d %d
    ",son[y],siz[x]);
    		memset(g,63,sizeof(g));
    		for (int i=siz[x];i>=0;i--)
    			for (int j=siz[son[y]];j>=0;j--){
    				//if (son[y]==8&&x==5) printf("%d %d
    ",i,j);
    				g[i+j][0]=min(g[i+j][0],f[x][i][0]+f[son[y]][j][0]);
    				g[i+j][1]=min(g[i+j][1],f[x][i][1]+f[son[y]][j][1]);
    				g[i+j][0]=min(g[i+j][0],f[x][i][0]+f[son[y]][j][1]+1);
    				g[i+j][1]=min(g[i+j][1],f[x][i][1]+f[son[y]][j][0]+1);
    			}
    		memcpy(f[x],g,sizeof(f[x]));
    		siz[x]+=siz[son[y]];
    	}
    }
    
    int main(){
    	scanf("%d",&n);
    	for (int i=1,a,b;i<n;i++) scanf("%d%d",&a,&b),add(a,b),add(b,a);
    	if (n==2) return puts("1"),0;
    	for (int i=1;i<=n;i++) isl[i]=(in[i]==1);
    	memset(f,63,sizeof(f));
    	for (int i=1;i<=n;i++) if (in[i]!=1){
    		dfs(i,0),printf("%d
    ",min(f[i][siz[i]>>1][0],f[i][siz[i]>>1][1]));
    		break;
    	}
    //	for (int i=1;i<=n;i++) printf("%d %d
    ",i,siz[i]);
    	return 0;
    }


  • 相关阅读:
    sphinx-2.1.9的安装使用
    lnmp源码安装
    lvs的负载均衡测试
    Packet Tracer路由器简单配置
    虚拟机克隆系统
    虚拟机永久磁盘和非永久磁盘的应用
    虚拟机和物理机之间的传输的六种方法
    数据库系列(二)之 开发规则与优化
    数据库系列(一)之 底层
    IT之快速提高效率的方法与思考
  • 原文地址:https://www.cnblogs.com/thythy/p/5493509.html
Copyright © 2020-2023  润新知