tree_cuttting(树形dp求解树的重心)
Tree Cutting
Bessie, feeling vindictive, decided to sabotage Farmer John's network by cutting power to one of the barns (thereby disrupting all the connections involving that barn). When Bessie does this, it breaks the network into smaller pieces, each of which retains full connectivity within itself. In order to be as disruptive as possible, Bessie wants to make sure that each of these pieces connects together no more than half the barns on FJ.
Please help Bessie determine all of the barns that would be suitable to disconnect.
Input
* Lines 2..N: Each line contains two integers X and Y and represents a connection between barns X and Y.
Output
Sample Input
10
1 2
2 3
3 4
4 5
6 7
7 8
8 9
9 10
3 8
Sample Output
3
8
Hint
The set of connections in the input describes a "tree": it connects all the barns together and contains no cycles.
OUTPUT DETAILS:
If barn 3 or barn 8 is removed, then the remaining network will have one piece consisting of 5 barns and two pieces containing 2 barns. If any other barn is removed then at least one of the remaining pieces has size at least 6 (which is more than half of the original number of barns, 5).
/*************************************************************************
> File Name: tree_cutting.cpp
> Author: CruelKing
> Mail: 2016586625@qq.com
> Created Time: 2019年09月22日 星期日 11时31分23秒
************************************************************************/ #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ; int n; int head[maxn], tot; int sonsize[maxn];//统计以i为根节点的子树的大小 int dp[maxn];//统计i结点子树中最大的那颗子树的大小 int ans; struct Edge {
int to, next;
} edge[maxn << ]; void init() {
memset(head, -, sizeof head);
tot = ;
} void addedge(int u, int v) {
edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot ++;
edge[tot].to = u; edge[tot].next = head[v]; head[v] = tot ++;
} void dfs(int u, int pre) {
sonsize[u] = ;
dp[u] = ;
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(v == pre) continue;
dfs(v, u);
sonsize[u] += sonsize[v];
dp[u] = max(dp[u], sonsize[v]);
}
dp[u] = max(dp[u], n - sonsize[u]);
ans = min(ans, dp[u]);
} int main() {
int u, v;
init();
ans = maxn;
scanf("%d", &n);
for(int i = ; i < n - ; i ++) {
scanf("%d %d", &u, &v);
addedge(u, v);
}
dfs(, );
int cnt = ;
for(int i = ; i <= n; i ++) {
if( * dp[i] <= n) {
printf("%d\n", i);
cnt ++;
}
}
if(!cnt) printf("NONE\n");
return ;
}
tree_cuttting(树形dp求解树的重心)的更多相关文章
- 树形DP求树的重心 --SGU 134
令一个点的属性值为:去除这个点以及与这个点相连的所有边后得到的连通分量的节点数的最大值. 则树的重心定义为:一个点,这个点的属性值在所有点中是最小的. SGU 134 即要找出所有的重心,并且找出重心 ...
- POJ 1655 Balancing Act (树形DP求树的重心)
题意: 求一棵树中以某个点为重心最小的子树集, 就是去掉这个点, 图中节点最多的联通块节点最少. 分析: 想知道这个点是不是最优的点, 只要比较它子树的数量和除去这部分其他的数量(它的父节点那部分树) ...
- 树形dp求树的重心
Balancing Act http://poj.org/problem?id=1655 #include<cstdio> #include<cstring> #include ...
- HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- 浅谈关于树形dp求树的直径问题
在一个有n个节点,n-1条无向边的无向图中,求图中最远两个节点的距离,那么将这个图看做一棵无根树,要求的即是树的直径. 求树的直径主要有两种方法:树形dp和两次bfs/dfs,因为我太菜了不会写后者这 ...
- hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解
题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...
- 2017 Wuhan University Programming Contest (Online Round) B Color 树形dp求染色方法数
/** 题目:Color 链接:https://oj.ejq.me/problem/23 题意:给定一颗树,将树上的点最多染成m种颜色,有些节点不可以染成某些颜色.相邻节点颜色不同.求染色方法数. 思 ...
- HDU - 3899 JLUCPC(树形dp求距离和)
JLUCPC Dr. Skywind and Dr. Walkoncloud are planning to hold the annual JLU Collegiate Programming Co ...
- 树形dp - 求树的直径
随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探确定了n个位置可 ...
随机推荐
- 1,Java消息服务-JMS
一,消息服务 消息服务指的是两个应用程序之间进行异步通信的API,它为标准消息协议和消息服务提供了一组通用接口,包括创建.发送.读取消息等,用于支持应用程序开发.在Java中,当两个应用程序使用JMS ...
- sklearn可实现的函数或者功能可分为如下几个方面
1.分类算法2.回归算法3.聚类算法4.降维算法5.模型优化6.文本预处理 其中分类算法和回归算法又叫监督学习,聚类算法和降维算法又叫非监督学习 分类算法 1 2 3 4 5 6 7 8 9 10 1 ...
- python3.6+Xadmin2.0系列(一) xadmin下载及安装
环境配置:win7+python3.6+Django2.1+xadmin2+PyCharm 一.Xadmin下载及安装: 1.下载: 下载地址:https://github.com/sshwsfc/x ...
- Scrapy学习(二)、安装及项目结构
一.安装 1.安装pywin32,下载地址:https://sourceforge.net/projects/pywin32/files/pywin32/ 我选择的是Build 221,点进去,根据自 ...
- 一、mybatis的插件介绍
摘自:https://www.cnblogs.com/qm-article/p/11785350.html mybatis的插件机制 一.mybatis的插件介绍 关于mybatis的插件,我想大 ...
- C++入门经典-例4.6-使用重载函数
1:代码如下: // 4.6.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...
- centos6 centos7 配置开机启动服务
centos 6 :使用chkconfig命令即可. 我们以apache服务为例: #chkconfig --add apache 添加nginx服务 #chkconfig apache on 开机自 ...
- docker容器无法删除——状态Dead
问题:由于容器挂载数据卷,无法直接删除. 解决: 先查出ID 进行杀掉,然后再删除. . 搞定
- Dubbo HA 高可用
一.zookeeper 宕机 现象:zookeeper 注册中心宕机,还可以消费 dubbo 暴露的服务 健壮性 监控中心宕掉不影响使用,只是丢失部分采样数据 数据库宕掉后,注册中心仍能通过缓 ...
- Emacs Python 自动补全之 eglot
eglot 个人水平有限,自己的测试难免有不足甚至错误的地方.欢迎各位emacser 能前来留言交流. 首先eglot 是一个lsp-mode的集成环境.作者说这不仅仅是一个lsp工具.但是我从其说明 ...