博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces581F. Zublicanes and Mumocrates
阅读量:5154 次
发布时间:2019-06-13

本文共 3548 字,大约阅读时间需要 11 分钟。

传送门:

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
81 42 43 46 57 58 54 5
output
1
input
51 21 31 41 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
#include
#include
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 blackvoid 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\n",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\n",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
>1][0],f[i][siz[i]>>1][1])); break; }// for (int i=1;i<=n;i++) printf("%d %d\n",i,siz[i]); return 0;}

转载于:https://www.cnblogs.com/thythy/p/5493509.html

你可能感兴趣的文章
[原创]python MySQLdb在windows环境下的安装、出错问题以及解决办法
查看>>
使用windbg查看DependencyObject的属性
查看>>
Notepad++ 更换主题
查看>>
orm
查看>>
FlashCache初探(一)
查看>>
第三次冲刺(一)
查看>>
WEB网站类型系统中使用的OFFICE控件
查看>>
leetcode-6-Z字形变换
查看>>
Silverlight访问Wcf Ria Library的问题总结
查看>>
centos 7 安装和配置vncserver
查看>>
可适配平板、手机的Web开发方式
查看>>
Android系统层次结构及分析
查看>>
Hadoop生态系统
查看>>
maven常见问题归纳
查看>>
HDU 1242 Rescue
查看>>
学习日记之单例模式和Effective C++
查看>>
异步I/O操作
查看>>
财务模块多组织,GL, SLA, SOB, COA, BSV, CCID, LE 概念的简单介绍
查看>>
FORM中读取图片
查看>>
扩展欧几里得定理
查看>>