UVA1218
定义:
为第
个节点的子节点集
为以
为根节点的最少服务器需求量
- 为将第 个节点作为服务器的时的情况,即它的子节点既可以是服务器,又可以是客户端。
- 为第 个节点为客户端且它的父节点为服务器的情况,即它的子节点只能是客户端。(一个客户端只能且必须能和一个服务器直接相连)
- 为第 个节点为客户端且它的父节点也为客户端的情况,即i有且只有一个子节点是服务器。
转移方程:
为
的子节点集合
-
即枚举 的每一个子节点作为服务器的情况。
AC代码:
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
int N, Status;
vector<int> Son[10001];
int dp[10001][3];
void Clear() {
for (int i = 1; i <= N; ++i) {
Son[i].clear();
}
}
void Input() {
cin >> N;
Clear();
for (int i = 1; i < N; ++i) {
int x, y;
cin >> x >> y;
Son[x].push_back(y);
Son[y].push_back(x);
}
}
void DP(int Root,int Father) {
dp[Root][0] = 1;
dp[Root][1] = 0;
dp[Root][2] = 10000;//inf
for (auto SonIt = Son[Root].cbegin(); SonIt != Son[Root].cend(); ++SonIt) {
if (*SonIt == Father) {
continue;
}
DP(*SonIt, Root);
dp[Root][0] += min(dp[*SonIt][0], dp[*SonIt][1]);
dp[Root][1] += dp[*SonIt][2];
}
for (auto SonIt = Son[Root].cbegin(); SonIt != Son[Root].cend(); ++SonIt) {
if (*SonIt == Father) {
continue;
}
dp[Root][2] = min(dp[Root][2], dp[Root][1] - dp[*SonIt][2] + dp[*SonIt][0]);
}
}
int main() {
while (true) {
Input();
DP(1, -1);
cout <<min(dp[1][0],dp[1][2]) << endl;
cin >> Status;
if (Status == -1) {
break;
}
}
return 0;
}