codeforces 1029E. Tree with Small Distances

题意:   给你一个n   表示有n个点组成的一棵树,接下来输入的是n-1条边 。1是这棵树的根节点。现在要求每个节点到根节点的距离要小于等于2。问:最少要连几根线才能满足要求。

 

思路 :贪心一下 找到距离不符合的叶子节点然后 然后把叶子节点的父亲连接到根节点。再去更新与这个父亲节点相连的点,这时会出现新的叶子节点。。才开始写的时候没写好出现新的叶子节点没找好wa了30次。。。。。

题面:

You are given an undirected tree consisting of nn vertices. An undirected tree is a connected undirected graph with n−1n−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 11 to any other vertex is at most 22. Note that you are not allowed to add loops and multiple edges.

Input

The first line contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of vertices in the tree.

The following n−1n−1 lines contain edges: edge ii is given as a pair of vertices ui,viui,vi (1≤ui,vi≤n1≤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.

Output

Print a single integer — the minimum number of edges you have to add in order to make the shortest distance from the vertex 11 to any other vertex at most 22. Note that you are not allowed to add loops and multiple edges.

Examples

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

Note

The tree corresponding to the first example:The answer is 22, 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 00.

The tree corresponding to the third example:The answer is 11, only one possible way to reach it is to add the edge (1,3)(1,3).

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdlib>
#include <cmath>
using namespace std;
const int MANX = 2e5+50;
vector<int>mp[MANX];
int n,fa[MANX],son[MANX],sum,len[MANX],dd[MANX];///父亲,儿子数量,到一的距离,bfs
///bool di[MANX];
void dfs(int x,int ll)
{
    for(int i=0;i<mp[x].size();i++)
    {
        if(fa[mp[x][i]]==0 )
        {
            fa[ mp[x][i] ]=x;
            len[ mp[x][i] ]=ll+1;
            dfs( mp[x][i] ,ll+1);
        }
    }
}
void f(int x)
{
    for(int i=0;i<mp[x].size();i++)
    {
        if(mp[x][i]==fa[x])
            continue ;
        len[mp[x][i]]=min(len[mp[x][i]],len[x]+1 );
        f(mp[x][i]);
        len[x]=min(len[x],len[mp[x][i]]+1);
    }
    if(len[x]>2)
    {
        len[x]=2;
        len[fa[x]]=1;
        sum++;
    }

}
void fidson()
{
    son[1]=mp[1].size();
    for(int i=2;i<=n;i++)
        son[i]=mp[i].size()-1;
}
int main()
{
    scanf("%d",&n);
    int a ,b;
    sum=0;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&a,&b);
        mp[a].push_back(b);
        mp[b].push_back(a);
    }
    fidson();
    memset(fa,0,sizeof(fa));
   /// memset(di,0,sizeof(di));
    fa[1]=1,len[1]=0,dfs(1,0);
    f(1);
    printf("%d\n",sum);
    return 0;
}

友情数据 :

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

2


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

2


12
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
8 10
8 11
11 12

3

52
1 2
1 3
1 4
1 5
2 6
2 7
3 8
3 9
3 10
5 11
6 12
6 13
7 14
8 15
8 16
8 17
10 18
10 19
11 20
12 21
13 22
13 23
16 24
16 25
16 26
19 27
19 28
20 29
20 30
23 31
23 32
23 33
25 34
26 35
26 36
28 37
29 38
29 39
29 40
30 41
30 42
41 43
41 44
42 45
42 46
43 47
43 48
44 49
44 50
46 51
51 52


16

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值