题目
题意翻译
题目描述
给定一棵树。要求往树中加入一些边使得从1到其他节点的距离至多是2 。 输出加入边的最小数量。(边全部都是无向的)
输入格式
第一行一个整数n,表示树中的节点个数。 接下来n−1行,每行两个整数x,y,表示x,y之间有一条连边。
输出格式
输出一个整数,表示加入边的最小数量。
数据范围
对于30%的数据n≤10。对于70%的数据n≤100。对于100%的数据n≤105。
题目描述
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 \le n \le 2 \cdot 10^5 2≤n≤2⋅10
5
) — the number of vertices in the tree.
The following n - 1 n−1 lines contain edges: edge i i is given as a pair of vertices u_i, v_i u
i
,v
i
( 1 \le u_i, v_i \le n 1≤u
i
,v
i
≤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.
输入输出样例
输入样例#1:
7
1 2
2 3
2 4
4 5
4 6
5 7
输出样例#1:
2
输入样例#2:
7
1 2
1 3
2 4
2 5
3 6
1 7
输出样例#2:
0
输入样例#3:
7
1 2
2 3
3 4
3 5
3 6
3 7
输出样例#3:
1
说明
The tree corresponding to the first example: The answer is 2 2 , some of the possible answers are the following: [(1, 5), (1, 6)] [(1,5),(1,6)] , [(1, 4), (1, 7)] [(1,4),(1,7)] , [(1, 6), (1, 7)] [(1,6),(1,7)] .
The tree corresponding to the second example: The answer is 0 0 .
The tree corresponding to the third example: The answer is 1 1 , only one possible way to reach it is to add the edge (1, 3) (1,3) .
思路
做法:
每次找出离根节点最远的点,然后由根节点向这个点的父节点连边,一直连到所有点都能被覆盖即可,这样构造出的一定是一个可行的最优解
证明:
首先,由于所有点都要求被覆盖,自然离根节点最远的点也不例外
那么,如果想覆盖上离根节点最远的点,只会有两种覆盖方法:一种是将根节点与这个点本身相连,另一种是将根节点与这个点的父节点相连
不难发现,将根节点与这个点的父节点相连,这样的方案一定不会差
证明:假设这个父节点还有别的子节点,那么与父节点相连后这些子节点都能被覆盖,这样一定是更优的
而即使这个父节点没有别的子节点,他还有自己的父节点,这样连边也能减少根节点与他的父节点的距离,也会达到更好的效果
即使上面两点都没有起作用,至少这样还可以覆盖上最远的点,也并不会使代价增大,所以这样做是完全可行的。
这样就完事了
代码
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
struct Edge
{
int next;
int to;
}edge[400005];
bool used[200005];
int head[200005];
int f[200005];
int n;
int cnt=1;
void init()
{
memset(head,-1,sizeof(head));
cnt=1;
}
void add(int l,int r)
{
edge[cnt].next=head[l];
edge[cnt].to=r;
head[l]=cnt++;
}
struct node
{
int num;
int dep;
}p[200005];
void bfs(int rt)
{
queue <int> M;
p[rt].dep=0;
p[rt].num=rt;
M.push(rt);
while(!M.empty())
{
int u=M.front();
M.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(p[to].dep)
{
continue;
}
p[to].dep=p[u].dep+1;
p[to].num=to;
f[to]=u;
M.push(to);
if(p[to].dep<=2)
{
used[to]=1;
}
}
}
}
bool cmp(node a,node b)
{
return a.dep>b.dep;
}
int main()
{
scanf("%d",&n);
init();
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
bfs(1);
sort(p+1,p+n+1,cmp);
int ans=0;
for(int i=1;i<=n;i++)
{
if(used[p[i].num])
{
continue;
}
ans++;
int u=f[p[i].num];
used[u]=1;
for(int j=head[u];j!=-1;j=edge[j].next)
{
int to=edge[j].to;
used[to]=1;
}
}
printf("%d\n",ans);
return 0;
}