POJ 3398 Perfect Service
来源:互联网 发布:迅雷淘宝充值卡号密码 编辑:程序博客网 时间:2024/05/21 20:22
POJ 3398 Perfect Service
Description
A network is composed ofN computers connected by N − 1 communication links such that any two computers can be communicated via a unique route. Two computers are said to beadjacent 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 aperfect 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 numberperfect service number.
We assume that N (≤ 10000) is a positive integer and theseN 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
61 32 33 44 54 6021 2-1
Sample Output
21
Source
Solution
树上最小支配集,但需要注意的是这道题中一个普通机子不能与两台及以上服务器相连,所以dp[i][0]的更新方式有变化:dp[i][0] = min(dp[u][0], dp[u][2]),并且u != fa[x].
Code
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <map>#include <vector>#include <queue>#define L 10010#define LL long long#define inf 0x7f7f7f7fusing namespace std;struct node { int to, nxt;} e[L << 1];int n, head[L], a, b, cnt, dp[L][3], temp;bool vis[L];inline void add(int a, int b) { e[++cnt].nxt = head[a], e[cnt].to = b, head[a] = cnt;}inline void dfs(int x, int fa) { int u, minn, minx = inf; bool pd = 1, bj = 1; vis[x] = 1, dp[x][0] = 1; for (int i = head[x]; i; i = e[i].nxt) { u = e[i].to; if (!vis[u]) { if (u == fa) continue; pd = 0, dfs(u, x); dp[x][0] += min(dp[u][0], dp[u][2]); if (dp[u][1] < dp[u][0]) {dp[x][1] += dp[u][1];if (dp[u][0] < minx) minx = dp[u][0], minn = dp[u][1]; } else dp[x][1] += dp[u][0], bj = 0; dp[x][2] += min(dp[u][1], dp[u][0]); } } if (bj) dp[x][1] += minx - minn; if (pd) dp[x][0] = 1, dp[x][1] = inf, dp[x][2] = 0;}int main() { freopen("POJ3398.in", "r", stdin); freopen("POJ3398.out", "w", stdout); while (scanf("%d", &n) != EOF) { cnt = 0; memset(dp, 0, sizeof(dp)); memset(head, 0, sizeof(head)); memset(vis, 0, sizeof(vis)); for (int i = 1; i < n; ++i) { scanf("%d %d", &a, &b); add(a, b), add(b, a); } dfs(1, n); printf("%d\n", min(dp[1][1], dp[1][0])); scanf("%d", &temp); if (temp == -1) return 0; }}
Summary
审题一定要仔细,一开始看错了题
- POJ 3398 Perfect Service
- POJ-3398-Perfect Service
- POJ 3398 Perfect Service
- poj 3398 Perfect Service
- POJ 3398 Perfect Service
- POJ 3398 Perfect Service
- poj 3398 Perfect Service
- POJ 3398 Perfect Service(树形DP)
- POJ 3398 Perfect Service 最小支配集变形
- poj 3398 Perfect Service(最小支配集树形DP)
- POJ 3398 / UVA 1218 Perfect Service 树形DP
- POJ 3398 Perfect Service(树形DP,最小支配集)
- POJ 3398 Perfect Service【最小支配集修改 dp】
- POJ 3398 Perfect Service 树形DP 最小支配集变形
- poj 3398 Perfect Service 树形dp类似求树的点最小支配集
- Poj 3659 Perfect Service【最小支配集、树型dp】
- UVA 1218Perfect Service
- UVa1218 Perfect Service
- 【MOOC】数学实验
- 用maven创建web项目
- 使用Jhash替换传统hash有效降低hash冲突提供查找效率
- Java面向对象思想
- vue常用内置指令
- POJ 3398 Perfect Service
- 素数判断
- java学习第十九天之API、Date、Calendar
- [并发]java.util.concurrent并发工具包
- 图像处理作业——模糊阈值分割
- 全连接神经网络
- c语言实现求最大公约数的三种方法
- javaday04
- 2017浙江理工大学校赛I题