牛客假日团队赛8:H.Cell Phone Network(最小支配集)
鏈接:https://ac.nowcoder.com/acm/contest/1069/A
來源:牛客網(wǎng)
時(shí)間限制: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
輸入
復(fù)制
輸出
復(fù)制
說明
The towers can be placed at pastures 2 and 3 or pastures 3 and 5.
一.題意:
給出直接相連的幾條邊,選擇最少的點(diǎn),使所有點(diǎn)都被覆蓋(點(diǎn)能被覆蓋的條件是:與所選點(diǎn) 有邊直接相連)
這題實(shí)質(zhì)上是求最小支配集。
二.最小支配集的概念:
支配集的定義如下:給定無向圖G =(V , E),其中V是點(diǎn)集, E是邊集, 稱V的一個(gè)子集S稱為支配集當(dāng)且僅當(dāng)對于V-S中任何一個(gè)點(diǎn)v, 都有S中的某個(gè)點(diǎn)u, 使得(u, v) ∈E。
特別地,最小的支配集S(即任意一個(gè)比S小的集合都不可能是支配集)叫做最小支配集
三 .求解最小支配集的方法:
1.類似Tarjan且基于dfs的貪心
2.樹形dp
四.具體操作
解法一:
貪心:
1.選擇其中一個(gè)點(diǎn)作為root,定義father數(shù)組,并標(biāo)記root的父親為自己。
2.定義一個(gè)dfsn數(shù)組,用于dfs時(shí)記錄每個(gè)點(diǎn)dfs序
3.dfs,dfs得到dfs序和每個(gè)點(diǎn)的father
4.定義一個(gè)s數(shù)組,作為支配集容器,
重新初始化vis,vis用于標(biāo)記是否屬于支配集和是否與支配集中的點(diǎn)相連
記錄答案:按照dfs序的逆序依次選點(diǎn)
對于一個(gè)即不屬于支配集也不與支配集中的點(diǎn)相連的點(diǎn)來說
,如果他的父節(jié)點(diǎn)不屬于支配集,將其父節(jié)點(diǎn)加入到支配集,ans++,標(biāo)記當(dāng)前節(jié)點(diǎn)父節(jié)點(diǎn)屬于支配集s
標(biāo)記當(dāng)前結(jié)點(diǎn)、當(dāng)前結(jié)點(diǎn)的父節(jié)點(diǎn)(屬于支配集)、當(dāng)前結(jié)點(diǎn)的父節(jié)點(diǎn)的父節(jié)點(diǎn)(與支配集中的點(diǎn)相連)。
解法二:
dp:(我表示并不會)
請參考下面來自百度百科的代碼:
總結(jié)
以上是生活随笔為你收集整理的牛客假日团队赛8:H.Cell Phone Network(最小支配集)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客假日团队赛8:K.Cow Conte
- 下一篇: 牛客假日团队赛8:F.Telephone