牛客假日团队赛8:H.Cell Phone Network(最小支配集)
鏈接:https://ac.nowcoder.com/acm/contest/1069/A
來源:??途W
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
題目描述
Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1…N) so they can all communicate.
Exactly N-1 pairs of pastures are adjacent, and for any two pastures A and B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B) there is a sequence of adjacent pastures such that A is the first pasture in the sequence and B is the last. Farmer John can only place cell phone towers in the pastures, and each tower has enough range to provide service to the pasture it is on and all pastures adjacent to the pasture with the cell tower.
Help him determine the minimum number of towers he must install to provide cell phone service to each pasture.
輸入描述:
- Line 1: A single integer: N
- Lines 2…N: Each line specifies a pair of adjacent pastures with two space-separated integers: A and B
輸出描述: - Line 1: A single integer indicating the minimum number of towers to install
示例1
輸入
復制
輸出
復制
說明
The towers can be placed at pastures 2 and 3 or pastures 3 and 5.
一.題意:
給出直接相連的幾條邊,選擇最少的點,使所有點都被覆蓋(點能被覆蓋的條件是:與所選點 有邊直接相連)
這題實質上是求最小支配集。
二.最小支配集的概念:
支配集的定義如下:給定無向圖G =(V , E),其中V是點集, E是邊集, 稱V的一個子集S稱為支配集當且僅當對于V-S中任何一個點v, 都有S中的某個點u, 使得(u, v) ∈E。
特別地,最小的支配集S(即任意一個比S小的集合都不可能是支配集)叫做最小支配集
三 .求解最小支配集的方法:
1.類似Tarjan且基于dfs的貪心
2.樹形dp
四.具體操作
解法一:
貪心:
1.選擇其中一個點作為root,定義father數組,并標記root的父親為自己。
2.定義一個dfsn數組,用于dfs時記錄每個點dfs序
3.dfs,dfs得到dfs序和每個點的father
4.定義一個s數組,作為支配集容器,
重新初始化vis,vis用于標記是否屬于支配集和是否與支配集中的點相連
記錄答案:按照dfs序的逆序依次選點
對于一個即不屬于支配集也不與支配集中的點相連的點來說
,如果他的父節點不屬于支配集,將其父節點加入到支配集,ans++,標記當前節點父節點屬于支配集s
標記當前結點、當前結點的父節點(屬于支配集)、當前結點的父節點的父節點(與支配集中的點相連)。
解法二:
dp:(我表示并不會)
請參考下面來自百度百科的代碼:
總結
以上是生活随笔為你收集整理的牛客假日团队赛8:H.Cell Phone Network(最小支配集)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客假日团队赛8:K.Cow Conte
- 下一篇: 牛客假日团队赛8:F.Telephone