標籤:相互 last article min rmi mini content 節點 ++
P2899 [USACO08JAN]行動電話通訊Cell Phone Network題目描述
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.
John想讓他的所有牛用上手機以便相互交流(也是醉了。。。),他需要建立幾座訊號塔在N塊草地中。已知與訊號塔相鄰的草地能收到訊號。給你N-1個草地(A,B)的相鄰關係,問:最少需要建多少個訊號塔能實現所有草地都有訊號。
輸入輸出格式輸入格式:
輸出格式:
- Line 1: A single integer indicating the minimum number of towers to install
輸入輸出範例輸入範例#1:
51 35 24 33 5
輸出範例#1:
2
/* f[i][0]表示節點i不選但被覆蓋 f[i][1]表示節點i選 f[i][2]表示節點i不選也不被覆蓋*/#include<iostream>#include<cstdio>#define maxn 10010#define INF 2000000000using namespace std;int f[maxn][3],n,num,head[maxn];struct node{ int to,pre;}e[maxn*2];void Insert(int from,int to){ e[++num].to=to; e[num].pre=head[from]; head[from]=num;}void dfs(int now,int father){ int f0=INF,f2=0,f1=0,w=0,s=0; for(int i=head[now];i;i=e[i].pre){ int to=e[i].to; if(to==father)continue; dfs(to,now); s=min(f[to][0],f[to][1]); w+=s; f0=min(f0,f[to][1]-s); f1+=min(f[to][1],min(f[to][0],f[to][2])); if(f2<INF)f2+=f[to][0]; } f[now][1]=f1+1;f[now][2]=f2; if(f0==INF)f[now][0]=INF; else f[now][0]=w+f0;}int main(){ int x,y; scanf("%d",&n); for(int i=1;i<n;i++){ scanf("%d%d",&x,&y); Insert(x,y); Insert(y,x); } dfs(1,0); int ans=min(f[1][1],f[1][0]); cout<<ans;}
洛穀P2899 [USACO08JAN]行動電話通訊Cell Phone Network