[bzoj1131][POI2008]Sta
来源:互联网 发布:台湾处境现状 知乎 编辑:程序博客网 时间:2024/06/05 06:37
1131: [POI2008]Sta
Time Limit: 10 Sec Memory Limit: 162 MB[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
1 4
5 6
4 5
6 7
6 8
2 4
3 4
Sample Output
7
HINT
sum表示子树,anti表示父亲作为子树的值
#include<bits/stdc++.h>using namespace std;const int N = 1000000 + 5;long long siz[N],anti[N],sum[N],ans; int ans2,last[N*2],cnt,n;struct Edge{ int to,next; }e[N*2];void insert( int u, int v ){e[++cnt].to = v; e[cnt].next = last[u]; last[u] = cnt;e[++cnt].to = u; e[cnt].next = last[v]; last[v] = cnt;}void dfs( int x, int f ){siz[x] = 1;for( int i = last[x]; i; i = e[i].next )if( e[i].to ^ f ){dfs( e[i].to, x );siz[x] += siz[e[i].to];sum[x] += (sum[e[i].to]+siz[e[i].to]);}}void dfs2( int x, int f ){for( int i = last[x]; i; i = e[i].next )if( e[i].to ^ f ){anti[e[i].to] = anti[x] + sum[x] - sum[e[i].to] - siz[e[i].to] + n - siz[e[i].to];dfs2( e[i].to, x );}if( anti[x] + sum[x] >= ans ){if( anti[x] + sum[x] > ans ) ans = anti[x] + sum[x], ans2 = x;else ans2 = min( ans2, x );}}int main(){scanf("%d", &n);for( int i = 1,u,v,w; i < n; i++ ){scanf("%d%d", &u, &v);insert( u, v );}dfs( 1, 0 ); dfs2( 1, 0 );printf("%d\n", ans2);return 0;}
阅读全文
0 0
- [BZOJ1131] [POI2008]Sta
- 【POI2008】【BZOJ1131】Sta
- 【BZOJ1131】[POI2008]Sta【TreeDP】
- 【bzoj1131】 [POI2008]Sta
- [bzoj1131][POI2008]Sta
- BZOJ1131 [POI2008]Sta
- bzoj1131: [POI2008]Sta
- 【bzoj1131】[POI2008]Sta
- bzoj1131: [POI2008]Sta 树形dp
- BZOJ1131 [POI2008]Sta 树DP
- bzoj1131 [POI2008]Sta 树形DP
- [BZOJ1131][POI2008]Sta(树形dp+维护)
- [bzoj1131][POI2008]Sta(树上dp)
- [BZOJ1131][POI2008]Sta(树形dp+维护)
- [POI2008]Sta
- bzoj 1131: [POI2008]Sta
- BZOJ 1131 [POI2008]Sta
- bzoj 1131 [POI2008]Sta
- Spark源码解析之SparkSql
- Mybatis 循环遍历 foreach
- 网站响应式页面查看工具源代码
- Windows下安装MySQL解压缩版
- TensorFlow(三) 常用的 Python API
- [bzoj1131][POI2008]Sta
- WinScp初次连接Linux(Ubuntu)问题
- CSS基础知识
- android 主流工具类的封装和使用
- eclipse,idea搜索手动添加到本地maven库的jar
- mediaplayer+surfaceview实现视频播放以及全屏窗口切换
- 事务管理
- 阿里云 LNMP 环境配置
- hive 服务