題目大意
- 給出一棵樹,每個守衛可以瞭望相鄰的點;
- 問,最少需要多少個守衛,可以瞭望全圖。
題目分析
- 瞭望:其實是希望用最少的點,覆蓋長度範圍爲1的圖。
- 所以是經典的“最小點覆蓋”問題,在樹上也叫“最大獨立集”問題;
- 因爲是板子,所以肯定很簡單,直接上代碼,相信應該問題不大。
解題流程
參考代碼
#include<bits/stdc++.h>
using namespace std;
const int N=3000007;
struct node{
int nex,to;
}e[N];
int n,f[1501][2];
int las[N],cnt;
void add(int x,int y){
cnt++;
e[cnt].nex=las[x];
e[cnt].to=y;
las[x]=cnt;
}
void dfs(int x,int fa){
f[x][1]=1;
f[x][0]=0;
for(int i=las[x];i;i=e[i].nex){
int y=e[i].to;
if(y==fa) continue;
dfs(y,x);
f[x][0]+=f[y][1];
f[x][1]+=min(f[y][1],f[y][0]);
}
}
int main(){
cin >> n;
while(n--){
int x,k,y;
cin >> x >> k;
while(k--){
cin>>y; add(x,y); add(y,x);
}
}
dfs(0,-1);
cout << min(f[0][1],f[0][0]);
return 0;
}