对于一棵树, 考虑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. Curling 2.0(dfs回溯)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15567   Accepted: 6434 Desc ...

  2. POJ1797 Heavy Transportation 【Dijkstra】

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 21037   Accepted:  ...

  3. CALayer3-层的属性

    一.隐式动画属性 * 在前面几讲中已经提到.每个UIView内部都默认关联着一个CALayer,我们可用称这个Layer为Root Layer(根层). 全部的非Root Layer.也就是手动创建的 ...

  4. 改变status bar的状态

    两种改变status bar状态的方法 一 :(全局的) 直接在当前控制器中(一般是在navigationcontroller) //- (UIStatusBarStyle)preferredStat ...

  5. UIView 设置阴影(属性说明)

    以下代码实现: 第一个图片的代码 //加阴影--任海丽编辑 _imageView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowCo ...

  6. HTML5 标准属性 NEW:HTML 5 中新的标准属性。 注释:HTML 4.01 不再支持 accesskey 属性:

    属性 值 描述 accesskey character 规定访问元素的键盘快捷键 class classname 规定元素的类名(用于规定样式表中的类). contenteditable true f ...

  7. nginx启动

    查看nginx的进程 ps -ef | grep nginx 重启nginx的3种办法1.service nginx restart2.改了配置文件让其生效办法 nginx -s reload3.到n ...

  8. UNIX网络编程 12 15共享内存区

    管道,FIFO,消息队列,在两个进程交换信息时,都要经过内核传递 共享内存可以绕过,默认fork生成的子进程 并不与父进程共享内存区 mmap munmap msync 父子进程共享内存区的方法之一是 ...

  9. 复习知识点:GCD多线程

    GCD的基础 #pragma mark - 使用GCD 创建一个 串行 队列 // 第一种:系统提供的创建串行队列的方法 // 在真正的开发中如果需要创建串行队列,比较习惯用这种 // dispatc ...

  10. The Hungarian algorithm Template

    The Hungarian algorithm with The adjacency matrix : 计算最大匹配问题 int n1, n2, m, ans; int res[MAXN]; bool ...