poj3398 Perfect Service
来源:互联网 发布:linux ddos攻击脚本 编辑:程序博客网 时间:2024/06/06 03:52
Description
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.
用f,g和h分别表示自己是服务器、自己不是服务器并且没有儿子是服务器、自己不是服务器而且有一个儿子是服务器的最小代价。
f[u]=sum{min(g[v],f[v])}
g[u]=sum{h[v]}
h[u]=min{g[u]-h[v]+f[v]}
注意如果u没有儿子的话h要设成无穷大,但是不能太大,否则加起来以后可能变负。
#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int oo=20000;int f[10010],g[10010],h[10010],n;vector<int> to[10010];void dfs(int u,int fa){ int i,j,k,v,x,y,z; f[u]=1; g[u]=0; h[u]=oo; for (i=0;i<to[u].size();i++) if ((v=to[u][i])!=fa) { dfs(v,u); f[u]+=min(g[v],f[v]); g[u]+=h[v]; } for (i=0;i<to[u].size();i++) if ((v=to[u][i])!=fa) h[u]=min(h[u],g[u]-h[v]+f[v]);}int main(){ int i,j,k,p,q,x,y,z; while (1) { scanf("%d",&n); for (i=1;i<=n;i++) to[i].clear(); for (i=1;i<n;i++) { scanf("%d%d",&x,&y); to[x].push_back(y); to[y].push_back(x); } dfs(1,-1); printf("%d\n",min(f[1],h[1])); scanf("%d",&x); if (x==-1) return 0; }}
- poj3398 Perfect Service
- POJ 3398 Perfect Service
- POJ-3398-Perfect Service
- UVA 1218Perfect Service
- UVa1218 Perfect Service
- 1218 - Perfect Service
- UVa 1218 Perfect Service
- Uva-1218 Perfect Service
- UVA-1218 Perfect Service
- UVA1218 - Perfect Service
- UVa-1228 Perfect Service
- UVA1218 Perfect Service
- UVA 1218 Perfect Service
- POJ 3398 Perfect Service
- poj 3398 Perfect Service
- POJ 3398 Perfect Service
- UVa1218 Perfect service[树规]
- POJ 3398 Perfect Service
- Docker配置桥接网络
- java内存详解
- 测试数据库
- Fragment运行机制源码分析(二)
- 数组与指针2
- poj3398 Perfect Service
- linux 中php以及nginx的重启命令
- storyboard学习心得
- NOIP2016 滚粗记
- Java编程思想(第四章)
- 翻转链表
- 并查集详解
- java程序员职业进阶
- 进制转换升级版