HDU 6023 ping ping ping

/ 0评 / 0

Time Limit: 2000/1000 MS (Java/Others) LCA + 贪心
The structure of the computer room in Northeastern University is pretty miraculous. There are $ n$ servers, some servers connect to the gateway whose $ IP$ address is $ 0$ directly. All servers are connected with each other by $ n$ netting twines. It is said that this structure is favorable for maintaining physical problem of servers.
But because of an unexpected rainstorm, the computer room was destroyed by a terrible thunderclap!
Our maintainer Bittersweet found that many servers were not able to be visited, so he hurried to the computer room to lookup the reason. After several hours, Bittersweet realized that some net gape of servers were broken by thunderclap. However, there were too many servers to find out all the broken net gapes quickly. So he came up with an idea to assess the damaged condition roughly. Bittersweet decided to turn on some servers and ping other servers randomly, then record the unsuccessful pairs of servers.
Now he need a program to analyze the record to confirm what is the minimum number of servers whose net gape was destroyed by thunderclap. Can you help him to complete this work?

Input

There are at most $ 20$ test cases.
In each test case, the first line is an integer $ n (3≤n≤10^4)$, denoting the number of servers. The $ IP$ address of these servers is $ 1…n$.
Then follows $ n$ lines, each line contains two integers $ u$ and $ v$ $ (0≤u,v≤n)$, denoting that the server whose $ IP$ address $ is$ u is connected with the server whose $ IP$ address is$ v$ by netting twine initially.
After those, there is one line contains only an integer $ p (p≤50000)$, denoting the number that Bittersweet uses ping.
Then follows p lines, each line contains two integers $ U$ and $ V$ , denoting when using server $ U$ to ping server $ V$, it returned unsuccessful.

Output

A single integer $ x$ in a line, denoting at least $ x$ servers whose net gape were broken.

Sample Input

Sample Output

Source

2017 ACM/ICPC Asia Regional Shenyang Online

题解:

本题的贪心策略 :

本题不是单纯地删除$lca(u,v)$,例如对于一条链上的$ (root,a1,a2,a3)$ $ lca(root, a1)=root$ $ lca(a1,a3)=a1 $;那么不是删去$ root$ 而是删去$ a1$即可断开两条通信恰好是lca深度较大的点应该删去,所以原本深度最小的$lca$没有必要删去

解决办法:

问题在于删去的点一定是$ lc$a,那么每次删去$ lca$后暴力$ dfs$,把删去$ lca$的子树的所有未被标记的点都标记一遍,这样标记算法$ O(N)$,N为子树节点数,然后按照点对$ lca$的深度从深到浅处理,如果当前点对两个有一个已经被标记,说明两点已经断开了,没有必要再次删除,否则断开,标记$ lca$子树。如此贪心即可。

Code

发表评论

电子邮件地址不会被公开。 必填项已用*标注