A12711 | Tree Painting
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a tree (an undirected connected acyclic graph) consisting of $n$ 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.
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 $n$ — the number of vertices in the tree ( $2 \le n \le 2 \cdot 10^5$ ).
Each of the next $n - 1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$ , the indices of vertices it connects ( $1 \le u_i, v_i \le n$ , $u_i \ne v_i$ ).
It is guaranteed that the given edges form a tree.
Each of the next $n - 1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i$ , the indices of vertices it connects ( $1 \le u_i, v_i \le n$ , $u_i \ne v_i$ ).
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.
输入输出样例
输入 #1
9 1 2 2 3 2 5 2 6 1 4 4 9 9 7 9 8
输出 #1
36
输入 #2
5 1 2 1 3 2 4 2 5
输出 #2
14
The first example tree is shown in the problem statement.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted