题目

The first stage of train system reform (that has been described in the problem Railways of the third stage of 14th Polish OI.

However, one needs not be familiar with that problem in order to solve this task.) has come to an end in Byteotia. The system consists of bidirectional segments of tracks that connect railway stations. No two stations are (directly) connected by more than one segment of tracks.

Furthermore, it is known that every railway station is reachable from every other station by a unique route. This route may consist of several segments of tracks, but it never leads through one station more than once.

The second stage of the reform aims at developing train connections.

Byteasar count on your aid in this task. To make things easier, Byteasar has decided that:

one of the stations is to became a giant hub and receive the glorious name of Bitwise, for every other station a connection to Bitwise and back is to be set up, each train will travel between Bitwise and its other destination back and forth along the only possible route, stopping at each intermediate station.

It remains yet to decide which station should become Bitwise. It has been decided that the average cost of travel between two different stations should be minimal.

In Byteotia there are only one-way-one-use tickets at the modest price of bythaler, authorising the owner to travel along exactly one segment of tracks, no matter how long it is.

Thus the cost of travel between any two stations is simply the minimum number of tracks segments one has to ride along to get from one stations to the other.

Task Write a programme that:

reads the description of the train system of Byteotia, determines the station that should become Bitwise, writes out the result to the standard output.

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

输入格式

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

输出格式

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

题解

随便取一个点做根,比如1号节点,然后从1号节点出发dfs每个节点,算出每棵子树的大小和每个节点的深度

然后再一次dfs,树形dp,求每个点做根时,所有点的深度之和,然后输出最大值即可.

那么转移方程怎么考虑?

这棵树从\(fa\)搜到\(root\)的时候,如何转移?

dp值的含义是所有点的深度,那么在树根从\(fa\)变成\(root\)时,所有点的深度之和怎么变化?

显然红色圈内所有点的深度+1,紫色圈内所有点深度-1

紫色圈内点数就是以\(root\)为根的子树的大小,记为\(size\),则紫色圈内点数就是总点数减去\(size\)即\(n-size\)

所以转移方程就是:

\(dp_{root} = dp_{fa} - size_{root} + (n-size_{root})
\\\ \ \ \ \ \ \ \ \ \ \ = dp_{fa} + n - 2 \times size_{root}
\)

还要注意用long long

代码

#include <cstdio>
const int maxn = 1000005;
int head[maxn], tot, n, ans, fa[maxn], size[maxn], ix, iy;
long long dp[maxn];
struct Edge { int to, next; } edges[maxn << 1];
inline int input() { int t; scanf("%d", &t); return t; }
void add(int x, int y) { edges[++tot].to = y; edges[tot].next = head[x]; head[x] = tot; }
void dfs(int root, int fa) {
size[root] = 1;
for (int x = head[root]; x; x = edges[x].next) {
if (edges[x].to == fa) continue;
dfs(edges[x].to, root);
size[root] += size[edges[x].to];
dp[root] += dp[edges[x].to] + size[edges[x].to];
}
}
void dpf(int root, int fa) {
if (root != 1) dp[root] = dp[fa] + n - size[root] * 2;
for (int x = head[root]; x; x = edges[x].next)
if (edges[x].to != fa) dpf(edges[x].to, root);
}
int main() {
n = input();
for (int i = 1; i < n; i++) add(ix = input(), iy = input()), add(iy, ix);
dfs(1, 0), dpf(1, 0);
for (int i = 1; i <= n; i++) if (dp[i] > dp[ans]) ans = i;
printf("%d\n", ans);
}

BZOJ 1131 [POI2008] STA-Station 题解的更多相关文章

  1. BZOJ 1131: [POI2008]Sta( dfs )

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

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

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

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

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

  4. BZOJ 1131: [POI2008]Sta

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

  5. 1131: [POI2008]Sta

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

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

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

  7. BZOJ1131 POI2008 Sta 【树形DP】

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

  8. BZOJ 4033: [HAOI2015]树上染色题解

    BZOJ 4033: [HAOI2015]树上染色题解(树形dp) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1327400 原题地址: BZOJ 403 ...

  9. [POI2008]Sta(树形dp)

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

随机推荐

  1. Redis企业级数据备份与恢复方案

    一.持久化配置 RBD和AOF建议同时打开(Redis4.0之后支持) RDB做冷备,AOF做数据恢复(数据更可靠) RDB采取默认配置即可,AOF推荐采取everysec每秒策略 AOF和RDB还不 ...

  2. bat 脚本定时删除备份文件

    删除 D:\yswbak 目录下rar类型 6天前的 文件 @echo off forfiles /p D:\yswbak /m *.rar /d -6 /c "cmd /c del @pa ...

  3. centos7上安装redis以及PHP安装redis扩展(二)

    PHP 使用 Redis 安装 开始在 PHP 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 PHP redis 驱动,且你的机器上能正常使用 PHP. 接下来让我们安装 PH ...

  4. swift - TextView和TextField之return隐藏回收键盘

    一.点击界面空白处即可收起键盘,空白处不能有其他控件的响应事件. //点击空白处关闭键盘 override func touchesEnded(_ touches: Set<UITouch> ...

  5. JSON案例

    原文链接:https://zhuanlan.zhihu.com/p/62763428 json字符串->JSONObject 用JSON.parseObject()方法即可将JSon字符串转化为 ...

  6. ado.net Web前端:关于JavaScript知识点的简单梳理

    学习js:1.htmml2.cssjs+html+css == html5 js的组成:1).ecamscript ES是js的标准,js 是es 的实现2)文档对象模型(Document Objec ...

  7. css方法1(清除ul边距间隙,两端对齐,字母大写,首字放大)

    一.清除ul自带左边间距 ul{ margin:; padding:; } 二.ul li 与li  之间隙 1.ul 设置font-size:0 ; 子li 设置字体大小 2.把li写到一起,不换行 ...

  8. Laya 吐槽日志.

    新换了一个公司,公司有两个产品都是用的laya, 一个as写的2D游戏, 一个ts写的3D游戏 as写小游戏,各种不舒服啊,  一堆 __JS这样的代码,   体验极差. laya IDE 按钮只能做 ...

  9. platform驱动架构初探

    platform总线是Linux2.6引入的虚拟总线,这类总线没有对应的硬件结构.与之相反,USB总线和PCI总线在内核中是有对应的bus(USB-bus和PCI-bus)的.为了统一管理CPU这些既 ...

  10. [白话解析] 通过实例来梳理概念 :准确率 (Accuracy)、精准率(Precision)、召回率(Recall)和F值(F-Measure)

    [白话解析] 通过实例来梳理概念 :准确率 (Accuracy).精准率(Precision).召回率(Recall)和F值(F-Measure) 目录 [白话解析] 通过实例来梳理概念 :准确率 ( ...