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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章