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

题目大意:

一棵树,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的更多相关文章

  1. 树形dp - Codeforces Round #322 (Div. 2) F Zublicanes and Mumocrates

    Zublicanes and Mumocrates Problem's Link Mean: 给定一个无向图,需要把这个图分成两部分,使得两部分中边数为1的结点数量相等,最少需要去掉多少条边. ana ...

  2. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  3. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  4. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  5. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  6. Codeforces Round #322 (Div. 2) E F

    E. Kojiro and Furrari 题意说的是 在一条直线上 有n个加油站, 每加一单位体积的汽油 可以走1km 然后每个加油站只有一种类型的汽油,汽油的种类有3种 求从起点出发到达终点要求使 ...

  7. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)

    题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...

  8. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  9. 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 ...

随机推荐

  1. 【转】YUV值对应的颜色

    版权声明:本文为博主原创文章,未经博主允许不得转载.欢迎大家积极评论,博主会一一答复! 最近有人在网上问我,YUV的值对应的颜色是如何的 下面给出YUV值对应的颜色关系 256张图512x512,每张 ...

  2. JavaScript新手学习笔记4——我记不住的几个坑:短路逻辑、按值传递、声明提前

    1.短路逻辑 逻辑运算中,如果前一个条件已经可以得出最终结论,则后续所有条件不再执行!这里的逻辑运算指的是逻辑与和逻辑或. 我们要理解逻辑与是两个条件都为真的时候,才为真,如果第一个就是假的,那么后面 ...

  3. String 和 InputStream 互转方式

    /** * 利用BufferedReader实现Inputstream转换成String <功能详细描述> * * @param in * @return String */ public ...

  4. c++大作业--学籍管理系统--

    1.题目描写叙述 学籍管理系统: 依据信息管理系统的业务流程.要求以及所要实现的目标,完毕下面功能: (1)建立学生档案的管理和维护.实现计算机自己主动化管理体制. (2)建立学生成绩管理机制,在计算 ...

  5. android4.4 settings 中控制卡1 卡2都振动

    在package/app/Settings/src/com/android/settings/SoundSettings.java

  6. [React Testing] className with Shallow Rendering

    The React Shallow Renderer test utility lets us inspect the output of a component one level deep. In ...

  7. Android(java)学习笔记260:JNI之native方法头文件的生成

    1. JDK1.6 ,进入到工程的bin目录下classes目录下: 使用命令: javah  packageName.ClassName 会在当前目录下生成头文件,从头文件找到jni协议方法 下面举 ...

  8. asp.net验证控件注意事项

    1.如果触发某个控件事件是只对指定验证控件进行验证,可以将验证控件和被触发控件放到到一个ValidationGroup中.比如点提交按钮的时候,验证文本框,可以将提交按钮和验证控件放到一个Valida ...

  9. access 2007 vba 开发中学到的知识(一)

    使用ado连接本身的数据库,需要先创建一个 adodb.connection的连接对象 Set cn = CreateObject("ADODB.Connection") 数据库的 ...

  10. java下管道流 PipedOutputStream 与PipedInputStream

    package cn.stat.p2.demo; import java.io.IOException; import java.io.PipedInputStream; import java.io ...