CF1646D.Weight the Tree

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a tree of nn vertices numbered from 11 to nn. A tree is a connected undirected graph without cycles.

For each i=1,2,…,ni=1,2, \ldots, n, let wiw_i be the weight of the ii-th vertex. A vertex is called good if its weight is equal to the sum of the weights of all its neighbors.

Initially, the weights of all nodes are unassigned. Assign positive integer weights to each vertex of the tree, such that the number of good vertices in the tree is maximized. If there are multiple ways to do it, you have to find one that minimizes the sum of weights of all vertices in the tree.

给你一棵包含 nn 个顶点的树,顶点编号为 11 到 nn。树是一种无环的连通无向图。

对每个 i=1,2,…,ni = 1, 2, \ldots, n,令 wiw_i 表示第 ii 个顶点的权值。若一个顶点的权值等于其所有邻接顶点的权值之和,则称该顶点为好顶点。

初始时,所有顶点的权值均未赋值。请为树的每个顶点赋予正整数权值,使得树中好顶点的数量最大化。若存在多种方案能达到最大好顶点数量,则需在其中选出一种使得所有顶点权值之和最小的方案。

输入格式

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

Then, n−1n−1 lines follow. Each of them contains two integers uu and vv (1≤u,v≤n1\le u,v\le n) denoting an edge between vertices uu and vv. It is guaranteed that the edges form a tree.

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

接下来是 n−1n-1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1\le u,v\le n),表示顶点 uu 与顶点 vv 之间的一条边。保证这些边构成一棵树。

输出格式

In the first line print two integers — the maximum number of good vertices and the minimum possible sum of weights for that maximum.

In the second line print nn integers w1,w2,…,wnw_1, w_2, \ldots, w_n (1≤wi≤1091\le w_i\le 10^9) — the corresponding weight assigned to each vertex. It can be proven that there exists an optimal solution satisfying these constraints.

If there are multiple optimal solutions, you may print any.

第一行输出两个整数——好顶点的最大数量,以及达到该最大数量时权重和的最小可能值。

第二行输出 nn 个整数 w1,w2,…,wnw_1, w_2, \ldots, w_n(1≤wi≤1091\le w_i\le 10^9)——分别赋给每个顶点的对应权重。可以证明存在满足这些约束的最优解。

若存在多个最优解,输出任意一个即可。

输入输出样例

  • 输入#1

    4
    1 2
    2 3
    2 4

    输出#1

    3 4
    1 1 1 1
  • 输入#2

    3
    1 2
    1 3

    输出#2

    2 3
    1 1 1
  • 输入#3

    2
    1 2

    输出#3

    2 2
    1 1
  • 输入#4

    9
    3 4
    7 6
    2 1
    8 3
    5 6
    1 8
    8 6
    9 6

    输出#4

    6 11
    1 1 1 1 1 1 1 3 1

说明/提示

This is the tree for the first test case:

In this case, if you assign a weight of 11 to each vertex, then the good vertices (which are painted black) are 11, 33 and 44. It impossible to assign weights so that all vertices are good vertices. The minimum sum of weights in this case is 1+1+1+1=41+1+1+1=4, and it is impossible to have a lower sum because the weights have to be positive integers.

This is the tree for the second test case:

In this case, if you assign a weight of 11 to each vertex, then the good vertices (which are painted black) are 22 and 33. It can be proven that this is an optimal assignment.

这是第一个测试用例的树:

本例中,若为每个顶点赋予权值 11,则好顶点(涂成黑色的顶点)为 11、33 和 44。不可能为顶点赋予权值使得所有顶点均为好顶点。本例中权值之和的最小值为 1+1+1+1=41+1+1+1=4;由于权值必须为正整数,因此无法得到更小的和。

这是第二个测试用例的树:

本例中,若为每个顶点赋予权值 11,则好顶点(涂成黑色的顶点)为 22 和 33。可以证明该赋权方案是最优的。

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

首页