(hdu step 6.3.1)Strategic Game(求用最少顶点数把所有边都覆盖,使用的是邻接表)

本文介绍了一种解决战略游戏中的最小防御策略问题的方法。玩家需要在一张形成树状结构的地图上部署最少数量的士兵来监视所有的道路。文章通过构建邻接表而非邻接矩阵的方式,实现了更高效的算法设计,采用最大匹配数除以二的方法来确定最小顶点覆盖数。

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

题目:

Strategic Game

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 110 Accepted Submission(s): 75
 
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
 
Recommend
JGShining


题目分析:

               二分图,求最小顶点覆盖,简单题。这道题需要注意的是,用邻接矩阵来做会超时,数组开得太大,要用

邻接表来做。

最小顶点覆盖 = 最大匹配数/2


代码如下:

/*
 * a.cpp
 *
 *  Created on: 2015年3月13日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 1501;
//bool map[maxn][maxn];//这个是使用邻接矩阵实现时所需要用到的数据结构
vector<int> map[maxn];//这个是使用邻接表实现时所需要用到的数据结构.这时候map[i]里面村的就是愿意和结点i匹配的结点集合了
bool useif[maxn];//用于标记某个节点是否已经匹配
int link[maxn];//link[i] = t .表示结点i匹配的结点是t


int n;//节点数

/**
 * 用邻接表实现的,判断结点t是否能找到匹配的节点
 */
bool can(int t){
	int i;

	int size = map[t].size();//获取元一个结点t匹配的结点的个数
	for(i = 0 ; i < size ; ++i){//遍历每一个愿意和结点t匹配的结点
		int index = map[t][i];//获取愿意和节点t匹配的结点的序号
		if(useif[index] == false){//如果该节点还没有匹配
			useif[index] = true;//将该节点标记为已经匹配
			if(link[index] == - 1 || can(link[index])){//如果该节点还没有匹配的结点||该匹配结点能找到其他的匹配结点
				link[index] = t;//那么僵该节点的匹配结点设置为t

				return true;//返回true,表示能够t找到匹配的结点
			}
		}
	}

	return false;//返回false,表示结点t无法找到匹配的节点
}


/**
 * 求最大匹配数
 */
int max_match(){
	int num = 0;

	int i;
	for(i = 0 ; i < n ; ++i){//遍历所有节点,求最大匹配数
		memset(useif,false,sizeof(useif));
		if(can(i) == true){
			num++;
		}
	}

	return num;
}


int main(){
	while(scanf("%d",&n)!=EOF){
//		memset(map,false,sizeof(map));//使用邻接矩阵实现求最大匹配数时,清空map的写法
		int i;

		for(i = 0 ; i < n ; ++i){//使用邻接矩阵求最大匹配数时,清空map的写法
			map[i].clear();
		}
		memset(link,-1,sizeof(link));

		int a,b;
		for(i = 0 ; i < n ; ++i){
			scanf("%d:(%d)",&a,&b);

			int c;
			int j;
			for(j = 0 ; j < b ; ++j){
				scanf("%d",&c);
//				map[a][c] = true;//邻接矩阵建立关系的写法
//				map[c][a] = true;
				/**
				 * 这道题和Grils And Boys 的区别就在于
				 * 如果0 (2): 1 2  --->这个表示0与1和2相互认识
				 * 在Grils And Boys中,0会出现在1和2的描述中如
				 * 1  (...): 0 ...
				 * 2  (...): 0 ...
				 *
				 * 但是这道题中0 (2): 1 2 表示的仅仅是0认识1和2
				 * 但1和2不一定认识0.
				 * 在做的时候我们要把它转换成相互认识来做
				 */
				map[a].push_back(c);//邻接表建立关系的写法
				map[c].push_back(a);
			}
		}

		//最小顶点覆盖 = 最大匹配数/2
		printf("%d\n",max_match()/2);
	}

	return 0;
}







好的,关于 HDU4992 所有原根的问题,我可以给出以下解答: 1. 首先,我们需要知道什么是原根。原根是指模 n 意义下,所有与 n 互质的数 a 的最小正整数 k 次幂对 n 取模的值覆盖了所有与 n 互质的数。简单来说,就是如果一个数 a 是模 n 意义下的原根,那么 a 的任何正整数次幂对 n 取模的值都不相同,且能覆盖所有与 n 互质的数。 2. 为了模 n 意义下的所有原根,我们需要先出与 n 互质的数的个数 phi(n)。phi(n) 可以使用欧拉函数出。 3. 然后,我们需要枚举模 n 意义下的所有数,判断它是否是原根。具体来说,对于每个 a,我们需要判断 a 的每个小于 phi(n) 的正整数次幂对 n 取模的值是否都不相同,且能覆盖所有与 n 互质的数。如果是,那么 a 就是模 n 意义下的原根。 4. 代码实现可以参考以下 Java 代码: ``` import java.util.*; public class Main { static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int phi(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res = res / i * (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res = res / n * (n - 1); } return res; } static int pow(int a, int b, int mod) { int res = 1; while (b > 0) { if ((b & 1) != 0) { res = res * a % mod; } a = a * a % mod; b >>= 1; } return res; } static boolean check(int a, int n, int phi) { for (int i = 1, j = pow(a, i, n); i <= phi; i++, j = j * a % n) { if (j == 1) { return false; } } return true; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int phi = phi(n); List<Integer> ans = new ArrayList<>(); for (int i = 1; i < n; i++) { if (gcd(i, n) == 1 && check(i, n, phi)) { ans.add(i); } } Collections.sort(ans); for (int x : ans) { System.out.print(x + " "); } System.out.println(); } } } ``` 其中,gcd 函数用于最大公约数,phi 函数用于欧拉函数,pow 函数用于快速幂模,check 函数用于判断一个数是否是原根。在主函数中,我们依次读入每个 n,出 phi(n),然后枚举模 n 意义下的所有数,判断它是否是原根,将所有原根存入一个 List 中,最后排序输出即可。 希望我的回答能够帮到你,如果你有任何问题,欢迎随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值