P2899 [Usaco08jan] Cell phone network topic description
Farmer John had 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 He N (1≤n≤10,000) pastures (conveniently numbered 1..N) so They can all communicate.
Exactly N-1 pairs of pastures is adjacent, and for any of the pastures A and B (1≤a≤n; 1≤b≤n; A≠B) There is a sequence of adjacent pastures such, that's the first pasture in the sequence and B are the last. Farmer John can only place cell phone towers in the pastures with each tower have enough range to provide service to the PA Sture it is in 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 wants all his cows to use their phones to communicate with each other (and get drunk ...). ), he needs to build several signal towers in the N-Block Meadow. The grassland known to be adjacent to the signal tower receives a signal. To give you the N-1 of a meadow (a, B), Q: The minimum number of towers to be built can signal all meadows.
Input output Format input format:
Output format:
- Line 1: A single integer indicating the minimum number of towers to install
Input and Output Sample input example # #:
51 35 24) 33 5
Sample # # of output:
2
/*f[i][0] means node I is not selected but overwritten f[i][1] means node I select F[i][2] means node I is not selected or overwritten*/#include<iostream>#include<cstdio>#defineMAXN 10010#defineINF 2000000000using namespacestd;intf[maxn][3],N,NUM,HEAD[MAXN];structnode{intTo,pre;} E[MAXN*2];voidInsert (int from,intTo ) {e[++num].to=to ; E[num].pre=head[ from]; head[ from]=num;}voidDfsintNowintfather) { intF0=inf,f2=0, f1=0, w=0, s=0; for(intI=head[now];i;i=e[i].pre) { intto=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; Elsef[now][0]=w+F0;}intMain () {intx, y; scanf ("%d",&N); for(intI=1; i<n;i++) {scanf ("%d%d",&x,&y); Insert (x, y); Insert (Y,X); } DFS (1,0); intAns=min (f[1][1],f[1][0]); cout<<ans;}
Rokua P2899 [Usaco08jan] Cellular cell phone network