Cell Phone Network
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 6012 |
|
Accepted: 2163 |
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 a was the first pasture in the Sequenc E 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.
Input
* Line 1: A single integer: N
* Lines 2. N: Each line specifies a pair of adjacent pastures with the space-separated integers: a and b
Output
* Line 1: A single integer indicating the minimum number of towers to install
Sample Input
51 35 24) 33 5
Sample Output
2
Source
Usaco January Gold It's really a sore egg. Understand the wrong test instructions. Thought it was covering the edge. Later saw the god Ben Blog just suddenly dawned ... or DP; Start with any node as the root DFS+DP (that is, the tree DP) 3 cases, (1) The node number x is a tower, to ensure that X and its descendants are covered by the base tower. (2) The node numbered X does not build towers, ensuring that X and its descendants are covered by the base tower. (3) The node numbered X does not build a tower, ensuring that X is not overwritten, but the descendants of X are covered. So much, it's easy to write the transfer equation.
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN =10005;structedge{int from, to, next; Edge () {} Edge (intUintV): from(U), to (v) {}}EDGES[MAXN<<1];intFIRST[MAXN], tots;intf[maxn][3], N;voidAddintAintb) {Edges[tots]=Edge (A, b); Edges[tots].next=First[a]; First[a]= tots++;}voidDfsintx) {memset (f[x],0,sizeof(F[x])); BOOLFlag =true; intMint =0x7fffffff; for(inti = first[x]; I! =-1; i =Edges[i].next) {Edge&e =Edges[i]; if(f[e.to][0] == -1) {DFS (e.to); f[x][0] + = min (f[e.to][0], Min (f[e.to][1], f[e.to][2])); f[x][2] + = min (f[e.to][0], f[e.to][1]); if(f[e.to][0] <= f[e.to][1]) {flag=false; f[x][1] + = f[e.to][0]; } Else{Mint= min (Mint, f[e.to][0]-f[e.to][1]); f[x][1] + = f[e.to][1]; } } } if(flag) f[x][1] +=Mint; f[x][0]++;}intMain () {scanf ("%d", &N); Tots=0; memset (First,-1,sizeof(first)); for(inti =1; I < n; ++i) {intu, v; scanf ("%d%d", &u, &v); Add (U, v); Add (V, u); } memset (F,-1,sizeof(f)); DFS (1); printf ("%d\n", Min (f[1][0], f[1][1])); return 0;}
"POJ 3659" Cell Phone Network