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)<= ... 
随机推荐
- cas+tomcat+shiro实现单点登录-1-tomcat添加https协议
			目录 1.tomcat添加https安全协议 2.下载cas server端部署到tomcat上 3.CAS服务器深入配置(连接MYSQL) 4.Apache Shiro 集成Cas作为cas cli ... 
- Java系列--第四篇 基于Maven的SSME之发送邮件
			在系列第一篇中,使用的是mybatis得到了一个小小的项目,而该项目的用户对象是有邮件地址的,如果按照邮件地址给对方去一封邮件会不会更能体现针对性呢,所以,我在这篇准备加入发送邮件的功能,利用的就是s ... 
- 使用Android平板编程,执行linux命令
			android有一些应用支持开发, AIDE 介绍http://www.wandoujia.com/apps/com.aide.ui https://play.google.com/store/app ... 
- python之7-1类
			面向对象的编程,其实是将对象抽象成类,然后在类中,通过init定义实例初始化函数和多个操作实例的函数. 整个类就如同一个模板,我们可以用这个模板生成众多具现实例,并赋予实例动作. py中定义类的大致格 ... 
- C语言初学 比较五个整数并输出最大值和最小值1
			#include<stdio.h> #include<math.h> int max(int x,int y) { if(x>y) return x; else retu ... 
- Android中写入读取XML
			获取XML文件的基本思路是,通过getResources().getXml()获的XML原始文件,得到XmlResourceParser对象,通过该对象来判断是文档的开头还是结尾,是某个标签的开始还是 ... 
- Eclipse远程调试weblogic
			http://www.cnblogs.com/dyllove98/archive/2013/08/06/3241140.html http://blog.csdn.net/afgasdg/articl ... 
- LeetCode_Restore IP Addresses
			Given a string containing only digits, restore it by returning all possible valid IP address combina ... 
- 自制单片机之十三……时钟IC_DS1302
			在网上看了很久,发现初学者最有兴趣的就是DS1302时钟电路,也很自然,它是个做出来就让你觉得最实用的电路了,但实际上制做上并不简单,首先你要让你的显示部分(不管是数码管还是LCD)调试通过.然后把D ... 
- 自制单片机之六……串行I2C总线E2PROM AT24CXXX的应用
			这一篇介绍I2C存储器的使用.主要是介绍AT24CXX系列器件,它分为两类,主要是通过被存储容量地址来分的,一类是AT24C02-AT24C16,它的存储容量从256字节到2048字节.另一类是AT2 ... 
