Rokua P2899 [Usaco08jan] Cellular cell phone network

Source: Internet
Author: User

P2899 [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:

    • 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
/*f[i][0] means node I is not selected but overwritten f[i][1] means node I select F[i][2] means node I is not selected or overwritten*/#include<iostream>#include<cstdio>#defineMAXN 10010#defineINF 2000000000using namespacestd;intf[maxn][3],N,NUM,HEAD[MAXN];structnode{intTo,pre;} E[MAXN*2];voidInsert (int  from,intTo ) {e[++num].to=to ; E[num].pre=head[ from]; head[ from]=num;}voidDfsintNowintfather) {    intF0=inf,f2=0, f1=0, w=0, s=0;  for(intI=head[now];i;i=e[i].pre) {        intto=e[i].to; if(To==father)Continue;        DFS (To,now); S=min (f[to][0],f[to][1]); W+=s; F0=min (f0,f[to][1]-s); F1+=min (f[to][1],min (f[to][0],f[to][2])); if(F2<inf) f2+=f[to][0]; } f[now][1]=f1+1; f[now][2]=F2; if(F0==inf) f[now][0]=INF; Elsef[now][0]=w+F0;}intMain () {intx, y; scanf ("%d",&N);  for(intI=1; i<n;i++) {scanf ("%d%d",&x,&y);        Insert (x, y);    Insert (Y,X); } DFS (1,0); intAns=min (f[1][1],f[1][0]); cout<<ans;}

Rokua P2899 [Usaco08jan] Cellular cell phone network

Related Article

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.