和poj3107,poj1655一样的方法

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
#define INF 100000000
#define eps 1e-8
#define pii pair<int,int>
#define LL long long int
struct node
{
int v,next;
}e[];
int n,a,b,head[];
int num[],bal[],cnt=;
void add(int aa,int bb);
int dfs1(int x,int fa);
void dfs2(int x,int fa);
int main()
{
//freopen("in1.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&n);
memset(head,-,sizeof(int)*(n+));
for(int i=;i<=n;i++) num[i]=;
for(int i=;i<=n-;i++)
{
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
dfs1(,-);
dfs2(,-);
for(int i=;i<=n;i++)
{
if(bal[i]<=n/)
{
printf("%d\n",i);
}
}
//fclose(stdin);
//fclose(stdout);
return ;
}
void add(int aa,int bb)
{
e[cnt].v=bb;
e[cnt].next=head[aa];
head[aa]=cnt++;
}
int dfs1(int x,int fa)
{
for(int i=head[x];i!=-;i=e[i].next)
{
if(e[i].v==fa)
continue;
else
{
num[x]+=dfs1(e[i].v,x);
}
}
return num[x];
}
void dfs2(int x,int fa)
{
for(int i=head[x];i!=-;i=e[i].next)
{
if(e[i].v==fa)
{
bal[x]=max(bal[x],n-num[x]);
}
else
{
bal[x]=max(bal[x],num[e[i].v]);
dfs2(e[i].v,x);
}
}
}

poj2378(dfs,树形dp)的更多相关文章

  1. 杭电OJ——1011 Starship Troopers(dfs + 树形dp)

    Starship Troopers Problem Description You, the leader of Starship Troopers, are sent to destroy a ba ...

  2. POJ 1849 - Two - [DFS][树形DP]

    Time Limit: 1000MS Memory Limit: 30000K Description The city consists of intersections and streets t ...

  3. 图论--树的直径--DFS+树形DP模板

    #include <iostream> #include <cstring> using namespace std; //maxv:源点能到的最远点,maxdis:最远点对应 ...

  4. Codeforces 765E. Tree Folding [dfs][树形dp]

    题解:先从节点1开始dfs.对于每一个节点,用一个set记录:以该点为根的子树的深度. a) 如果此节点的某个子节点打出了GG,则此节点直接打出GG. b) 若set的元素个数<=1,那么,以该 ...

  5. poj1655(dfs,树形dp,树的重心)

    这是找树的重心的经典题目. 树的重心有下面几条常见性质: 定义1:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心.定义2:以这个点为根,那么所有的子树(不算整个树自身)的大 ...

  6. poj1655(dfs,树形dp,树的重心)(点分治基础)

    题意:就是裸的求树的重心. #include<cstring> #include<algorithm> #include<cmath> #include<cs ...

  7. CF979C Kuro and Walking Route(简单的dfs/树形dp)

    题意:给出一个$n$个点,$n-1$条边的无向连通图,给出两个点$x,y$,经过$x$后的路径上就不能经过$y$,问可以走的路径$(u,v)$有多少条,($(u,v)$和$(v,u)$考虑为两条不同的 ...

  8. 【bzoj2435】[NOI2011]道路修建 树形dp

    题目描述 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿意修建恰好 n – 1条双向道路. 每条道路的修 ...

  9. HDU5834Magic boy Bi Luo with his excited tree 树形dp

    分析:典型的两遍dfs树形dp,先统计到子树的,再统计从祖先来的,dp[i][0]代表从从子树回来的最大值,dp[i][1]代表不回来,id[i]记录从i开始到哪不回来 吐槽:赛场上想到了状态,但是不 ...

随机推荐

  1. Spark1.x和2.x如何读取和写入csv文件

    看很多资料,很少有讲怎么去操作读写csv文件的,我也查了一些.很多博客都是很老的方法,还有好多转来转去的,复制粘贴都不能看.下面我在这里归纳一下,以免以后用到时再费时间去查.前端实现文件下载和拖拽上传 ...

  2. MySQL-Last_Errno: 1594

    故障现象  :MySQL slave所在机器自动重启,启动MySQL后,查看主从信息如下: Error_code: 1594   mysql> show slave status \G . ro ...

  3. cisco笔记

    交换机 show cdp neighbors 显示邻居信息 路由 show ip interface brief 显示接口ip

  4. 20145201 实验二 Java面向对象程序设计

    20145201实验二 Java面向对象程序设计 初步掌握单元测试和TDD 实验步骤 (一)单元测试 (1) 三种代码 编程是智力活动,不是打字,编程前要把干什么.如何干想清楚才能把程序写对.写好.与 ...

  5. String创建方式的区别

    String str0 = "abc"; String str1 = new String("abc"); 第一句执行后,会在String pool中创建一个& ...

  6. 华为交换机S5700系列配置通过STelnet登录设备示例

    配置通过STelnet登录设备示例 组网图形 图1 配置用户通过STelnet登录设备组网图 在服务器端生成本地密钥对 <HUAWEI> system-view [HUAWEI] sysn ...

  7. HDU 1337 && POJ 1218&& zju 1350 方法总结

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1337 杭电 http://poj.org/problem?id=1218清华 http://acm.zj ...

  8. 迁移cnblog博客

    title: 迁移cnblog博客 date: 2018-03-18 categories: cnblogs tags: life 写博客最怕就是这种迁来迁去的,太麻烦了 还好老早就使用markdow ...

  9. Which HTTP methods match up to which CRUD methods?

    https://stackoverflow.com/questions/6203231/which-http-methods-match-up-to-which-crud-methods   Crea ...

  10. Java中的日期和时间

    Java中的日期和时间 Java在java.util包中提供了Date类,这个类封装了当前的日期和时间. Date类支持两种构造函数.第一个构造函数初始化对象的当前日期和时间. Date() 下面的构 ...