思路:删除根结点,其最大子树的节点最少。求一次树的重心即可。

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 5e4 + 5;
vector<int>G[maxn];
int ans, n;

int dfs(int u, int pre) {
	int cnt = 0, maxt = 0;
	for(int i = 0; i < G[u].size(); ++i) {
		int v = G[u][i];
		if(v == pre) continue;
		int d = dfs(v, u);
		cnt += d;
		maxt = max(maxt, d);
	}
	maxt = max(maxt, n - cnt - 1);
	ans = min(ans, maxt);
	return cnt + 1;
}

int main() {
	while(scanf("%d", &n) == 1) {
		for(int i = 1; i <= n; ++i) G[i].clear();
		int u, v;
		for(int i = 0; i < n-1; ++i) {
			scanf("%d%d", &u, &v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		ans = inf;
		dfs(1, -1);
		printf("%d\n", n - ans);
	}
	return 0;
} 

如有不当之处欢迎指出!

ACdream 1015 Double Kings 树的重心的更多相关文章

  1. ACdream 1015 Double Kings

    假设第一个人选的点为P,并且当作根,那么第二个人选的最优情况必然是根p连着的那些点中的一个.然后枚举一下P即可. #pragma comment(linker, "/STACK:102400 ...

  2. poj1655 Balancing Act 找树的重心

    http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  3. POJ 3107 Godfather(树的重心)

    嘟嘟嘟 题说的很明白,就是求树的重心. 我们首先dfs一遍维护每一个点的子树大小,然后再dfs一遍,对于一个点u,选择子树中size[v]最小的那个和n - size[u]比较,取最大作为删除u后的答 ...

  4. codeforces 701E E. Connecting Universities(树的重心)

    题目链接: E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes i ...

  5. POJ 2378 Tree Cutting (树的重心,微变形)

    题意: 给定一棵树,n个节点,若删除点v使得剩下的连通快最大都不超过n/2,则称这样的点满足要求.求所有这样的点,若没有这样的点,输出NONE. 思路: 只需要拿“求树的重心”的代码改一行就OK了.因 ...

  6. POJ 3107 Godfather (树的重心)

    题意:求树的重心,若有多个,全部打印出来. 思路: 树的重心:在删除点v后,森林中的每棵树的节点数尽量均匀,若最大的那棵树的节点数最小,称v为树的重心. 这道题只是求树的所有重心,当且经当这棵树有对称 ...

  7. hdu-4118 Holiday's Accommodation(树形dp+树的重心)

    题目链接: Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others)     Memory Limit: 200000/200000 ...

  8. poj-1655 Balancing Act(树的重心+树形dp)

    题目链接: Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11845   Accepted: 4 ...

  9. 求树的重心 DFS

    树的重心 何谓重心 树的重心:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 树的重心可以通过简单的两次搜索求出,第一遍搜索求出每个结 ...

随机推荐

  1. python_tornado_session用户验证

    什么是session? -- Django中带有session,tornado中自己写 -- 逻辑整理 用户请求过来,验证通过,随机生成一个字符串当作value返回给浏览器, 在服务器中用户信息与随机 ...

  2. JavaScript数组的22种方法

    原文:http://www.cnblogs.com/xiaohuochai/p/5682621.html javascript中数组的22种方法   前面的话 数组总共有22种方法,本文将其分为对象继 ...

  3. java之拦截器Interceptor

    1,拦截器的概念    java里的拦截器是动态拦截Action调用的对象,它提供了一种机制可以使开发者在一个Action执行的前后执行一段代码,也可以在一个Action执行前阻止其执行,同时也提供了 ...

  4. Directx3d绘制包围体并控制光照效果

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. es head插件通过Nginx http basic 限制访问

    原文链接: http://www.sojson.com/blog/213.html

  6. 在ASP.NET MVC中使用Web API和EntityFramework构建应用程序

    最近做了一个项目技术预研:在ASP.NET MVC框架中使用Web API和EntityFramework,构建一个基础的架构,并在此基础上实现基本的CRUD应用. 以下是详细的步骤. 第一步 在数据 ...

  7. CDN设置回源host的意义

    CDN设置回源host的意义 如果CDN后端用户的的源站web服务上没有绑定加速域名,只绑定了其他域名,未限制域名访问(比如通过服务器IP可以访问到默认网站),可以在CDN控制台填写回源host,这样 ...

  8. 基于Appium1.6.X的WebDriverAgent编译、安装

    转自:http://www.cnblogs.com/baconLiu/p/6861431.html tips:WebDriverAgent是Appium1.6.3以后版本新添加的模块,为了让appiu ...

  9. MDT 2013 从入门到精通之概念扫盲

    从今日开始为大家带来微软MDT 2013批量部署操作系统从入门到精通系列教程,旨在为大家以后的工作.学习提供一个便利的参考教程,以便大家更好.更深入的了解微软MDT,从而减轻企业工程师.IT从业人员及 ...

  10. Leetcode刷题C#版之 Median of Two Sorted Arrays

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...