You are given an undirected tree consisting of n
n vertices. An undirected tree is a connected undirected graph with n−1
n−1 edges.
Your task is to add the minimum number of edges in such a way that the length of the shortest path from the vertex 1
1 to any other vertex is at most 2
2. Note that you are not allowed to add loops and multiple edges.
The first line contains one integer n
n (2≤n≤2×105)
(2≤n≤2×105) — the number of vertices in the tree.
The following n−1
n−1 lines contain edges: edge ii is given as a pair of vertices ui,viui,vi (1≤ui,vi≤n)
(1≤ui,vi≤n). It is guaranteed that the given edges form a tree. It is guaranteed that there are no loops and multiple edges in the given edges.
Print a single integer — the minimum number of edges you have to add in order to make the shortest distance from the vertex 1
1 to any other vertex at most 2
2. Note that you are not allowed to add loops and multiple edges.
Input
7
1 2
2 3
2 4
4 5
4 6
5 7
Output
2
Input
7
1 2
1 3
2 4
2 5
3 6
1 7
Output
0
Input
7
1 2
2 3
3 4
3 5
3 6
3 7
Output
1
The tree corresponding to the first example:
The answer is 22, some of the possible answers are the following: [(1,5),(1,6)],[(1,4),(1,7)],[(1,6),(1,7)]
[(1,5),(1,6)],[(1,4),(1,7)],[(1,6),(1,7)].
The tree corresponding to the second example:
The answer is 22, some of the possible answers are the following: [(1,5),(1,6)],[(1,4),(1,7)],[(1,6),(1,7)]
[(1,5),(1,6)],[(1,4),(1,7)],[(1,6),(1,7)].
The tree corresponding to the second example:
The answer is 11, only one possible way to reach it is to add the edge (1,3)(1,3).
已知一颗树,求最少要添加多少条边(加边后图可以不为树)使得所有结点到1号结点的最短路的边数<=2(所有边长度为1)
本来按样例想的是连接所有与1号点隔着两条边的点,然后不停更新各点距离,然后被魔鬼的test 5教做人。。
那么换个思路:我们添加的边连的点深度越深,那么该点在连接后减去的深度就越多,可是如果连接了最深的点,那么这条新加的边只能把最多两个点拉到深度为2以内,但是如果我们连接最深的点的父节点,那么所有与最深的点互为兄弟的点都会被拉到深度为2以内,而且其父节点的父节点也被拉到了深度2以内,显然,连接最深的点的父节点更划算,那么,我们就贪心地不断连接这样的父节点,然后这个父节点以及所有与其直接相连的点都被从树上去除(标记访问过了),直到树为空。
AC代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include