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.
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.
Print a single number — the minimum number of roads connecting the squares with demonstrations of different parties.
8
1 4
2 4
3 4
6 5
7 5
8 5
4 5
1
5
1 2
1 3
1 4
1 5
2
题目大意:
一棵树,5000个结点,两个政党,你必须满证这两个政党占领的度数为1的点的数量相同,其他点随意,然后求的是最少的X。这个X指的是连接两个不同政党的边的数量.
注意,所以点必须被某个政党占领,同时题目保证度数为1的政党数目相同.
解题报告:
不妨有dp(i,j,k) , k ∈ {0,1} 表示将以 i 为根的子树全部染色,同时i染K色,同时下面有J个染0色的度一点.
转移比较简单,这里不再累述.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>
#include <conio.h>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1) using namespace std;
const int maxn = 5e3 + ;
struct Edge{int v , nxt;};
int sz[maxn],head[maxn],n,tot=,root,dp[maxn][maxn][],errorcode;
Edge e[maxn*]; void add(int u,int v)
{
e[tot].v = v , e[tot].nxt= head[u],head[u] = tot++ , sz[u]++;
} void initiation()
{
memset(sz,,sizeof(sz));memset(dp,0x3f,sizeof(dp));memset(head,-,sizeof(head));
errorcode = dp[][][];
scanf("%d",&n);
for(int i = ; i < n ; ++ i)
{
int u , v;
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
} inline void updata(int & x ,int v)
{
x = min(x , v );
} int dfs(int u ,int fa)
{
int res = ;
if(sz[u] == ) res = , dp[u][][] = dp[u][][] = ;
else dp[u][][] = dp[u][][] = ;
for(int i = head[u] ; ~i ; i = e[i].nxt)
{
int v = e[i].v;
if(v == fa) continue;
int t = dfs(v,u);
for(int j = res ; j >= ; -- j)
{
for(int k = t ; k >= ; -- k)
{
updata( dp[u][j+k][] , dp[u][j][] + dp[v][k][] + );
updata( dp[u][j+k][] , dp[u][j][] + dp[v][k][]);
updata( dp[u][j+k][] , dp[u][j][] + dp[v][k][]);
updata( dp[u][j+k][] , dp[u][j][] + dp[v][k][] + );
}
int s1 = min(dp[v][][] + , dp[v][][]);
dp[u][j][] = s1 > (<<) ? errorcode : s1 + dp[u][j][];
s1 = min(dp[v][][],dp[v][][]+);
dp[u][j][] = s1 > (<<) ? errorcode : s1 + dp[u][j][];
}
res += t;
}
return res;
} void solve()
{
int res = ;
for(int i = ; i <= n ; ++ i) if(sz[i] != ) {root = i ; break;}
for(int i = ; i <= n ; ++ i) if(sz[i] == ) res ++ ;
dfs(root,);
printf("%d\n",min(dp[root][res/][],dp[root][res/][]));
} int main(int argc,char *argv[])
{
initiation();
if(n == ) printf("1\n");
else solve();
return ;
}
Codeforces Round #322 (Div. 2) —— F. Zublicanes and Mumocrates的更多相关文章
-
树形dp - Codeforces Round #322 (Div. 2) F Zublicanes and Mumocrates
Zublicanes and Mumocrates Problem's Link Mean: 给定一个无向图,需要把这个图分成两部分,使得两部分中边数为1的结点数量相等,最少需要去掉多少条边. ana ...
-
Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
-
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
-
Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
-
Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
-
Codeforces Round #322 (Div. 2) E F
E. Kojiro and Furrari 题意说的是 在一条直线上 有n个加油站, 每加一单位体积的汽油 可以走1km 然后每个加油站只有一种类型的汽油,汽油的种类有3种 求从起点出发到达终点要求使 ...
-
Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
-
Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
-
Codeforces Round #325 (Div. 2) F. Lizard Era: Beginning meet in the mid
F. Lizard Era: Beginning Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
随机推荐
-
java基础-servlet-2:生命周期
1.加载(class load) 2.实例化 3.init() 4.doGet() 5.destroy 只有一个对象存在于服务端提供服务.再次访问,不会再起新对象.
-
phpcms前台退出登录的时候提示信息&#39;退出成功0&#39;
问题背景: phpcms前台退出登录的时候,提示了一个退出成功0 让我很困惑为啥有个0呢? 问题分析: 进入 ./phpcms/modules/member/index.php 找到logout方法, ...
-
USACO 08-Nov( 最小生成树)
美国人出题拐弯抹角,倒是挺尊重动物的 10206301 2 52 3 52 4 123 4 172 5 153 5 64 5 12 Hint从牧场4起床, 然后按照 4, 5, 4, 2, 3, 2, ...
-
【转】JavaScript中,{}+{}等于多少?
原文链接:http://www.2ality.com/2012/01/object-plus-object.html 译文链接:http://www.cnblogs.com/ziyunfei/arch ...
-
Angular动态注册组件(controller,service...)
使用angular的场景一般是应用类网站 这也意味着会有很多的controller,service,directive等等 正常情况下我们要把这些内容一次性下载并注册,由于文件较多,对首次加载的效率影 ...
-
HDU2196computer(树上最远距离 + DP)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
-
Sybase分页存储过程实现
项目中需要用到Sybase数据库的分页功能,想尽各种办法都没有成功,最后用如下的存储过程成功实现功能,记录备忘. ),@start int, @pageSize int as begin declar ...
-
RabbitMQ延迟消息学习
准备做一个禁言自动解除的功能,立马想到了订单的超时自动解除,刚好最近在看RabbitMQ的实现,于是想用它实现,查询了相关文档发现确实可以实现,动手编写了这篇短文. 准备工作 1.Erlang安装请参 ...
-
Elasticsearch基础分布式架构
写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...
-
lnmp集成环境Access Denied的问题
在你的php.ini配置文件中,设置cgi.fix_pathinfo=1