1218 - Perfect Service

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=847&page=show_problem&problem=3659
网络是由计算机通过N - 1通信链路等,任何两台电脑可以通过一个独特的沟通路线。两台电脑是相邻的如果有它们之间的通信链路。邻居的电脑电脑的设置相邻。为了快速访问和检索大量的信息,我们需要选择一些计算机作为服务器来提供资源,以他们的邻居。注意,服务器可以为它所有的邻居。一组服务器的网络形成了一个完善的服务如果每个客户(非服务器端)时有准确的一个服务器。问题是要找到一个最小的服务器数量形成一个完美的服务,和这个号码我们打完善的服务号码。
我们假设N(10000)是一个正整数,这些电脑编号从1到N。例如,图1显示了一个网络组成的六个电脑,在黑色节点代表服务器和白色节点代表客户。如图1所示(一个),服务器3和5不形成一个完善的服务,因为客户端4是相邻的两个服务器3和5,因此它是由两个服务器,与假设。相反,服务器3和4形成一个完善的服务,如图1所示(b)。这组也有最低基数。因此,完善的服务数量的这个例子等于2。
你的任务是编写一个程序,计算出最小所需服务器的数量。
输入
输入由一个测试用例的数量。每个测试用例的格式如下:第一行包含一个正整数,N,代表网络中计算机的数量。接下来的N - 1行包含所有的通信链接和为每个链接一行。每一行都是代表两个正整数用一个空格隔开。最后,在(N + 1)’ 0 ’ th行表示结束的第一个测试用例。
下一个测试用例开始前结束后’ 0 ‘象征。“1”表示整个输入的结束。
输出
为每个测试用例输出包含一行。每一行包含一个正整数,这是最小所需服务器的数量。
样例输入
6
1 3
2 3
3 4
4 5
4 6
0
2
1 2
1
样例输出
2
1

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 (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.

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

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 10000 + 5;
const int INF = 1000000000;
vector<int> G[maxn], vertices;
int p[maxn], d[maxn][3];
void dfs(int u, int fa) {/*建树*/
vertices.push_back(u);
p[u] = fa;
for(int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if(v != fa) dfs(v, u);
}
}
int main() {
int n;
while(scanf("%d", &n) == 1) {
for(int i = 0; i < n; i++) G[i].clear();
for(int i = 0; i < n-1; i++) {
int u, v;
scanf("%d%d", &u, &v); u--; v--;
G[u].push_back(v);
G[v].push_back(u);
}
vertices.clear();
dfs(0, -1);
for(int i = vertices.size()-1; i >= 0; i--) {
int u = vertices[i];
d[u][0] = 1; d[u][1] = 0;//初始状态。当然是服务器就是1不是就是0啰
for(int j = 0; j < G[u].size(); j++) {
int v = G[u][j];
if(v == p[u]) continue;
d[u][0] += min(d[v][0], d[v][1]); // u是服务器,它的子节点v可是也可不是
d[u][1] += d[v][2]; // d[u][1]意味u不是服务器,但u的父节点是服务器,即u的所有子节点都不是服务器。d[v][2]意味v的父亲节点和v都不是服务器,所以v的子节点必有一个会是服务器
if(d[u][0] > INF) d[u][0] = INF; 
//避免超出范围,其实比预定的无穷大还大即可果断舍弃
if(d[u][1] > INF) d[u][1] = INF;
}
d[u][2] = INF;
for(int j = 0; j < G[u].size(); j++) {
int v = G[u][j];
if(v == p[u]) continue;//父亲节点,跳过
d[u][2] = min(d[u][2], d[u][1] - d[v][2] + d[v][0]); 
//这里我们利用已经算出的d[u][1],可以节省枚举一个子节点是而其他子节点不是再累加所花费的Oj^2)时间
}
}
printf("%d\n", min(d[0][0], d[0][2]));
scanf("%d", &n); //标记
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值