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

来源:互联网 发布:免费手机记账软件 编辑:程序博客网 时间:2024/05/17 23:34

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 21 21 31 21 37 31 21 32 42 53 63 72 34 56 7
 

Sample Output
12

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

思路:以任意一点为根dfs树,然后优先选择路径中最小深度的节点(可以用LCA求)最深的路径,每次选完一条路径以后将该路径中以最小深度节点为根的子树标记(之后不能再选),然后就没了。

0 0
原创粉丝点击