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. C++安装JSONCPP

    VS2013里新建一个空的控制台程序(用作测试jsoncpp是否可用),名为: TestJSON 解压下载好的文件:jsoncpp-src-0.5.0.tar.gz 利用VS2008打开jsoncpp ...

  2. 兄弟选择器 E + F

    兄弟选择器在IE7下支持会有bug,特记于此 如果兄弟选择器有Html注释,兄弟选择器在IE7下会失效  代码如下 E + Fp + p{color:red} <p class="te ...

  3. event对象具有的方法

    // dataTransfer,toElement,fromElement,y,x,offsetY,offsetX,webkitMovementY,webkitMovementX,relatedTar ...

  4. C++第三课(2013.10.03 )

    函数的默认参数: 1.函数的默认参数必须放在形参的右边而且在默认形参的右边不能出现没有无默认参数的形参 2.如果函数的声明给出了默认的参数,那么函数的实现就不能定义默认参 3.声明成员函数时没有给出默 ...

  5. Python 番外 消息队列设计精要

    消息队列已经逐渐成为企业IT系统内部通信的核心手段.它具有低耦合.可靠投递.广播.流量控制.最终一致性等一系列功能,成为异步RPC的主要手段之一.当今市面上有很多主流的消息中间件,如老牌的Active ...

  6. Python新手学习基础之循环结构练习

    有几个元音字母? 有一个字符串"I learn Python from maya",我们想要查找出它里面的元音字母(aeiou)(其实是找出这几个小写字母),并统计出其元音字符的个 ...

  7. ubuntu 14.04解决gedit中文乱码的问题

    终端输入 gsettings set org.gnome.gedit.preferences.encodings auto-detected "['UTF-8','GB18030','GB2 ...

  8. 安卓网络请求之——OkHttp学习

    之前做安卓项目的时候,HTTP请求用的是android api中的HttpURLConnection和HttpClient,编码比较繁琐,自己封装的也不好.后来知道有很多网络请求的第三方框架,可以方便 ...

  9. Activiti工作流学习-----基于5.19.0版本(7)

    八.BPMN 2.0流程图详解 BPMN 2.0的标准的出现是好事,用户不在被某个工作流开发商绑架或者在工作流中开发妥协,Activiti作为BPMN标准的一套解决方案,使得用户在选择工作流框架时可以 ...

  10. 关于zMPLS的设计解析

    zMPLS是一个关于mpls标准实现的开源软件,它起源于2002年6月份,项目终止于2006年,目前它已经可以支持ipv6,ipv4,ldp,cr-ldp,rsvp,rsvp-te等MPLS协议簇.该 ...