Title 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:
Line 1: A single integer:n
Lines 2..n:each Line Specifies a pair of adjacent pastures with a space-separated integers:a and B
Output format:
- Line 1: A single integer indicating the minimum number of towers to install
Input/Output sampleInput Sample # #:
51 35 24) 33 5
Sample # # of output:
2
#include <bits/stdc++.h>using namespace std; #define LL Long long#define man 10050int n,f[man][4];// The security here indicates that the subtree with the root x is all safe//The unsafe here means that X is the root of the subtree except X is safe//f[][0] do not put tower but safety//f[][1] do not put tower and unsafe//f[][2] put tower but safety//f[][3] The tower is not safe (certainly not exist, So do not consider) struct edge{int next,to;} E[man<<2];int head[man<<2],num=0;inline void Add (int from,int to) {E[++num].next=head[from];e[num].to=to ; head[from]=num;} void treedp (int x,int fa) {f[x][0]=f[x][1]=0;f[x][2]=1;int minn=12456789;//when f[][2], the initial value must have his own value bool changed=0;//kee If you want to change the value of f[x][0] for (int i=head[x];i;i=e[i].next) {int to=e[i].to; if (TO==FA) continue;//prevent re-search back TREEDP (to,x); Minn=min (minn,f[to][2]-f[to][0]);//pre-processing if a point from the tower to the tower, then his minimum cost is how much f[x][0]+=min (f[to][0],f[to][2]),//first to add the smallest value, If not, then change it below; if (f[to][2]<=f[to][0]) changed=1;//If the value of the tower is smaller than the value of the tower, then it must be in the tower, because the tower can also be more than a few places to look after (and can also see his father) f[x][1]+=f[to][0];//itself f[][1] means not to put the tower is not safe, then can only call it so continue wrong, because it is the current point to become safe is f[][0] and f[][2] The responsibility, do not need f[][1] interference f[x][2]+=min (F[to][0],min (f[to][1],f[to][2]));//Because the tower is currently in place and safe, then his sub-tree will be safe, so any state of the child node can be established} if (changed==0) f[x][0]+=minn;/ /If his sub-tree does not put the tower, then the current node is not safe, so it must be placed in his sub-node in the lowest cost point of the tower, so as to ensure his safety}int main () {scanf ("%d", &n); for (int i=1;i<n;i++) {int u,v; scanf ("%d%d", &u,&v); Add (u,v); add (V,u); } TREEDP (1,-1); printf ("%d\n", Min (f[1][0],f[1][2])); return 0; }
Rokua P2899 [Usaco08jan] Cellular cell phone network (tree-shaped motion)