对于一棵树, 考虑root的答案向它的孩子转移, 应该是 ans[son] = (ans[root] - size[son]) + (n - size[son]).

so , 先 dfs 预处理一下, 然后再 DFS 求出各点答案 , 取最优即可

-----------------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
 
#define rep(i ,n) for(int i=0; i < n; ++i)
#define clr(x ,c) memset(x, c, sizeof(x))
 
using namespace std;
 
typedef long long ll;
 
const int maxn = 1000005;
 
struct edge {
int to;
edge*next;
} E[maxn << 1], *pt = E, *head[maxn];
 
void add(int u, int v) {
pt->to = v;
pt->next = head[u];
head[u] = pt++;
}
#define add_edge(u, v) add(u, v), add(v, u)
 
int size[maxn], dep[maxn], n;
ll ans[maxn];
 
void init() {
clr(head, 0);
cin >> n;
rep(i, n - 1) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
add_edge(u, v);
}
}
 
void dfs(int x, int fa) {
size[x] = 1;
for(edge*e= head[x]; e; e = e->next) if(e->to != fa) {
ans[0] += (dep[e->to] = dep[x] + 1);
dfs(e->to, x);
size[x] += size[e->to];
}
}
 
void DFS(int x, int fa) {
for(edge*e = head[x]; e; e = e->next) if(e->to != fa) {
ans[e->to] = ans[x] + n - 2 * size[e->to];
DFS(e->to, x);
}
}
 
void work() {
ans[0] = dep[0] = 0;
dfs(0, -1);
DFS(0, -1);
cout << max_element(ans, ans + n) - ans + 1 << "\n";
}
 
int main(){
freopen( "test.in" , "r" , stdin );
init();
work();
return 0;

-----------------------------------------------------------------------------------------------

1131: [POI2008]Sta

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 870  Solved: 271
[Submit][Status][Discuss]

Description

给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大

Input

给出一个数字N,代表有N个点.N<=1000000 下面N-1条边.

Output

输出你所找到的点,如果具有多个解,请输出编号最小的那个.

Sample Input

8
1 4
5 6
4 5
6 7
6 8
2 4
3 4

Sample Output

7

HINT

Source

BZOJ 1131: [POI2008]Sta( dfs )的更多相关文章

  1. bzoj 1131 [POI2008]Sta 树形dp 转移根模板题

    [POI2008]Sta Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1889  Solved: 729[Submit][Status][Discu ...

  2. BZOJ 1131: [POI2008]Sta

    Description 一棵树,问以那个节点为根时根的总和最大. Sol DFS+树形DP. 第一遍统计一下 size 和 d. 第二遍转移根,统计答案就行了. Code /************* ...

  3. BZOJ 1131 [POI2008]Sta(树形DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1131 [题目大意] 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度 ...

  4. 1131: [POI2008]Sta

    1131: [POI2008]Sta Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 783  Solved: 235[Submit][Status] ...

  5. Bzoj 1131[POI2008]STA-Station (树形DP)

    Bzoj 1131[POI2008]STA-Station (树形DP) 状态: 设\(f[i]\)为以\(i\)为根的深度之和,然后考虑从他父亲转移. 发现儿子的深度及其自己的深度\(-1\) 其余 ...

  6. BZOJ 1131 [POI2008] STA-Station 题解

    题目 The first stage of train system reform (that has been described in the problem Railways of the th ...

  7. BZOJ1131 POI2008 Sta 【树形DP】

    BZOJ1131 POI2008 Sta Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=10 ...

  8. [POI2008]Sta(树形dp)

    [POI2008]Sta Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面 ...

  9. bzoj千题计划151:bzoj1131: [POI2008]Sta

    http://www.lydsy.com/JudgeOnline/problem.php?id=1131 dp[i]=dp[fa[i]]-son[i]+n-son[i] #include<cst ...

随机推荐

  1. apache添加fastcgi支持

    A,安装apache服务器和fastcgi模块支持(ubuntu测试) sudo apt-get install apache2 sudo apt-get install libapache2-mod ...

  2. JqueryMobile新手问题大全

    Jquery mobile 新手问题总汇 34 2013-04-22 / 分类:JqueryMobile / 标签:JqueryMobile,Jqm 此文章将会持续更新,主要收录一些新手比较常见的问题 ...

  3. JSPatch技术文档

    一.背景需求介绍 为什么我们需要一个热修复(hot-fix)技术? 工作中容易犯错.bug难以避免. 开发和测试人力有限. 苹果Appstore审核周期太长,一旦出现严重bug难以快速上线新版本. 作 ...

  4. C# Best Practices - Accessing and Using Classes

    References and Using Do: Take care when defining references References must be one way (or circular ...

  5. lightOJ 1317 Throwing Balls into the Baskets

    lightOJ  1317  Throwing Balls into the Baskets(期望)  解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/ ...

  6. 高质量程序设计指南C/C++语言——C++/C编译预处理

    C++/C的编译预处理器对预编译伪指令进行处理后生成中间文件作为编译器的输入,因此所有的预编译伪指令都不会进入编译阶段.预编译伪指令一般都以#打头,且其前面只能出现空白字符.预编译伪指令不是C++/C ...

  7. 在 Windows Azure 网站 (WAWS) 上对 Orchard CMS 使用 Azure 缓存

    编辑人员注释: 本文章由 Windows Azure 网站团队的项目经理 Sunitha Muthukrishna 撰写. 如果您当前的 OrchardCMS 网站在 Windows Azure 网站 ...

  8. ios8 swift开发:显示变量的类名称

    var ivar = [:] ivar.className // __NSDictionaryI var i = 1 i.className // error: 'Int' does not have ...

  9. EntityFramework经典的left join语法

    /*  * 常常看到有人问linq语法怎样写left join的查询语句,但网上找到的都是简单的两表连接.參考意义有限.  * 今天最终项目里要用到复杂的多表连接,同一时候含有多个左连接,  * 恰好 ...

  10. LeetCode——Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...