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 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]

    题目链接: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 (Online Round) 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. PHP教程-反序列化的方法

    序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性.兄弟连PHP培训() 1. ...

  2. Tarjan求强连通分量、求桥和割点模板

    Tarjan 求强连通分量模板.参考博客 #include<stdio.h> #include<stack> #include<algorithm> using n ...

  3. POJ 3692 幼儿园做游戏 最大团 模板题

    Kindergarten Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6191   Accepted: 3052 Desc ...

  4. HUD 1166:敌兵布阵(线段树 or 树状数组)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

  5. Unity3D_(API)射线检测Raycast()

    Unity射线检测官方文档: 传送门 一.检测前方是否有游戏物体(射线无限长度) 二.检测前方是否有游戏物体(射线长度为1m) 三.检测前方游戏物体碰撞信息(射线无限长度): 四.指定检测碰撞Tag层 ...

  6. 前端面试题总结——HTML(持续更新中)

    前端面试题总结--HTML(持续更新中) 1.什么是HTML? HTML:HyperText Markup Language超文本标记语言 2.XHTML和HTML有什么区别 HTML是一种基本的WE ...

  7. SpringBoot启动加载yml配置文件出现编码格式错误

    Caused by: org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input l ...

  8. 万变的Web,不变的CRUD

    用JSP+Servlet写程序,到Struts,Spring,hibernate写程序,到现在Spring Cloud分布式写程序,到底有多大区别,是不是还在写CRUD? 看着JD上各种要求,简直是S ...

  9. python MySQLdb 如何设置读超时read_timeout

    在python中,经常用到 MySQLdb操作MySQL数据库. 在实现上,MySQLdb并不是纯python的,而是封装了MySQL C API库_mysql. 对于MySQLdb是否支持read_ ...

  10. leetcode 11盛水最多的容器

    class Solution { public: int maxArea(vector<int>& height) { //双指针法:从最宽的容器开始计算,当更窄的容器盛水量要大于 ...