题目链接: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. Java 之 TCP 通信程序

    一.概述 TCP 通信能实现两台计算机之间的数据交互,通信的两端,要严格区分为客户端(Client)与服务端(Server). 两端通信时步骤: 1.服务端程序,需要事先启动,等待客户端的连接: 2. ...

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

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

  3. ubuntu18.04 下启动Android Studio报错KVM is required to run this AVD. /dev/kvm device: permission denied.

    在ubuntu18.04下安装Android Studio,安装了模拟器后运行报错 KVM is required to run this AVD. /dev/kvm device: permissi ...

  4. LeetCode:135. 分发糖果

    LeetCode:135. 分发糖果 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分 ...

  5. SpringBoot Kafka 整合集成 示例教程

    1.使用IDEA新建工程,创建工程 springboot-kafka-producer 工程pom.xml文件添加如下依赖: <!-- 添加 kafka 依赖 --> <depend ...

  6. WebClient 与HttpClient 的区别

    需要搜索下资料. -------------------------------------------------- 微软文档介绍,新的开发中推荐使用:HttpClient WebClient 文档 ...

  7. go常量的定义和枚举类型

    const a,b int = 1,2 const a,b     = 1,2 const ( a = "hello" b,c =3,4 ) 常量数值可作为各种类型使用 枚举类型的 ...

  8. kombu在redis中的键值名

    参考flower源码 取队列名,发送到求数量的函数中 queue_names = ControlHandler.get_active_queue_names() queues = yield brok ...

  9. 找到一些经验,关于使用thymeleaf时遇到的一些问题

    最近一直在使用spring boot,所以自然而然的使用了thymeleaf,但是我想说习惯了jsp之后使用thymeleaf真实觉得不顺手,在使用thymeleaf中也遇到了一些问题,在这里记录一下 ...

  10. LeetCode 308. Range Sum Query 2D - Mutable

    原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...