codeforces 1029E(贪心)

Description

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.

Input

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.

Output

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.

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,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 <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <vector>
#define qcin ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 50;
const double eps = 1e-6;
const long long MOD = 1000000007;

int dis[MAXN], fa[MAXN];
bool vis[MAXN];
vector<int> gra[MAXN<<1];
vector<int> :: iterator it;

struct node
{
    int n, d;
};
priority_queue<node> Q;

bool operator < (node a, node b)
{
    return a.d < b.d;
}

void dfs(int u, int f)
{
    for(int i = 0; i < gra[u].size(); i++)
    {
        int opt = gra[u][i];
        if(opt == f) continue;
        fa[opt] = u, dis[opt] = dis[u] + 1;
        dfs(opt, u);
    }
}

int main()
{
    memset(vis, false, sizeof(vis));
    memset(dis, 0, sizeof(dis));
    int n;
    scanf("%d", &n);
    for(int i = 1; i < n; i++)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        gra[u].push_back(v);
        gra[v].push_back(u);
    }
    dfs(1, 0);
    int ans = 0;
    for(int i = 1; i <= n; i++)
        if(dis[i] > 2)
        {
            node tm;
            tm.n = i, tm.d = dis[i];
            Q.push(tm);
        }
    while(!Q.empty())
    {
        int u = Q.top().n;
        Q.pop();
        if(vis[u]) continue;
        vis[fa[u]] = true, ans++;
        for(it = gra[fa[u]].begin(); it != gra[fa[u]].end(); it++)
            vis[*it] = true;
    }
    printf("%d\n", ans);
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值