PT07X - Vertex Cover


You are given an unweighted, undirected tree. Write a program to find a vertex set of minimum size in this tree such that each edge has as least one of its end-points in that set.

Input

The first line of the input file contains one integer N --- number of nodes in the tree (0 < N <= 100000). Next N-1 lines contain N-1 edges of that tree --- Each line contains a pair (u, v) means there is an edge between node u and node v (1 <= u,v <= N).

Output

Print number of nodes in the satisfied vertex set on one line.

Example 1

Input:
3
1 2
1 3

Output:
1

Explanation:
The set can be {1}

Example 2

Input:
3
1 2
2 3

Output:
1

Explanation:
The set can be {2}

hide comments
osmanay2: 2020-02-10 23:07:22

Test your solution with this case:

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

Answer is 4.

scolar_fuad: 2019-12-01 05:26:35

Minimum vertex cover dp problem
AC in first go
Hurrah

mzuenni: 2019-11-29 12:32:36

just solve it greedy: take the neighbour of a leaf and remove it.

sajalagrawal14: 2019-11-09 05:53:13

nice question : dp on tree , pretty standard problem

saurav_paul: 2019-09-09 07:14:25

Accepted in first go. Dp + Dfs

kumar18tushar: 2019-05-31 09:34:13

dp+dfs worth solving !!!

deepak097: 2019-03-26 15:53:54

Indeed a good one :)

paranoidninja: 2019-02-11 20:05:36

Good problem to start off DP on trees

ankitraj1996: 2019-01-26 03:02:31

Why can not we use Bipartite checking? If anyone can explain, it would be helpful, I got a WA with that!
Well sorry I figured it out, it won't work!!

Last edit: 2019-01-26 03:57:04
nitish235: 2019-01-25 10:35:11

if you are applying greedy approach and getting WA then think about this case......
13
1 2
1 3
1 4
1 5
1 6
1 7
2 8
2 9
2 10
2 11
2 12
2 13

ans: 2
set={1,2}


Added by:Thanh-Vy Hua
Date:2007-03-28
Time limit:1s-3s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Co-author Amber