tree_cuttting(树形dp求解树的重心)

时间:2021-11-24 13:24:03

Tree Cutting

After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible cost, he sued Bessie to mitigate his losses.

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

* Line 1: A single integer, N. The barns are numbered 1..N.

* Lines 2..N: Each line contains two integers X and Y and represents a connection between barns X and Y.

Output

* Lines 1..?: Each line contains a single integer, the number (from 1..N) of a barn whose removal splits the network into pieces each having at most half the original number of barns. Output the barns in increasing numerical order. If there are no suitable barns, the output should be a single line containing the word "NONE".

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

INPUT DETAILS:

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求解树的重心)的更多相关文章

  1. 树形DP求树的重心 --SGU 134

    令一个点的属性值为:去除这个点以及与这个点相连的所有边后得到的连通分量的节点数的最大值. 则树的重心定义为:一个点,这个点的属性值在所有点中是最小的. SGU 134 即要找出所有的重心,并且找出重心 ...

  2. POJ 1655 Balancing Act (树形DP求树的重心)

    题意: 求一棵树中以某个点为重心最小的子树集, 就是去掉这个点, 图中节点最多的联通块节点最少. 分析: 想知道这个点是不是最优的点, 只要比较它子树的数量和除去这部分其他的数量(它的父节点那部分树) ...

  3. 树形dp求树的重心

    Balancing Act http://poj.org/problem?id=1655 #include<cstdio> #include<cstring> #include ...

  4. HDU 4514 - 湫湫系列故事——设计风景线 - &lbrack;并查集判无向图环&rsqb;&lbrack;树形DP求树的直径&rsqb;

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  5. 浅谈关于树形dp求树的直径问题

    在一个有n个节点,n-1条无向边的无向图中,求图中最远两个节点的距离,那么将这个图看做一棵无根树,要求的即是树的直径. 求树的直径主要有两种方法:树形dp和两次bfs/dfs,因为我太菜了不会写后者这 ...

  6. hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解

    题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...

  7. 2017 Wuhan University Programming Contest &lpar;Online Round&rpar; B Color 树形dp求染色方法数

    /** 题目:Color 链接:https://oj.ejq.me/problem/23 题意:给定一颗树,将树上的点最多染成m种颜色,有些节点不可以染成某些颜色.相邻节点颜色不同.求染色方法数. 思 ...

  8. HDU - 3899 JLUCPC(树形dp求距离和)

    JLUCPC Dr. Skywind and Dr. Walkoncloud are planning to hold the annual JLU Collegiate Programming Co ...

  9. 树形dp - 求树的直径

    随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探确定了n个位置可 ...

随机推荐

  1. CozyRSS开发记录20-CanResizeWithGrip

    CozyRSS开发记录20-CanResizeWithGrip 1.窗口样式 首先,WindowStyle有四种: 然后,对于窗口缩放的ResizeMode,也有四种,CanResize和CanRes ...

  2. ISIN编码

    国际证券识别编码(ISIN编码)是由国际标准化组织(ISO)制定的证券编码标准,并在<证券及相关金融工具-国际证券识别编码体系>(ISO6166)中正式发布.ISO6166主要规定了ISI ...

  3. wince6下载地址

    http://geekswithblogs.net/WindowsEmbeddedCookbook/archive/2010/08/31/installing-windows-ce-6.0-tools ...

  4. 关于delete和delete&lbrack;&rsqb;

    [精彩] 求问delete和delete[] 的区别??http://www.chinaunix.net/jh/23/311058.html C++告诉我们在回收用 new 分配的单个对象的内存空间的 ...

  5. android性能调优之traceview的使用

    1.在开始使用TraceView你要注意: 你的设备和模拟器必须设置SD card 和 你的程序拥有对SD card 具有读写操作的权限( <uses-permission android:na ...

  6. java环境配置,试用和基本数据结构

    一.java环境配置 1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变量值:jdk文件所在的路经变量名:CLASS ...

  7. JPA关系映射之one-to-many和many-to-one

    one-to-many(一对多)和many-to-one(多对一)双向关联 假设部门与员工是一对多关系,反过来员工与部门就是多对一关系. Dept.java类 public class Dept im ...

  8. 通用的contain函数

    用来检测节点所属关系:document.documentElement.contains(document.body) function contains(refNode, otherNode) {i ...

  9. (转)Java并发包基石-AQS详解

    背景:之前在研究多线程的时候,模模糊糊知道AQS这个东西,但是对于其内部是如何实现,以及具体应用不是很理解,还自认为多线程已经学习的很到位了,贻笑大方. Java并发包基石-AQS详解Java并发包( ...

  10. stark组件开发之组合搜索实现思路

    - 关键字搜索. 可以做到的效果是, 输入20. 后太通过 Q()  函数. 来实现.  搜索是一个大的问题点. -  要想实现组合搜索, 首先要 明确的一点是. 在我当前的页面上, 正在进行展示的是 ...