hdoj 1054 Strategic Game 最小点覆盖模版题dfs方式(Java版)

该博客介绍了如何解决HDU 1054 Strategic Game问题,即在树形结构中用最少的士兵防守所有路径。博主通过将问题转化为求解二分图的最小点覆盖,利用最大匹配算法实现,Java代码采用链式前向星建图。错误初期因忽视数据格式导致WA,修正后AC。博主还提到可以使用树形DP解决,但对此不熟悉,鼓励读者自行研究。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054
题目:
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

题解:可以把题目转化成二分图求最小点覆盖。而最小点覆盖=最大边匹配。这题数据量有点大,所以用邻接矩阵会超时,Java里没有用Vector的,这里用链式前向星的方式建图。 其实这就是个匈牙利算法的模版题,建个无向图,结果除以2就是答案,我卡了好几个小时。。。差点以为数据出错了。因为wa的时间很短,也就是没过几组数据,可是写法应该是没问题的,又仔细看了看题,突然发现数据的输入有问题。。求括号里的值时,我没注意,想当然地只求了一位。。。改了之后就ac了。 这题还可以用树形dp做,不过这方面还不太熟,大家可以自行去学习学习。

代码:

import java.util.Arrays;
import java.util.Scanner;

//最小点覆盖或者树形dp都能做
//改了n遍代码一直过不去。。突然发现是读取数据时出问题了,我的num,也就是括号里的数只有一位。。
public class Main {
	static class edge{
		int v,next;				
	}
	static int p[]=new int[2000];
	static boolean vis[]=new boolean[2000];
	static edge e[]=new edge[5000];
	static int linked[]=new int[2000];
	static int n,eid;
	static void insert(int u,int v){
		e[eid].v=v;
		e[eid].next=p[u];
		p[u]=eid++;
	}
	static boolean match(int u){
		if(!vis[u]){
			vis[u]=true;
			for(int i=p[u];i!=-1;i=e[i].next){
				int v=e[i].v;
				if(!vis[v]){
					vis[v]=true;
					if(linked[v]==-1||match(linked[v])){
						linked[v]=u;
						//linked[u]=v;
						return true;
					}
				}
			}
		}
		return false;
	}
	static int hungry(){
		int ans=0;
		Arrays.fill(linked, -1);
		for(int i=0;i<n;i++){
			Arrays.fill(vis, false);
			if(match(i)){
				ans++;
			}
		}
		return ans;
	}
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			n=sc.nextInt();
			Arrays.fill(p, -1);
			eid=0;
			for(int i=0;i<e.length;i++){
				e[i]=new edge();
			}
			for(int i=0;i<n;i++){
				String s=sc.next();
				String goal[]=s.split(":");
				int u=Integer.valueOf(goal[0]);
				//int num=goal[1].charAt(1)-'0';//!!!!!!!num不一定是个位数,不能这么求
				//应该这么求num
				int num=Integer.parseInt(goal[1].substring(1, goal[1].length()-1));
				for(int j=0;j<num;j++){
					int v=sc.nextInt();
					insert(u,v);
					insert(v,u);					
				}
			}
			System.out.println(hungry()/2);
		}
		sc.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值