您的位置:首页 > 其它

HDU 1054 Strategic Game 二分匹配 | 树型DP | 贪心

2014-06-08 12:11 274 查看

Strategic Game

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4717 Accepted Submission(s): 2133



Problem Description
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form
a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?

Your program should find the minimum number of soldiers that Bob has to put for a given tree.

The input file contains several data sets in text format. Each data set represents a tree with the following description:

the number of nodes

the description of each node in the following format

node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifier

or

node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.

For example for the tree:




the solution is one soldier ( at the node 1).

The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following table:



Sample Input
4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)




Sample Output
1
2




Source
Southeastern Europe 2000

题目链接:HDU 1054 Strategic Game

题目大意:给你一颗树,让你求树上的最小点覆盖,一看题,裸裸的二分,不管三七二十一先敲一个朴素的矩阵版,交上去,TLE。。。加个输入优化,依旧TLE。。看来不用邻接表不行了。。再敲。。直接1A。。果然邻接表很强大~

PS:以下代码均加入了输入优化。

108566232014-06-08 11:20:08Accepted1054140MS296K1261 BC++poursoul
代码如下:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define clear(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE + 1))
using namespace std;
const int maxN = 2000;
const int maxE = 1000000;
struct Edge{
	int v, n;
}edge[maxE];
int adj[maxN], cntE, vis[maxN], link[maxN];
int n;
void addedge(int u, int v){
	edge[cntE].v = v; edge[cntE].n = adj[u]; adj[u] = cntE++;
	edge[cntE].v = u; edge[cntE].n = adj[v]; adj[v] = cntE++;
}
int find(int u){
	for(int i = adj[u]; ~i; i = edge[i].n) if(!vis[edge[i].v]){
		int v = edge[i].v;
		vis[v] = 1;
		if(link[v] == -1 || find(link[v])){
			link[v] = u;
			return 1;
		}
	}
	return 0;
}
int match(){
	int ans = 0, i;
	clear(link, -1, n);
	for(i = 0; i < n; ++i){
		clear(vis, 0, n);
		ans += find(i);
	}
	return ans;
}
int read(){
	int x = 0;
	char ch = ' ';
	while(ch < '0' || ch > '9') ch = getchar();
	while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
	return x;
}
void work(){
	int i, j, m, u, v;
	clear(adj, -1, n);
	cntE = 0;
	for(i = 0; i < n; ++i){
		u = read(); m = read();
		for(j = 0; j < m; ++j){
			v = read();
			addedge(u, v);
		}
	}
	printf("%d\n", match() / 2);
}
int main(){
	while(~scanf("%d", &n)) work();
	return 0;
}


那么这到题能不能用树型DP写呢?当然可以~

dp[i][0]表示当前点是覆盖点且子节点均被覆盖

dp[i][1]表示当前点不是覆盖点且子节点及子节点全被覆盖。

则每个节点有:

dp[i][0] += sum(min(dp[j][0], dp[j][1]));

dp[i][1] += sum(dp[j][0]);

效率还不错~

108566872014-06-08 11:40:19Accepted105431MS300K1201 BG++poursoul
代码如下:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define min(A, B) ((A) < (B) ? (A) : (B))
#define clear(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE + 1))
using namespace std;
const int maxN = 2000;
const int maxE = 1000000;
struct Edge{
	int v, n;
}edge[maxE];
int adj[maxN], cntE, dp[maxN][2];
int n;
void addedge(int u, int v){
	edge[cntE].v = v; edge[cntE].n = adj[u]; adj[u] = cntE++;
	edge[cntE].v = u; edge[cntE].n = adj[v]; adj[v] = cntE++;
}
void dfs(int u, int fa){
	dp[u][0] = 1;
	dp[u][1] = 0;
	for(int i = adj[u]; ~i; i = edge[i].n){
		int v = edge[i].v;
		if(v == fa) continue;
		dfs(v, u);
		dp[u][0] += min(dp[v][0], dp[v][1]);
		dp[u][1] += dp[v][0];
	}
}
int read(){
	int x = 0;
	char ch = ' ';
	while(ch < '0' || ch > '9') ch = getchar();
	while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
	return x;
}
void work(){
	int i, j, m, u, v;
	clear(adj, -1, n);
	cntE = 0;
	for(i = 0; i < n; ++i){
		u = read(); m = read();
		for(j = 0; j < m; ++j){
			v = read();
			addedge(u, v);
		}
	}
	dfs(0, -1);
	printf("%d\n", min(dp[0][0], dp[0][1]));
}
int main(){
	while(~scanf("%d", &n)) work();
	return 0;
}


当然,这题贪心也毫无压力。

只要从叶子节点一直向上更新,如果u 和 v 都没有被访问则将u标记为覆盖点。

108568042014-06-08 12:10:51Accepted105431MS308K1136 BC++poursoul
代码如下:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define clear(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE + 1))
using namespace std;
const int maxN = 2000;
const int maxE = 1000000;
struct Edge{
	int v, n;
}edge[maxE];
int adj[maxN], cntE, vis[maxN];
int n, ans;
void addedge(int u, int v){
	edge[cntE].v = v; edge[cntE].n = adj[u]; adj[u] = cntE++;
	edge[cntE].v = u; edge[cntE].n = adj[v]; adj[v] = cntE++;
}
void dfs(int u, int fa){
	for(int i = adj[u]; ~i; i = edge[i].n){
		int v = edge[i].v;
		if(v == fa) continue;	
		dfs(v, u);
		if(!vis[v] && !vis[u]){
			vis[u] = 1;
			ans++;
		}
	}
}
int read(){
	int x = 0;
	char ch = ' ';
	while(ch < '0' || ch > '9') ch = getchar();
	while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
	return x;
}
void work(){
	int i, j, m, u, v;
	clear(vis,  0, n);
	clear(adj, -1, n);
	cntE = 0;
	ans = 0;
	for(i = 0; i < n; ++i){
		u = read(); m = read();
		for(j = 0; j < m; ++j){
			v = read();
			addedge(u, v);
		}
	}
	dfs(0, -1);
	printf("%d\n", ans);
}
int main(){
	while(~scanf("%d", &n)) work();
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: