1596: [Usaco2008 Jan] Telephone networkDescription
Farmer John has decided to equip all his cows with mobile phones to encourage them to communicate with each other. For this reason, however, FJ had to select a radio tower in the N (1 <= n <= 10,000) meadow where the cows lived, to ensure that there were cell phone signals between any two grasslands. All n blocks of Grass press 1. N is numbered sequentially. Only N-1 pairs are adjacent to all grasslands, but to any two grasslands A and B (1 <= a <= n; 1 <= B <= N; A! = b), you can find a sequence of grasses ending with B at the beginning of a, and the adjacent numbers in the sequence represent the grasslands adjacent to each other. The radio tower can only be built on grass, and the service of a tower is in the meadow where it resides, and all the meadows adjacent to that meadow. Please help FJ calculate how many radio towers he will build in order to build a communication system that covers all the meadows.
Input
* Line 1th: 1 integers, N
* 2nd. N rows: 2 integers separated by spaces A, B, for two adjacent grassland numbers
Output
* Line 1th: output 1 integers, i.e. the number of radio communication towers established at least FJ
Sample Input5
1 3
5 2
4 3
3 5
Input Description:
Farmer John's Farm has 5 meadows: Meadows 1 and Meadows 3 adjacent, Meadows 5 and Meadows 2, meadows
4 and Grassland 3, grassland 3 and grassland 5 are also so. More image, the relationship between the grass is generally as follows:
(or other similar shapes)
4 2
| |
1--3--5
Sample Output 2
Output Description:
FJ can choose to build a communication tower on grassland 2 and grassland 3, or grassland 3 and grassland 5.
HINT
Check the wrong for a long day ....
Directly to the puzzle bar, or relatively bare tree-shaped DP
F[x][0] Indicates that this point is not selected
F[X][1] Indicates that this band selection
F[X][2] means this point will not be chosen, but at least one son will be chosen.
Transfer to look at the code bar.
#include <stdio.h>#include<iostream>using namespacestd;Const intn=10005;intn,i,x,y,f[n][3];inttot,head[n],next[n<<1],to[n<<1];voidAddintXinty) {Tot++; To[tot]=y; Next[tot]=Head[x]; HEAD[X]=tot;}voidDfsintXintpre) {f[x][0]=0; f[x][1]=1; f[x][2]=1000000; inti,y,s=0; for(i=head[x];i!=-1; i=Next[i])if(to[i]!=pre) DFS (TO[I],X); for(i=head[x];i!=-1; i=Next[i]) {y=To[i]; if(Y==pre)Continue; f[x][1]+=min (f[y][2],min (f[y][1],f[y][0])); f[x][0]+=min (f[y][2],f[y][1]); S+=min (f[y][2],f[y][1]); } for(i=head[x];i!=-1; i=Next[i]) {y=To[i]; if(Y==pre)Continue; f[x][2]=min (f[x][2],f[y][1]+s-min (f[y][2],f[y][1])); }}intMain () {scanf ("%d",&N); for(i=1; i<=n;i++) head[i]=-1; for(i=1; i<n;i++) {scanf ("%d%d",&x,&y); Add (x, y); Add (y,x); } DFS (1,0); cout<<min (f[1][2],f[1][1]); return 0;}
Bzoj 1596: [Usaco2008 Jan] Telephone network