POJ 3107 Godfather (树重心)
题目链接:http://poj.org/problem?id=3107
题意:
数重心,并按从小到大输出。
思路:
dfs
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <vector>
- using namespace std;
- const int N = 5e4 + ;
- struct Edge {
- int next, to;
- }edge[N << ];
- vector <int> G[N];
- int dp[N], n, head[N], cnt;
- inline void add(int u, int v) {
- edge[cnt].next = head[u];
- edge[cnt].to = v;
- head[u] = cnt++;
- }
- int dfs(int u, int p) {
- dp[u] = ;
- int sum = ;
- for(int i = head[u]; ~i; i = edge[i].next) {
- int v = edge[i].to;
- if(v == p)
- continue;
- int temp = dfs(v, u);
- dp[u] = max(dp[u], temp);
- sum += temp;
- }
- dp[u] = max(dp[u], n - sum - );
- return sum + ;
- }
- int main()
- {
- while(~scanf("%d", &n)) {
- for(int i = ; i <= n; ++i) {
- head[i] = -;
- }
- cnt = ;
- for(int i = ; i < n; ++i) {
- int u, v;
- scanf("%d %d", &u, &v);
- add(u, v);
- add(v, u);
- }
- dfs(, -);
- int Max = n;
- vector <int> ans;
- for(int i = ; i <= n; ++i) {
- Max = min(Max, dp[i]);
- }
- for(int i = ; i <= n; ++i) {
- if(Max == dp[i]) {
- ans.push_back(i);
- }
- }
- for(int i = ; i < ans.size(); ++i) {
- if(i == ans.size() - ) {
- printf("%d\n", ans[i]);
- } else {
- printf("%d ", ans[i]);
- }
- }
- }
- return ;
- }
POJ 3107 Godfather (树重心)的更多相关文章
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
- poj 3107 Godfather(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7885 Accepted: 2786 Descrip ...
- POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...
- # [Poj 3107] Godfather 链式前向星+树的重心
[Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...
- poj 3107 Godfather 求树的重心【树形dp】
poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...
- POJ 1655 Balancing Act && POJ 3107 Godfather
题目大意: 根据题目的图很好理解意思,就是记录每一个点的balance,例如 i 的balance就是把 i 从这棵树中除去后得到的森林中含有结点数最多 的子树中的节点个数,然后找到所有节点中对应的b ...
- POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)
参考网址:http://blog.csdn.net/acdreamers/article/details/16905653 树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的 ...
- POJ 3107 Godfather(树的重心)
嘟嘟嘟 题说的很明白,就是求树的重心. 我们首先dfs一遍维护每一个点的子树大小,然后再dfs一遍,对于一个点u,选择子树中size[v]最小的那个和n - size[u]比较,取最大作为删除u后的答 ...
- POJ 3107 Godfather (树的重心)
题意:求树的重心,若有多个,全部打印出来. 思路: 树的重心:在删除点v后,森林中的每棵树的节点数尽量均匀,若最大的那棵树的节点数最小,称v为树的重心. 这道题只是求树的所有重心,当且经当这棵树有对称 ...
随机推荐
- python解析库之 XPath
1. XPath (XML Path Language) XML路径语言 2. XPath 常用规则: nodename 选取此节点的所有子节点 / 从当前 ...
- navigationcontroller和navigationbar和navigationitem之间的区别以及不用nib实现点击屏幕关闭虚拟键盘20130911
1.UIViewController UIView的关系. UIView是视图,UIViewController是视图控制器,两者之间是从属关系,当创建一个UIViewController的时候,一般 ...
- HDU:5040-Instrusive
Instrusive Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Proble ...
- ACM训练联盟周赛 A. Teemo's bad day
65536K Today is a bad day. Teemo is scolded badly by his teacher because he didn't do his homework ...
- SQL_3_表达式、条件语句与运算
加号的两种用法: 1.在SELECT子句中使用+号以执行对数据的运算并将结果显示出来. SELECT ITEM WHOLESALE WHOLESALE+0.15 FROM PRICE; 还可以重命名新 ...
- 《Python全栈开发指南》第3版 Alex著(LFXC2018)
第一章 Python基础——Python介绍&循环语句 1.1 编程语言介绍 1.2 Python介绍 1.3 Python安装 1.4 第一个Python程序 1.5 变量 1.6 程序交互 ...
- 爬虫Scrapy框架-1
Scrapy 第一步:安装 linux: pip3 install scrapy windows: 1:pip3 install wheel ,安装wheel模块 2.下载twisted:http:/ ...
- [python 测试框架学习篇] 分享 uiautomator测试框架
uiautomator测试框架 :https://testerhome.com/topics/4194
- TOJ4537: n阶行列式
4537: n阶行列式 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 28 ...
- VMware RHEL6.3 开启网络连接
确认/etc/sysconfig/network是否存在,如果不存在,service network 命令使用不了.新建: NETWORKING=yes HOSTNAME=RHEL6. GATEWAY ...