1596: [Usaco2008 Jan] Telephone network

Source: Internet
Author: User

1596: [Usaco2008 Jan] Telephone network time limit:10 Sec Memory limit:162 MB
submit:601 solved:265
[Submit] [Status] [Discuss] Description

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 Output2

Output Description:

FJ can choose to build a communication tower on grassland 2 and grassland 3, or grassland 3 and grassland 5.
HINT Source

Gold

Solution: A classic tree-like DP problem, we can classify the discussion, set \ (F[i,j] \) Represents the first point, \ (j=0\) to select the optimal value of the current point, \ (j=1\) means the current point is not selected and the current point has been controlled by the optimal value, \ (j=2\) Represents an optimal value that does not select the current point and the current point is not controlled (but the node under the current point must be fully controlled), and (Y \) is a subordinate node of \ (x \), the following relationship: \ (b[x,0]=\sum min (b[y,0],b[y,1],b[y,2]) \) for \ (b[ x,1] \), if present \ (b[y,0] \leq b[y,1]\) then \ (b[x,1]=\sum min (b[y,0],b[y,1]) \); otherwise \ (B[x,1]=\sum b[y,1] +min (b[y,0]-b[y,1]) \) If it is not possible (b[x,1]=2147483647 \) \ (b[x,2] = \sum b[y,1] \), if it is not possible to have the \ (b[x,2]=2147483647\) formula as above, then DP, note that the final result should be \ (min (b [1,0],b[1,1]), instead of \ (min (b[1,0],b[1,1],b[1,2]) \), the last node must be controlled if the scheme is valid (ps:\ (2147483647= maxlongint \) Represents only a larger number, facilitates subsequent alignment operations)
1/**************************************************************2Problem:15963 User:hansbug4 language:pascal5 result:accepted6Time: theMs7Memory:2500KB8****************************************************************/9  Ten type OnePoint=^node; ANode=Record - G:longint; - Next:point; the     End; - var - I,j,k,l,m,n:longint; -A:Array[0..100000] ofPoint ; +B:Array[0..100000,0..2] ofLongint; -C:Array[0..100000] ofLongint; + procedureAdd (x,y:longint); A           varP:point; at           begin -New (p);p ^.g:=y; -p^.next:=a[x];a[x]:=p; -           End; - functionmin (x,y:longint): Longint; -          begin in               ifX<y ThenMin:=xElsemin:=y; -          End; to procedureDP (x:longint); +           var - P:point; the A1,a2,a3,a4,a5:longint; *           begin $p:=a[x];c[x]:=1;Panax Notoginsenga1:=0; a2:=0; -a3:=0; a4:=0; a5:=Maxlongint; the                 whileP<>Nil  Do +                      begin A                           ifc[p^.g]=0  Then the                              begin + DP (P^.G); -A1:=a1+min (min (B[P^.G,0],B[P^.G,1]), B[P^.G,2]); $                                   ifB[P^.G,1]=maxlongint Thena3:=Maxlongint $                                   Else ifA3<>maxlongint ThenA3:=A3+B[P^.G,1]; -                                   ifB[P^.G,0]&LT;=B[P^.G,1] Then -                                      begin thea4:=1; -A2:=A2+B[P^.G,0];Wuyi                                      End the                                   Else -                                       begin WuA5:=min (A5,B[P^.G,0]-B[P^.G,1]); -A2:=A2+B[P^.G,1]; About                                       End; $                              End; -p:=P^.next; -                      End; -                ifa4=0  Thena2:=a2+A5; Aa1:=a1+1; +B[x,0]:=a1;b[x,1]:=a2;b[x,2]:=A3; the           End; - begin $ READLN (n); the       fori:=1  toN Doa[i]:=Nil; the       fori:=1  toN-1  Do the          begin the readln (j,k); - Add (j,k); add (k,j); in          End; theFillchar (C,sizeof (c),0); theFillchar (b,sizeof (b),0); Aboutdp1); theWriteln (min (b[1,1],b[1,0])); the Readln; the End.

1596: [Usaco2008 Jan] Telephone network

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.