Perfect Service(树形DP/树的最小支配集变形)

描述

传送门:POJ3398

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

1
2
3
4
5
6
7
8
9
10
6
1 3
2 3
3 4
4 5
4 6
0
2
1 2
-1

Sample Output

1
2
2
1

思路

  • 题意:$n$个计算机,其中一些要作为服务器,服务器可以与其他机子相连,问最小的服务器的个数,但是一个普通机子不能与两台及以上服务器相连。
  • 如果 不看最后一句话,这就是一个树上的最小支配集的问题,具体可以看这个
  • 现在我们又多了一个条件,所以要对原来的dp算法进行一些修改。
    • $dp[u][0]$表示当前结点属于支配集的情况;
    • $dp[u][1]$表示当前结点不属于支配集,且被其儿子覆盖的情况;
    • $dp[u][2]$表示当前结点不属于支配集,且被其父亲覆盖的情况。
  • 对于$dp[u][0]$,转移应为每个儿子结点的第一种状态和第三种状态的最小值加$1$;
  • 对于$dp[u][2]$,转移应为每个儿子结点的第二种状态之和;
  • 对于$dp[u][1]$,如果没有儿子结点,值为$+\infty$,否则与其儿子结点的第一第二个状态有关,而根据题目要求,必须且只能有一个儿子结点的第一个状态转移上来,否则会出现其所有儿子结点都不在支配集而当前点也不在支配集的情况。因此在计算过程中应标记存不存在一个儿子结点为第一种状态同时计算更新所有儿子结点的第二种状态和第一种状态的差的最小值。
  • 这样最终的答案就是$min(dp[1][0],dp[1][1]$

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <bits/stdc++.h>
using namespace std;
#define clr(a,x) memset(a, x, sizeof(a))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define X first
#define Y second
#define fastin ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;

const int maxn = 10005;
vector <int> G[maxn];
int dp[maxn][3];

void init(int n)
{
for (int i = 1; i <= n; i++)
G[i].clear();
clr(dp, 0x3f);
}

void addedge(int u, int v)
{
G[u].pb(v);
G[v].pb(u);
}

void dfs(int u, int p)
{
dp[u][0] = 1, dp[u][2] = 0;
int sum = 0, inc = INF;
bool flag = 0;
for (int i = 0; i < G[u].size(); i++)
{
int v = G[u][i];
if (v == p) continue;
dfs(v, u);
dp[u][0] += min(dp[v][0], dp[v][2]);
if (dp[v][0] <= dp[v][1])
sum += dp[v][0], flag = 1;
else
sum += dp[v][1], inc = min(inc, dp[v][0] - dp[v][1]);
dp[u][2] += dp[v][1];
dp[u][2] = min(dp[u][2], INF);
}
if (!flag && inc != INF) dp[u][1] = INF;
else
{
dp[u][1] = sum;
if (!flag) dp[u][1] += inc;
}
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
int n;
while (~scanf("%d", &n))
{
int u, v, tmp;
init(n);
for (int i = 1; i < n; i++)
{
scanf("%d%d", &u, &v);
addedge(u, v);
}
dfs(1, 1);
int ans = min(dp[1][0], dp[1][1]);
printf("%d\n", ans);
scanf("%d", &tmp);
if (tmp) break;
}
return 0;
}
捐助作者
0%