[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 1:
51 35 24) 33 5
Sample # # of output:
2
Exercises
A classic tree DP with a status of F[i][3] indicates that this node is not selected and cannot be overwritten, this node is selected, this node is not selected but can be covered in three cases, then the motion regulation equation is obvious. However, the following is an AC code:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>using namespacestd;intn,m;intf[10001][3];structstudent{intNext,to;} edge[30001];inthead[10001],size;voidPutin (int from,intTo ) {Size++; Edge[size].next=head[ from]; Edge[size].to=to ; head[ from]=size;}voidDfsintKintfather) { intf0=2000000000, f2=0, f1=0, w=0, i,s=0; for(i=head[k];i!=-1; i=Edge[i].next) { inty=edge[i].to; if(Y==father)Continue; DFS (Y,K); S=min (f[y][1],f[y][0]); W+=s; if(f[y][1]-S<F0) f0=f[y][1]-s; F1+=min (f[y][1],min (f[y][0],f[y][2])); if(f2<2000000000) f2+=f[y][0]; } f[k][1]=f1+1; f[k][2]=F2; if(f0==2000000000) f[k][0]=2000000000; Elsef[k][0]=w+F0;}intMain () {intI,j, from, to; CIN>>N; for(i=1; i<=n;i++) head[i]=-1; for(i=1; i<n;i++) {scanf ("%d%d",& from,&to ); Putin ( from, to); Putin (to, from); } DFS (1,0); cout<<min (f[1][0],f[1][1]); return 0;}
[Usaco08jan] Cellular cell phone network