[POI2008]STA-Station
一道树形dp题。
令dp[u]表示以u为根时所有点的深度之和。考虑u到他的一个子节点v时答案的变化,v子树以外的点的深度都加1,v子树以内的点的深度都减1,所以dp[v] = dp[u] + (n - siz[v]) - siz[v]。于是dp式就搞出来了。
所以两边dfs,第一遍求siz和dp[1],第二遍更新答案。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e6 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, ans = ;
struct Edge
{
int nxt, to;
}e[maxn << ];
int head[maxn], ecnt = ;
void addEdge(int x, int y)
{
e[++ecnt] = (Edge){head[x], y};
head[x] = ecnt;
} int siz[maxn], dep[maxn];
ll dp[maxn];
void dfs1(int now, int f)
{
siz[now] = ;
for(int i = head[now]; i; i = e[i].nxt)
{
if(e[i].to == f) continue;
dep[e[i].to] = dep[now] + ;
dp[] += dep[e[i].to];
dfs1(e[i].to, now);
siz[now] += siz[e[i].to];
} }
void dfs2(int now, int f)
{
for(int i = head[now]; i; i = e[i].nxt)
{
if(e[i].to == f) continue;
dp[e[i].to] = dp[now] + n - (siz[e[i].to] << );
if(dp[e[i].to] > dp[ans]) ans = e[i].to;
dfs2(e[i].to, now);
}
} int main()
{
n = read();
for(int i = ; i < n; ++i)
{
int x = read(), y = read();
addEdge(x, y); addEdge(y, x);
}
dfs1(, ); dfs2(, );
write(ans); enter;
return ;
}
[POI2008]STA-Station的更多相关文章
- BZOJ 1131: [POI2008]Sta( dfs )
对于一棵树, 考虑root的答案向它的孩子转移, 应该是 ans[son] = (ans[root] - size[son]) + (n - size[son]). so , 先 dfs 预处理一下, ...
- 1131: [POI2008]Sta
1131: [POI2008]Sta Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 783 Solved: 235[Submit][Status] ...
- BZOJ1131 POI2008 Sta 【树形DP】
BZOJ1131 POI2008 Sta Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=10 ...
- bzoj 1131 [POI2008]Sta 树形dp 转移根模板题
[POI2008]Sta Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1889 Solved: 729[Submit][Status][Discu ...
- [POI2008]Sta(树形dp)
[POI2008]Sta Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面 ...
- [BZOJ1131][POI2008] Sta 树的深度
Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. Output ...
- 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 ...
- [BZOJ1131/POI2008]Sta树的深度
Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. Output ...
- BZOJ 1131: [POI2008]Sta
Description 一棵树,问以那个节点为根时根的总和最大. Sol DFS+树形DP. 第一遍统计一下 size 和 d. 第二遍转移根,统计答案就行了. Code /************* ...
- bzoj1131: [POI2008]Sta
思路:首先先求出以1为根的答案,然后考虑由i转移到i的儿子的答案的变化,显然以son[i]为根的子树的所有结点的深度都会减一,其余的点的深度都会加一,然后就可以直接O(n)求出所有结点的答案,然后取m ...
随机推荐
- 转 UTL_FILE Throws ORA-29284 Or ORA-29283 When Attempting To READ File
APPLIES TO: PL/SQL - Version 9.2.0.8 and laterInformation in this document applies to any platform.* ...
- springsource-tool-suite下载(sts)
1 新版本的插件下载 1 直接进入官网下载即可 官网地址:http://spring.io/tools/sts/all. 2 spring官网上下载历史版本的spring插件 1 获取新版本的插件的地 ...
- Coursera 机器学习 第7章 Support Vector Machines 学习笔记
7 Support Vector Machines7.1 Large Margin Classification7.1.1 Optimization Objective支持向量机(SVM)代价函数在数 ...
- What is Network Address Translation?---reference
http://whatismyipaddress.com/nat What is Network Address Translation? Network Address Translation (N ...
- centos6.5 源码编译 mysql5.6.21
1.yum安装各个依赖包 [root@WebServer ~]# yum -y install gcc gcc-devel gcc-c++ gcc-c++-devel autoconf* automa ...
- MATLAB中的概率论与数理统计
概率论与数理统计 产生随机数 binornd poissrnd exprnd unidrnd normrnd 概率密度函数(pdf) binopdf poisspdf geopdf unidpdf n ...
- react native 完美解决启动白屏
先讲下我的RN版本0.58.5 首先安装react-native-splash-screen(目前使用的版本是3.2.0) 项目地址https://github.com/crazycodeboy/re ...
- tornado handler 方法复用的 3 个方法
tornado handler 调用 特性 在一次 tornado 请求中调用其他 tornado handler 中的方法, 比如 run 方法 引言 在后台开发中, 有时需要做一些功能的整合, 比 ...
- js数据类型检测小结
在js中,有四种用于检测数据类型的方式,分别是: typeof 用来检测数据类型的运算符 instanceof 检测一个实例是否属于某个类 constructor 构造函数 Object.protot ...
- 测试UTC时间可以用的网址
需要FQ的网址 https://time.artjoey.com/cn/ 不需要FQ的网址 https://time.is/ http://time.123cha.com/?q=Minsk