POJ 3398/VA 1218 Perfect Service tree DP, pojdp

Source: Internet
Author: User

POJ 3398/VA 1218 Perfect Service tree DP, pojdp


Tree DP


Perfect Service
Time Limit:2000 MS   Memory Limit:65536 K
Total Submissions:1378   Accepted:668

Description

A network is composedNComputers connectedN− 1 communication links such that any two computers can be communicated via a unique route. Two computers are said to beAdjacentIf there is a communication link between them.NeighborsOf a computer is the set of computers which are adjacent to it. In order to quickly access and retrieve large amounts of information, we need to select some computers actingServersTo provide resources to their neighbors. Note that a server can serve all its neighbors. A set of servers in the network formsPerfect serviceIf every client (non-server) is servedExactly oneServer. The problem is to find a minimum number of servers which forms a perfect service, and we call this numberPerfect service number.

We assume thatN(≤ 10000) is a positive integer and theseNComputers are numbered from 1N. For example, Figure 1 extends strates a network comprised of six computers, where black nodes represent servers and white nodes represent clients. in Figure 1 (), servers 3 and 5 do not form a perfect service because client 4 is adjacent to both servers 3 and 5 and thus it is served by two servers which contradicts the assumption. conversely, servers 3 and 4 form a perfect service as shown in Figure 1 (B ). this set also has the minimum cardinality. therefore, the perfect service number of this example equals two.

Your task is to write a program to compute the perfect service number.

Input

The input consists of a number of test cases. The format of each test case is as follows: The first line contains one positive integer,N, Which represents the number of computers in the network. The nextN−1 lines contain all of the communication links and one line for each link. each line is represented by two positive integers separated by a single space. finally, a 0 at the (N + 1) th line indicates the end of the first test case.

The next test case starts after the previous ending symbol 0. A −1 indicates the end of the whole inputs.

Output

The output contains one line for each test case. Each line contains a positive integer, which is
The perfect service number.

Sample Input

61 32 33 44 54 6021 2-1

Sample Output

21

Source

Kaohsiung 2006

[Submit] [Go Back] [Status] [Discuss]


/*************************************** * ******** Author: CKbossCreated Time:, Sunday, January 1, February 22, 2015 File Name: POJ3398_2.cpp *************************************** * ********/# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <string> # include <cmath> # include <cstdlib> # include <vector> # include <queue> # include <set> # include <map> using namespace std; const int maxn = 11 000; const int INF = 1e6; struct Edge {int to, next;} edge [maxn * 2]; int Adj [maxn], Size; int dp [maxn] [3], n; void init () {Size = 0; for (int I = 0; I <= n + 10; I ++) {Adj [I] =-1; dp [I] [0] = 1; dp [I] [1] = 0; dp [I] [2] = INF ;}} void add_edge (int u, int v) {edge [Size]. to = v; edge [Size]. next = Adj [u]; Adj [u] = Size ++;} void dfs (int u, int fa) {for (int I = Adj [u]; ~ I; I = edge [I]. next) {int v = edge [I]. to; if (v = fa) continue; dfs (v, u); dp [u] [0] + = min (dp [v] [0], dp [v] [1]); dp [u] [1] + = dp [v] [2]; dp [u] [2] = min (dp [u] [2], dp [v] [0]-dp [v] [2]);} dp [u] [2] + = dp [u] [1];} int main () {// freopen ("in.txt", "r", stdin ); // freopen ("out.txt", "w", stdout); while (scanf ("% d", & n )! = EOF) {if (n =-1) break; else if (n = 0) continue; init (); for (int I = 0; I <n-1; I ++) {int a, B; scanf ("% d", & a, & B); add_edge (a, B); add_edge (B, a);} dfs (); printf ("% d \ n", min (dp [1] [0], dp [1] [2]);} return 0 ;}





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.