UVA1218
definitions :
for the first
child node of node set
is to
is a root of the minimum demand server
- is the first nodes as the time server, i.e. either its child nodes may be a server, but may be a client.
- for the first node to the client node and its parent server, the only child nodes that it is the client. (A client can only and must be directly connected to a server and)
- 为第 个节点为客户端且它的父节点也为客户端的情况,即i有且只有一个子节点是服务器。
转移方程:
为
的子节点集合
-
i.e. enumeration of each child node as the server.
AC Code :
#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;
}