题目链接:http://codeforces.com/problemset/problem/1187/E

E. Tree Painting

You are given a tree (an undirected connected acyclic graph) consisting of nn vertices. You are playing a game on this tree.

Initially all vertices are white. On the first turn of the game you choose one vertex and paint it black. Then on each turn you choose a white vertex adjacent (connected by an edge) to any black vertex and paint it black.

Each time when you choose a vertex (even during the first turn), you gain the number of points equal to the size of the connected component consisting only of white vertices that contains the chosen vertex. The game ends when all vertices are painted black.

Let's see the following example:

Vertices 1 and 4 are painted black already. If you choose the vertex 2, you will gain 4 points for the connected component consisting of vertices 2,3,5 and 6.

If you choose the vertex 9, you will gain 3 points for the connected component consisting of vertices 7,8 and 9.

Your task is to maximize the number of points you gain.

Input

The first line contains an integer nn — the number of vertices in the tree (2≤n≤2⋅105).

Each of the next n−1 lines describes an edge of the tree. Edge i is denoted by two integers ui and vi, the indices of vertices it connects (1≤ui,vi≤n, ui≠vi).

It is guaranteed that the given edges form a tree.

Output

Print one integer — the maximum number of points you gain if you will play optimally.

Examples
input
9
1 2
2 3
2 5
2 6
1 4
4 9
9 7
9 8
output
36
input
5
1 2
1 3
2 4
2 5
output
14
Note

The first example tree is shown in the problem statement.

题意:给你一棵树,给一个操作:每次选择一个已被涂黑的节点相邻的未被涂黑的节点,并获得一个值(等于与选中节点相通(不经过黑色节点,可以互相到达)的未被涂黑节点的数量)。

初始所有节点均未被涂黑,你可以任意涂黑一个节点(并获得值),然后重复上面的操作,问可以获得的值最大是多少。

思路:因为题目给我们的是一棵树,所以当我们选定一个点为根节点进行操作时,我们可以获得的值就确定了,所以我们可以随便选一个节点为根节点,然后以它的值求出以其他节点为根节点可以获得的值,最后取最大值就可以了。

代码:

#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
struct{
int v,next;
}edge[];
int head[];
struct{
ll num;//子树节点数
ll sum;//以当前节点为根节点的子树可以获得的最大值
}p[],ans[];
int cnt;
ll mx;
void add(int u,int v){
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void dfs(int k,int fz){
p[k].num=;
p[k].sum=;
for(int i=head[k];i!=-;i=edge[i].next){
int v=edge[i].v;
if(v!=fz){
dfs(v,k);
p[k].num+=p[v].num;//计算以当前节点为根节点的子树节点数
p[k].sum+=p[v].sum+p[v].num;//计算值
}
}
}
void dfs1(int k,int fz){
if(fz==){
ans[k].num=p[k].num;
ans[k].sum=p[k].sum;
}
else{
ans[k].sum=p[k].sum+ans[fz].sum-p[k].num-p[k].sum+ans[fz].num-p[k].num;//我们已经知道他父亲节点的值
//我们可以把当前节点看着父节点,父亲节点看着他的儿子节点,然后就可以和dfs中求值一样求了 ans[k].num=ans[fz].num;
}
//printf("%d %lld %lld %d %lld %lld\n",k,ans[k].num,ans[k].sum,fz,ans[fz].num,ans[fz].sum);
mx=max(mx,ans[k].sum);
for(int i=head[k];i!=-;i=edge[i].next){
int v=edge[i].v;
if(v!=fz){
dfs1(v,k);
}
}
}
int main(){
int n;
cnt=;
mx=;
scanf("%d",&n);
int u,v;
fill(head,head+n+,-);
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(,);//随便以一个点为根节点求值
dfs1(,);
printf("%lld\n",mx);
return ;
}

codeforces1187E的更多相关文章

随机推荐

  1. springboot学习入门简易版三---springboot2.0启动方式

    2.4使用@componentscan方式启动 2.4.1 @EnableAutoConfiguration 默认只扫描当前类 @EnableAutoConfiguration 默认只扫描当前类,如果 ...

  2. Linux命令——set 和 unset

    参考:Linux set and unset http://www.runoob.com/linux/linux-comm-set.html https://blog.csdn.net/u010003 ...

  3. python中redis

    一.简介 二.redis的安装和使用 三.python操作readis之安装和支持存储类型 四.python操作redis值普通链接 五.python操作redis值连接池 六.操作之String操作 ...

  4. 开源框架---tensorflow c++ API 一个卡了很久的问题

    <开源框架---tensorflow c++ API 运行第一个“手写字的例子”> 中可以说明tensorflow c++ API是好用的,.......

  5. Codeforces Round #512 (Div. 2) D. Vasya and Triangle(几何+思维)

    题目 题意: 给出 n,m,k ,让你在长为 n,宽为 m 的坐标系里构建一个三角形,使得面积= n*m/k.如果存在,输出“YES”,输出三角形三个顶点的坐标:  如果不存在,输出“NO”. 思路: ...

  6. 【BZOJ3691】游行 最小可相交路径覆盖转化

    因为C是不断变化的而且C是和点权相关和边权无关 所以我们可以MCMF但是MCMF的时候不能与C相关 再分析问题 我们可以认为每条路径S->T只覆盖T这个终点 因为题目中说了如果Si != Ti ...

  7. re模块及其用法

    一.re模块下的常用方法 首先在使用re模块之前,需要引入re模块 import re 1.与查找相关的: 1.findall 返回列表,找到所有的匹配项 ret = re.findall(" ...

  8. nexus私服搭建及信息配置

    nexus私服搭建及信息配置 下载 登录nexus官网下载nexus安装包https://help.sonatype.com/repomanager2/download/download-archiv ...

  9. Caused by: java.lang.ClassNotFoundException: org.fusesource.jansi.WindowsAnsiOutputStream

    08:23:18,995 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate append ...

  10. PL/SQL嵌入SQL语句

    一.PL/SQL块中只能直接嵌入SELECT.DML(INSERT,UPDATE,DELETE)以及事务控制语句(COMMIT,ROLLBACK,SAVEPOINT), 而不能直接嵌入DDL语句(CR ...