"Valley P2899" [Usaco08jan] cell phone network cell phone

Source: Internet
Author: User

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 and Output Sample input example # #:
51 35 24) 33 5
Sample # # of output:
2
Analysis

Inside the code is

Code

From the great god of slager_z.

#include <bits/stdc++.h>using namespacestd;#definell Long Long#defineMans 10050intn,f[man][4];//the security here means that the subtree with the x root is all safe.//The unsafe here means that the X-root subtree is safe except for X.//F[][0] Do not put the tower but safe//F[][1] does not put the tower and is not safe//F[][2] put the tower but safe//F[][3] The tower is still unsafe (certainly does not exist, so do not consider)structedge{intNext,to;} e[man<<2];inthead[man<<2],num=0; inlinevoidAddint  from,intTo ) {e[++num].next=head[ from];e[num].to=to;head[ from]=num;}voidTREEDP (intXintFA) {f[x][0]=f[x][1]=0; f[x][2]=1;intminn=12456789;//when F[][2], the initial value must have his own value    BOOLChanged=0;//record whether you want to change the value of f[x][0]     for(intI=head[x];i;i=E[i].next) {    intto=e[i].to; if(TO==FA)Continue;//prevent the re-search backTREEDP (to,x); Minn=min (minn,f[to][2]-f[to][0]);//preprocessing if a point from the tower to the tower, then his minimum cost is how muchf[x][0]+=min (f[to][0],f[to][2]);//first of all, add the smallest value, if not, then change the following;        if(f[to][2]<=f[to][0]) changed=1;//if the value of the tower is less 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];//Its own f[][1] means that the tower is not safe, then can only call it so continue to go wrong, because it is the current point of security is f[][0] and f[][2] responsibility, do not need f[][1] interferencef[x][2]+=min (f[to][0],min (f[to][1],f[to][2]));//since the tower is now in place and safe, it is safe to put the tree in its place, 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 in his sub-node in the cost of the lowest point in the tower, so as to ensure his safety    }intMain () {scanf ("%d",&N);  for(intI=1; i<n;i++)    {    intu,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; }

"Valley P2899" [Usaco08jan] cell phone network cell phone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.