题目链接: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. elk docker-compose

    version: '3.1' services: elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4 c ...

  2. 如何避免Linux操作系统客户端登陆超时-linux命令之TMOUT=

    工作中经常遇到使用ssh,telnet工具登陆Linux操作系统时,出现的超时问题,怎么处理呢? 添加下面命令: TMOUNT=

  3. FreeRTOS 移植

    添加FreeRTOS源码到工程中 在工程源码中创建FreeRTOS目录存放拷贝的文件 拷贝FreeRTOS->Source中的文件 可将其他不需要的文件夹全部删掉,只留3个 拷贝Demo中Fre ...

  4. IO模型之NIO代码及其实践详解

    一.简介 NIO我们一般认为是New I/O(也是官方的叫法),因为它是相对于老的I/O类库新增的( JDK 1.4中的java.nio.*包中引入新的Java I/O库).但现在都称之为Non-bl ...

  5. jmeter第一次使用

    创建线程组 依次创建后,在http请求页面填入接口地址,参数,头信息,点击运行,然后查看结果树

  6. 【Distributed】分布式解决方案【汇总】

    一.问题引出 二.分布式Session问题 三.网站跨域问题 四.分布式任务调度平台 五.分布式配置中心 六.分布式锁解决方案 七.缓存技术 一.问题引出 [Distributed]分布式系统中遇到的 ...

  7. 定制centos6.5自动安装ISO光盘

    一 ISO定制项清单 安装系统为centos6.5 (base server),安装方式为全新安装 使用ext4分区格式 安装前可以交互输入root密码,主机名,swap分区大小,之后安装过程自动化 ...

  8. [ansible-playbook]4 持续集成环境之分布式部署利器 ansible playbook学习

    3 ansible-play讲的中太少了,今天稍微深入学习一点 预计阅读时间:15分钟 一: 安装部署 参考 http://getansible.com/begin/an_zhuang_ansile ...

  9. windows RabbitMQ Server 环境配置中的一些坑

    原文:https://blog.csdn.net/lindonglian/article/details/55805637 RabbitMQ的服务端基于Erlang语言编写,要在机器上安装Rabbit ...

  10. jade变量声明和数据传递

    声明一个变量 - var course = 'jade'; 取得一个变量 #{course} 大括号里面写入变量命,前面加个#号就可以取得变量 在括号里面可以进行诸多都运行操作,比如大小写 #{cou ...