POJ 3659 Cell Phone Network
Description
Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N) so they can all communicate.
Exactly N-1 pairs of pastures are adjacent, and for any two pastures A and B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B) there is a sequence of adjacent pastures such that A is the first pasture in the sequence and B is the last. Farmer John can only place cell phone towers in the pastures, and each tower has enough range to provide service to the pasture it is on and all pastures adjacent to the pasture with the cell tower.
Help him determine the minimum number of towers he must install to provide cell phone service to each pasture.
Input
* Line 1: A single integer: N
* Lines 2..N: Each line specifies a pair of adjacent pastures with two space-separated integers: A and B
Output
* Line 1: A single integer indicating the minimum number of towers to install
Sample Input
51 35 24 33 5
Sample Output
2
Source
USACO 2008 January Gold
Solution
树的最小支配集(点支配其儿子和父亲节点),可以使用贪心和动态规划
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
using namespace std;
struct node {
int nxt, to;
} e[L << 1];
int n, cnt, head[L], num[L], dfn, pre[L], ans, a, b;
bool vis[L], set[L];
inline void add(int x, int y) {
e[++cnt].nxt = head[x], e[cnt].to = y, head[x] = cnt;
}
inline void dfs(int x) {
num[++dfn] = x;
for (int i = head[x]; i; i = e[i].nxt)
if (!vis[e[i].to]) {
vis[e[i].to] = true;
pre[e[i].to] = x;
dfs(e[i].to);
}
}
int main() {
freopen("POJ3659.in", "r", stdin);
freopen("POJ3659.out", "w", stdout);
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d %d", &a, &b);
add(a, b), add(b, a);
}
dfs(1);
memset(vis, 0, sizeof(vis));
for (int i = dfn; i >= 1; --i) {
int temp = num[i];
if (!vis[temp]) {
if (!set[pre[temp]]) set[pre[temp]] = true, ans++;
vis[temp] = vis[pre[temp]] = vis[pre[pre[temp]]] = true;
}
}
printf("%d\n", ans);
return 0;
}
动态规划
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
#include <queue>
#define L 10010
#define inf 07f7f7f7f
#define LL long long
using namespace std;
struct node {
int nxt, to;
} e[L << 1];
int n, cnt, head[L], ans, a, b, dp[L][3], minx = L, minn;
bool vis[L], bj, pd;
inline void add(int x, int y) {
e[++cnt].nxt = head[x], e[cnt].to = y, head[x] = cnt;
}
inline void dfs(int x) {
int u, minx = L, minn;
bool bj = 1, pd = 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]) {
pd = 0;
dfs(u);
dp[x][0] += min(dp[u][0], min(dp[u][1], 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] = L, dp[x][2] = 0;
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d %d", &a, &b);
add(a, b), add(b, a);
}
dfs(1);
printf("%d\n", min(dp[1][1], dp[1][0]));
return 0;
}
Summary
贪心没什么要注意的,但是动态规划要注意,在搜索函数中定义一下变量类型(我也不知道为什么),否则会WA