A14935 | Weight the Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a tree of $n$ vertices numbered from $1$ to $n$ . A tree is a connected undirected graph without cycles.
For each $i=1,2, \ldots, n$ , let $w_i$ be the weight of the $i$ -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.
For each $i=1,2, \ldots, n$ , let $w_i$ be the weight of the $i$ -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.
输入格式
The first line contains one integer $n$ ( $2\le n\le 2\cdot 10^5$ ) — the number of vertices in the tree.
Then, $n−1$ lines follow. Each of them contains two integers $u$ and $v$ ( $1\le u,v\le n$ ) denoting an edge between vertices $u$ and $v$ . It is guaranteed that the edges form a tree.
Then, $n−1$ lines follow. Each of them contains two integers $u$ and $v$ ( $1\le u,v\le n$ ) denoting an edge between vertices $u$ and $v$ . It is guaranteed that the edges form a tree.
输出格式
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 $n$ integers $w_1, w_2, \ldots, w_n$ ( $1\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.
In the second line print $n$ integers $w_1, w_2, \ldots, w_n$ ( $1\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.
输入输出样例
输入 #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 $1$ to each vertex, then the good vertices (which are painted black) are $1$ , $3$ and $4$ . 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=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 $1$ to each vertex, then the good vertices (which are painted black) are $2$ and $3$ . It can be proven that this is an optimal assignment.
 In this case, if you assign a weight of $1$ to each vertex, then the good vertices (which are painted black) are $1$ , $3$ and $4$ . 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=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 $1$ to each vertex, then the good vertices (which are painted black) are $2$ and $3$ . It can be proven that this is an optimal assignment.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted