您的位置:首页 > 其它

2014多校5(1002)hdu4912(贪心+LCA)

2014-08-06 16:53 302 查看


Paths on the tree

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 424 Accepted Submission(s): 127



Problem Description

bobo has a tree, whose vertices are conveniently labeled by 1,2,…,n.

There are m paths on the tree. bobo would like to pick some paths while any two paths do not share common vertices.

Find the maximum number of paths bobo can pick.

Input

The input consists of several tests. For each tests:

The first line contains n,m (1≤n,m≤105). Each of the following (n - 1) lines contain 2 integers ai,bi denoting an edge between vertices ai and bi (1≤ai,bi≤n). Each of the following
m lines contain 2 integers ui,vi denoting a path between vertices ui and vi (1≤ui,vi≤n).

Output

For each tests:

A single integer, the maximum number of paths.

Sample Input

3 2
1 2
1 3
1 2
1 3
7 3
1 2
1 3
2 4
2 5
3 6
3 7
2 3
4 5
6 7


Sample Output

1
2


题意:给出了m条路径,需要你选择尽量多的路径使得两两之间没有公共点

思路:以任意一点为根dfs树,然后优先选择路径中最小深度的节点(可以用LCA求)最深的路径,每次选完一条路径以后将该路径中以最小深度节点为根的子树标记(之后不能再选),然后就没了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: