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 ;
}

最新文章

  1. BackgroundWorker学习
  2. web桌面程序之图标拖动排序的分析
  3. Effective Java 17 Design and document for inheritance or else prohibit it
  4. JMeter学习(二十五)HTTP属性管理器HTTP Cookie Manager、HTTP Request Defaults
  5. JAVA关系运算符
  6. 数据库不能用delete---index空间不足
  7. 深入分析Java的序列化与反序列化
  8. DedeCMS安装及目录结构
  9. 转:Grunt:任务自动管理工具
  10. FindWindowEx使用方法
  11. 获取控件id
  12. Java学习之旅基础知识篇:数据类型及流程控制
  13. Linq 延迟加载
  14. 面试官问我,Redis分布式锁如何续期?懵了。
  15. git在vs2017中的使用
  16. python,关于用户登录与注册问题
  17. windows任务管理器怎么知道多个IIS网站进程分别对应哪个网站
  18. Redis危险命令重命名、禁用
  19. Apache的项目列表
  20. js之10天内免登陆

热门文章

  1. Oracle 常用语句汇总
  2. StoryBoard页面联线跳转已经页面之间传参数
  3. SEO_Alexa排名
  4. SWFLoader交互
  5. 你需要知道的九大排序算法【Python实现】之归并排序
  6. Tips:javascript 图片放大和取得尺寸
  7. 收集的URL
  8. Java 之 StringTokenizer
  9. jquery获取checkbox被选中的值
  10. Ubuntu下安装android studio的时候,无法进入图形界面--/usr/lib/jdk1.8.0_60/jre/lib/i386/libawt_xawt.so: libXtst.so.6: 无法打开共享对象文件: 没有那个文件或目录