codeforces1187E
题目链接:http://codeforces.com/problemset/problem/1187/E
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.
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.
Print one integer — the maximum number of points you gain if you will play optimally.
9
1 2
2 3
2 5
2 6
1 4
4 9
9 7
9 8
36
5
1 2
1 3
2 4
2 5
14
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的更多相关文章
随机推荐
- 两通道实信号使用一个FFT同时计算算法
前言 在工程的实际应用场景中,往往是需要最省资源量.而DSP资源和BRAM资源对FPGA来说弥足珍贵. 对于同时存在多个通道的实信号需要做FFT而言,常规做法是每个通道用一个FFT IP,FFT IP ...
- C++ 解决文件重复包含
// 如果zzz没有定义就定义zzz,然后执行下面的代码,定义了就不执行 #if !defined(zzz) #define zzz struct PPoint { int x; int y; }; ...
- input 框自动检测输入是否为数字
最近做一个公众号,我这个菜鸡不得不学习很多东西,谁让老师要我一个人做这个项目呢? 好,进入正题,因为菜,所以很菜,但是百度很厉害啊,谁让我不好意思问老师,而且我也觉得问这么小的问题,太难以启齿.. 因 ...
- 【问题】yum安装软件报错ERROR:dbus.proxies
转自:Yum安装报错:ERROR:dbus.proxies 环境: [红帽企业Linux.6.4.服务器版].rhel-server-6.4-x86_64-dvd(ED2000.COM).iso 安装 ...
- Java学习笔记——第2篇
Java程序的基本规则 Java程序的组织形式:纯粹的面向对象的程序设计语言,所以Java程序必须以类的形式存在,类是Java程序的最小程序单位,Java程序不允许可执行语句.方法等成分独立存在,所有 ...
- 剑指Offer(二十九):最小的K个数
剑指Offer(二十九):最小的K个数 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/baid ...
- Robot Framework--接口测试环境搭建
1.安装requests库 (robotframework-requests依赖这个request http client) 执行pip install requests 2. 安装requestLi ...
- JavaScript异步学习笔记——主线程和任务队列
任务队列 单线程就意味着,所有任务需要排队,前一个任务结束,才会执行后一个任务.如果前一个任务耗时很长,后一个任务就不得不一直等着. 同步任务指的是,在主线程上排队执行的任务,只有前一个任务执行完毕, ...
- 前端处理:elementUI 表格索引代表第几条数据
分析:表格结合分页 知识点:1.表格的自定义索引(索引以当前行的行号作为参数)number, Function(index)该属性传入数字时,将作为索引的起始值.也可以传入一个方法,它提供当前行的行号 ...
- [Ynoi2017]由乃的OJ
题意 由乃正在做她的OJ.现在她在处理OJ上的用户排名问题.OJ上注册了n个用户,编号为1-",一开始他们按照编号 排名.由乃会按照心情对这些用户做以下四种操作,修改用户的排名和编号:然而由 ...