CF1187E.Tree Painting

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 11 and 44 are painted black already. If you choose the vertex 22, you will gain 44 points for the connected component consisting of vertices 2,3,52, 3, 5 and 66. If you choose the vertex 99, you will gain 33 points for the connected component consisting of vertices 7,87, 8 and 99.

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

给你一棵树(一个无向、连通且无环的图),该树包含 nn 个顶点。你将在该树上进行一场游戏。

初始时,所有顶点均为白色。在游戏的第一轮中,你选择一个顶点并将其涂成黑色。随后,在每一轮中,你需选择一个与任意黑色顶点相邻(即通过一条边直接相连)的白色顶点,并将其涂成黑色。

每次当你选择一个顶点(包括第一轮)时,你将获得等于包含该顶点的、仅由白色顶点构成的连通分量的大小的分数。当所有顶点均被涂成黑色时,游戏结束。

我们来看如下示例:

顶点 11 和 44 已被涂成黑色。若你选择顶点 22,则你将获得 44 分,对应由顶点 2,3,52, 3, 5 和 66 构成的白色连通分量;若你选择顶点 99,则你将获得 33 分,对应由顶点 7,87, 8 和 99 构成的白色连通分量。

你的任务是最大化所获得的总分数。

输入格式

The first line contains an integer nn — the number of vertices in the tree (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5).

Each of the next n−1n - 1 lines describes an edge of the tree. Edge ii is denoted by two integers uiu_i and viv_i, the indices of vertices it connects (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i).

It is guaranteed that the given edges form a tree.

第一行包含一个整数 nn —— 树中顶点的数量(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)。

接下来的 n−1n - 1 行每行描述树的一条边。第 ii 条边由两个整数 uiu_i 和 viv_i 表示,即该边所连接的两个顶点的编号(1≤ui,vi≤n1 \le u_i, v_i \le n,且 ui≠viu_i \ne v_i)。

保证所给的边构成一棵树。

输出格式

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.

第一个示例树如题目陈述中所示。

输入解题思路,AI测评打分。不知道怎么写?

首页