hdu_1054最小点覆盖树上

本文介绍了HDU_1054问题,这是一个涉及战略游戏的算法挑战,探讨了如何构建最小点覆盖树以解决游戏中的策略问题。

Strategic Game

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8139    Accepted Submission(s): 3891


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
用的是贪心的写法,dfs o(n)效率 然后从叶子结点进行贪心,也是o(n)效率就是o(n),对于点覆盖问题,如果该节点和它父节点没有被访问过,则把父节点放入覆盖集合中,然后继续遍历。
#include<bits/stdc++.h>
using namespace std;
const int maxn=3005;
int head[maxn],Set[maxn],visit[maxn],father[maxn],now,cnt,nowpos[maxn];
int n;
struct Node{
	int to;
	int next;
	Node():to(0),next(0){	
	} ;
}node[maxn]; 
void init(){
	memset(head,-1,sizeof(head));
	memset(Set,0,sizeof(Set));
	memset(visit,0,sizeof(visit));
	memset(father,0,sizeof(father));
	memset(nowpos,0,sizeof(nowpos));
	now=0;
	cnt=0;
}

int add_edge(int a,int b){
	node[cnt].to=b;
	node[cnt].next=head[a];
	head[a]=cnt++;
	
	node[cnt].to=a;
	node[cnt].next=head[b];
	head[b]=cnt++;
	return 0;
}
int dfs(int x){
	nowpos[now++]=x;
	for(int k=head[x];k!=-1;k=node[k].next){
		int b=node[k].to;
		if(!visit[b]){
			father[b]=x;//标记b的父节点 
			visit[b]=1;
			dfs(b);
		}
	}
	return 0;
}
int greedy(){
	int res=0;
	for(int i=n-1;i>=1;i--){//到1就可以了不需要最后一个点,因为是点覆盖呢 
		int b=nowpos[i];
		if(!visit[b]&&!visit[father[b]]){
			Set[father[b]]=1;
			visit[father[b]]=1;
			visit[b]=1;
			res++;
			//cout<<"res:"<<res<<endl;
		}
	}
	return res;
}
int main(){
	while(~scanf("%d",&n)){
		init();
		for(int i=0;i<n;i++){
			int a,num;
			scanf("%d:(%d)",&a,&num);
			for(int j=1;j<=num;j++){
				int b;
				scanf("%d",&b);
				add_edge(a,b);
			}
		}
		visit[0]=1;
		//father[0]=node[head[0]].next;
		father[0]=0;
		dfs(0);
		memset(visit,0,sizeof(visit));
		int ans = greedy();
		printf("%d\n",ans);
	}
} 


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值