2013-07-31 22:23:15Morris

[UVA][dp] 1218 - Perfect Service

A network is composed of N computers connected by N - 1 communication links such that any two computers can be communicated via a unique route. Two computers are said to be adjacent if there is a communication link between them. The neighbors of 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 acting as servers to provide resources to their neighbors. Note that a server can serve all its neighbors. A set of servers in the network forms a perfect service if every client (non-server) is served by exactly one server. The problem is to find a minimum number of servers which forms a perfect service, and we call this number perfect service number.

We assume that N ($ le$10000) is a positive integer and these N computers are numbered from 1 to N . For example, Figure 1 illustrates a network comprised of six computers, where black nodes represent servers and white nodes represent clients. In Figure 1(a), 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.

epsfbox{p3685.eps}

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 next N - 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 

6 
1 3
2 3
3 4
4 5
4 6
0 
2 
1 2
-1

Sample Output 

2 
1


題目描述:

在一個樹狀圖中,可以讓一個節點當作伺服器,而每個不是伺服器的節點恰好一個伺服器服務。
而伺服器只會服務相鄰的節點,進可能使伺服器越少越好。求最少個數。

題目解法:


考慮三種情況,先將樹當作有根樹。

那麼狀態根據子樹去判定。

1. 當前這個點不是 server, 且沒有被服務到, 而其子樹其他點都有符合條件。
2. 當前這個點不是 server, 且這個點已經被服務到
3. 當前這個點是 server

先來想第 3. 點,由於其孩子所有點都要被服務則必須從 min(1., 3.) 之中去加總。
如果是取 2. 則會造成孩子被兩個 server 服務。

第 1. 點,加總所有的 2. 可能。

第 2. 點,孩子中其中一個是 server, 而其他都是 2.


#include <stdio.h>
#include <vector>
#include <string.h>
using namespace std;
vector<int> g[10005];
int dp[10005][3];
void dfs(int nd, int p) {
int &ret1 = dp[nd][0];//isn't server, but exactly one server
int &ret2 = dp[nd][1];//isn't server, no one server
int &ret3 = dp[nd][2];//is server.
int sum = 0;//dp[nd][0]-only one is server, other isn't server, but exactly one server
ret1 = 0xffffff;
ret2 = 0;
ret3 = 1;
for(vector<int>::iterator it = g[nd].begin();
it != g[nd].end(); it++) {
if((*it) != p) {
dfs(*it, nd);
ret3 += min(dp[*it][1], dp[*it][2]);
ret2 += dp[*it][0];
sum += dp[*it][0];
}
}
for(vector<int>::iterator it = g[nd].begin();
it != g[nd].end(); it++) {
if((*it) != p) {
ret1 = min(ret1, sum-dp[*it][0]+dp[*it][2]);
}
}
}
int main() {
int n, i, x, y;
while(scanf("%d", &n) == 1) {
for(i = 1; i <= n; i++)
g[i].clear();
for(i = 0; i < n-1; i++) {
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1, 0);
if(n == 1) dp[1][0] = dp[1][2] = 1;
printf("%d\n", min(dp[1][0], dp[1][2]));
scanf("%d", &i);
if(i < 0) break;
}
return 0;
}