C - 树形dp

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

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).

题目大意:给你一个树,让你删除其中某一个节点,使得剩下的部分节点数不大于n/2,如果满足则输出这个节点,若没有就输出NONE;
思路分析:用链式前向星存图,选择某一个结点作为根节点DFS一遍,将该结点所在的子树的结点数目跑出来,然后在dfs一遍,跑若该节点断开,分开的几段中节点数
最大值DP[i]=max(n-sum[i],max(sum[son]))
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=+;
struct node
{
int to;
int next;
};
int sum[maxn];
node edge[*maxn];
int head[maxn];
int dp[maxn];
bool vis[maxn];//把一个无向 图变成有向图
int tot;
int n;
void add(int x,int y)
{
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot++;
}
void init()
{
memset(head,-,sizeof(head));
memset(vis,false,sizeof(vis));
tot=;
int a,b;
for(int i=;i<n;i++)//构造图
{
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
}
void dfs(int nod)
{
sum[nod]=;
vis[nod]=true;
for(int i=head[nod];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(!vis[v])
{
dfs(v);
sum[nod]+=sum[v];
}
}
}
void dfs2(int nod)
{
dp[nod]=n-sum[nod];
vis[nod]=true;
for(int i=head[nod];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(!vis[v])
{
dfs2(v);
dp[nod]=max(dp[nod],sum[v]);
}
}
}
bool flag=true;
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
dfs();
memset(vis,false,sizeof(vis));//重置标记数组
dfs2();
for(int i=;i<=n;i++)
{
if(dp[i]*<=n)
{
printf("%d\n",i);
flag=false;
}
}
if(flag) printf("NONE\n");
}
}

poj2378 树形DP的更多相关文章

  1. [poj3107/poj2378]Godfather/Tree Cutting树形dp

    题意:求树的重心(删除该点后子树最大的最小) 解题关键:想树的结构,删去某个点后只剩下它的子树和原树-此树所形成的数,然后第一次dp求每个子树的节点个数,第二次dp求解答案即可. 此题一开始一直T,后 ...

  2. 树形DP小结

    树形DP1.简介:树是一种数据结构,因为树具有良好的子结构,而恰好DP是从最优子问题更新而来,那么在树上做DP操作就是从树的根节点开始深搜(也就是记忆化搜索),保存每一步的最优结果.tips:树的遍历 ...

  3. 树形 DP 总结

    树形 DP 总结 本文转自:http://blog.csdn.net/angon823/article/details/52334548 介绍 1.什么是树型动态规划 顾名思义,树型动态规划就是在“树 ...

  4. poj3417 LCA + 树形dp

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Descripti ...

  5. COGS 2532. [HZOI 2016]树之美 树形dp

    可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...

  6. 【BZOJ-4726】Sabota? 树形DP

    4726: [POI2017]Sabota? Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 128  Solved ...

  7. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

  8. 树形DP

    切题ing!!!!! HDU  2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...

  9. BZOJ 2286 消耗战 (虚树+树形DP)

    给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...

随机推荐

  1. mysql 5.7.9(GA) 安装

    mysql 5.7.9(GA) 终于发布了,感受一下. 一.下载 下载页面 http://dev.mysql.com/downloads/mysql/ 选择相应系统的版本下载. 本文OS为centos ...

  2. SmartQQ二维码登陆接口分析

    SmartQQ是腾讯在Web上推出的一款单纯的聊天工具,pc端与移动端都可以访问,接下来具体的分析下登陆流程. 网站:http://w.qq.com/ 工具:这个随意能够看到http数据包就可以,浏览 ...

  3. JS全部API笔记

    我相信对于程序猿都有做笔记的习惯. 我初学到现在也做了不少笔记,以前,总是怕写的文章或者好的内容分享出来就怕被直接copy以后更个名就不再是你的. 但通过博客园,学习到不少东西,人家都不怕什么了,我自 ...

  4. 3步学会用gulp

    1.安装gulp 安装gulp到全局:npm install -g gulp 安装gulp到某个项目:npm install --save gulp 注意:请先安装nodejs(自带npm) 2.创建 ...

  5. 文成小盆友python-num9 socket编程

    socket编程 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制,取后一种意思 ...

  6. 关于lambda表达式树

    总而言之: 就是在表达式中没有花括号. IEnumerable<Rect> rectlist3 = rectlist.Select(rect =>newRect(rect.X + 2 ...

  7. fontresize 移动端的手机字体 大小设置

    这段js 需要置于页面上端 也就是 需要先加载js然后加载页面 (这段js是原生js而且比较短小 基本对页面加载速度无影响) FontResize : function(maxWidth){ (fun ...

  8. iOS 两种方法实现左右滑动出现侧边菜单栏 slide view

      现在很多的APP中都有slide view,左右滑动出现侧边菜单栏的功能,Weico这个应用就有. 网上有很多第三方的类库实现了这种效果,其实自己代码写的话也是很简单的,下面我将介绍两种方法实现s ...

  9. 改变VC生成exe图标

    默认 VC 生成的 EXE 文件的图标很大众,可以根据以下办法修改 exe 的图标. 以 Visual C++ 6.0 为例: 1. 创建项目,添加代码,并且保证项目可以正常编译. 2. 为项目增加资 ...

  10. 安装Visual Studio 2010之后怎样安装MSDN Library

    这篇博客参考自:http://justargon.blog.163.com/blog/static/21394413020134100737688/ MSDN2010安装及使用(MSDN Librar ...