poj2378 树形DP
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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 <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的更多相关文章
- [poj3107/poj2378]Godfather/Tree Cutting树形dp
题意:求树的重心(删除该点后子树最大的最小) 解题关键:想树的结构,删去某个点后只剩下它的子树和原树-此树所形成的数,然后第一次dp求每个子树的节点个数,第二次dp求解答案即可. 此题一开始一直T,后 ...
- 树形DP小结
树形DP1.简介:树是一种数据结构,因为树具有良好的子结构,而恰好DP是从最优子问题更新而来,那么在树上做DP操作就是从树的根节点开始深搜(也就是记忆化搜索),保存每一步的最优结果.tips:树的遍历 ...
- 树形 DP 总结
树形 DP 总结 本文转自:http://blog.csdn.net/angon823/article/details/52334548 介绍 1.什么是树型动态规划 顾名思义,树型动态规划就是在“树 ...
- poj3417 LCA + 树形dp
Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4478 Accepted: 1292 Descripti ...
- COGS 2532. [HZOI 2016]树之美 树形dp
可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...
- 【BZOJ-4726】Sabota? 树形DP
4726: [POI2017]Sabota? Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 128 Solved ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
- 树形DP
切题ing!!!!! HDU 2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...
- BZOJ 2286 消耗战 (虚树+树形DP)
给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...
随机推荐
- 第1章 网络编程基础(4)——TCP/IP通信
TCP协议利用网络层IP协议提供不可靠的通信服务,解决分组的重传和排序问题,为应用程序提供可靠的.端到端的.面向连接的基于字节流的服务. 对等TCP传输实体间的通信具有如下特征: 全双工 只支持两个端 ...
- 《Hadoop权威》学习笔记五:MapReduce应用程序
一.API的配置---Configuration类 API的配置:Hadoop提供了专门的API对资源进行配置,Configuration类的实例(在org.apache.hadoop.conf包)包 ...
- CSS特殊性
样式的优先级取决于特殊性,特殊性为0,0,0,0 Ø每个元素或伪元素选择器贡献特殊性为 0,0,0,1 Ø每个类.伪类或者属性选择器的特殊性为 0,0,1,0 Ø每个ID选择器的特殊性为 0,1,0, ...
- python类方法与对象方法学习
class Test_Demo: TEST = 'test_value' def __init__(self,name,age): self.name = name self.age = age #s ...
- ECSTORE导航吸顶功能
ecstore导航吸顶功能,在导航父元素中加入id,如: <div id="mainNav1"></div> 在footer.html中添加以下js代码: ...
- C++得到最大的int值
要得到最大的int值: 1.利用(unsigned int)-1,这样得到的就是unsigned int表示的最大值. 2.int值只是比unsigned int多一位符号位,所以对(unsigned ...
- webapi 上传图片
需求:上传学员信息时同时上传头像信息,学员基本信息表和科目表为一对多关系表(添加基本信息后添加通过科目信息). 测试: [HttpPost] public string Post() { if ( ...
- gets和从键盘输入换行符
i was wrong! 虽然setbuf可以让程序自己管理缓冲,但是像getchar,gets这些标准IO函数还是要通过隐藏的stdin进行操作,而stdin是啥呢?还是一个FILE*,而FILE* ...
- Java7新语法 -try-with-resources
http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html The try-with- ...
- 【转】android 5.0 64bit系统加载库文件失败问题浅析
原文网址:http://blog.csdn.net/andrewblog/article/details/43601303 最近公司的一个项目使用android 5.0 64 bit平台,相对以前版本 ...