HDU 6203 ping Ping ping (online multiplier lca+bit)

Source: Internet
Author: User

Description

A tree with a n+1 n + 1 n+1 node is given, which requires destroying as few points as possible to make the M m m-pair non-reciprocal.

Input

The first line of an integer n n n, followed by n n n rows of two integers per line u,v u, v u,v represents a tree edge, and then input an integer m M m, the last m m m line two integers per line u,v u, v u,v indicates the need to make u,v u, v u,v non-reciprocal (3≤n≤ 104,1≤p≤5⋅104) (3≤n≤10 4, 1≤p≤5⋅10 4) (3\le n\le 10^4,1\le p\le 5\cdot 10^4)

Output

The minimum number of points to be deleted from the output

Sample Input

4
1 0
4 2
2 0
3 2
2
1 3
2 1

Sample Output

1

Solution

In order to make the deleted point as effective as possible, for a point pair to delete the most influential point, namely its LCA L c a LCA, for each point pair to find its LCA L c a LCA, the query by point to the LCA L c a LCA depth descending row, first process LCA L c A LCA depth of the deepest point pair, because To handle other point pairs first, the point pair does not resolve the problem, but first solve the problem of the point to solve the problem of other points, by deleting the LCA L C A LCA, in order to keep the effect of deleting the point, the tree with the root of the subtree all marked plus one, so since, for the back point to u,v u, v u,v, If the u u u or v v mark is nonzero, the U u or V

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.