poj 2378 Tree Cutting 树形dp
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).
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 1e5 + ;
const int INF = 0x7fffffff;
int n, dp[maxn][], head[maxn], tot, ans[maxn];
struct node {
int v, next;
} edge[maxn];
void init() {
tot = ;
memset(head, -, sizeof(head));
}
void add(int u, int v) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
edge[tot].v = u;
edge[tot].next = head[v];
head[v] = tot++;
}
int k = ;
int solve(int x, int fa) {
int sum = , maxs = -;
for (int i = head[x] ; i != - ; i = edge[i].next) {
int v = edge[i].v;
if (v == fa) continue;
int cost = solve(v, x);
if (cost > maxs) maxs = cost;
sum += cost;
}
if (n - sum > maxs) maxs = n - sum;
if (maxs <= n / ) ans[k++] = x;
return sum;
}
int main() {
while(scanf("%d", &n) != EOF) {
init();
for (int i = ; i < n - ; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
}
solve(, -);
if (k == ) printf("NONE\n");
else {
sort(ans, ans + k);
for (int i = ; i < k ; i++)
printf("%d\n", ans[i]);
}
}
return ;
}
poj 2378 Tree Cutting 树形dp的更多相关文章
- POJ 2378.Tree Cutting 树形dp 树的重心
Tree Cutting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4834 Accepted: 2958 Desc ...
- POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)
POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...
- hdu 5909 Tree Cutting [树形DP fwt]
hdu 5909 Tree Cutting 题意:一颗无根树,每个点有权值,连通子树的权值为异或和,求异或和为[0,m)的方案数 \(f[i][j]\)表示子树i中经过i的连通子树异或和为j的方案数 ...
- [poj3107/poj2378]Godfather/Tree Cutting树形dp
题意:求树的重心(删除该点后子树最大的最小) 解题关键:想树的结构,删去某个点后只剩下它的子树和原树-此树所形成的数,然后第一次dp求每个子树的节点个数,第二次dp求解答案即可. 此题一开始一直T,后 ...
- POJ 2378 Tree Cutting (DFS)
题目链接:http://poj.org/problem?id=2378 一棵树,去掉一个点剩下的每棵子树节点数不超过n/2.问有哪些这样的点,并按照顺序输出. dfs回溯即可. //#pragma c ...
- HDU - 5909 Tree Cutting (树形dp+FWT优化)
题意:树上每个节点有权值,定义一棵树的权值为所有节点权值异或的值.求一棵树中,连通子树值为[0,m)的个数. 分析: 设\(dp[i][j]\)为根为i,值为j的子树的个数. 则\(dp[i][j\o ...
- HDU.5909.Tree Cutting(树形DP FWT/点分治)
题目链接 \(Description\) 给定一棵树,每个点有权值,在\([0,m-1]\)之间.求异或和为\(0,1,...,m-1\)的非空连通块各有多少个. \(n\leq 1000,m\leq ...
- POJ 2378 Tree Cutting (树的重心,微变形)
题意: 给定一棵树,n个节点,若删除点v使得剩下的连通快最大都不超过n/2,则称这样的点满足要求.求所有这样的点,若没有这样的点,输出NONE. 思路: 只需要拿“求树的重心”的代码改一行就OK了.因 ...
- POJ 2378 Tree Cutting 子树统计
题目大意:给出一棵树.将树中的一个节点去掉之后,这棵树会分裂成一些联通块.求去掉哪些点之后.全部联通块的大小不超过全部节点的一半.并按顺序输出. 思路:基础的子树统计问题,仅仅要深搜一遍就能够出解.这 ...
随机推荐
- html 弹框 优化 alert
<!DOCTYPE html> <html> <head> <title>cs</title> </head> <styl ...
- 用filter()筛选出素数
'use strict'; function get_primes(arr) { return arr.filter(function isPrime(number) { if (typeof num ...
- itchat和matplotlib的结合使用爬取微信信息
前几天无意中看到了一片文章,<一件有趣的事:我用 Python 爬了爬自己的微信朋友>,这篇文章写的是使用python中的itchat爬取微信中朋友的信息,其中信息包括,昵称.性别.地理位 ...
- P3388 【模板】割点
题目背景 割点 题目描述 给出一个n个点,m条边的无向图,求图的割点. 输入输出格式 输入格式: 第一行输入n,m 下面m行每行输入x,y表示x到y有一条边 输出格式: 第一行输出割点个数 第二行按照 ...
- 数据库 MySQL part1
数据库介绍 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据库中的数据按一定的数学模型组织.描述和存储,具有较小的冗余,较高的数据独立性和易扩展性,并可为各种 ...
- 线程池ThreadPoolExecutor使用
一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...
- springmvc基础篇—掌握三种控制器
上一篇文章中我们讲过了处理器的映射,接下来我们来一起学习下springmvc的控制器吧. 首先咱们先创建一个咱们用来测试的实体(model)类: package cn.cfs.springmvc.do ...
- CWindowWnd类源码分析
CWindowWnd代码在UIBase.h和UIBase.cpp文件里.主要实现的是一个基本窗口的创建与消息处理. 相关代码: 头文件: class UILIB_API CWindowWnd { pu ...
- fiddler抓包-简单易操作(二)
Fiddler抓包简介 原理:fiddler是通过改写HTTP代理,客户端和服务器进行交互时,数据会从他那里通过,来监控和截取数据.我是这样理解的,如果不对,欢迎指正.如下图: 如果想要抓到数据包,首 ...
- Wannafly挑战赛21:C - 大水题
链接:Wannafly挑战赛21:C - 大水题 题意: 现在给你N个正整数ai,每个数给出一“好数程度” gi(数值相同但位置不同的数之间可能有不同的好数程度).对于在 i 位置的数,如果有一在j位 ...