历届试题 分考场

/**

  • 试题 历届试题 分考场

资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
  n个人参加某项特殊考试。
  为了公平,要求任何两个认识的人不能分在同一个考场。
  求是少需要分几个考场才能满足条件。
输入格式
  第一行,一个整数n(1<n<100),表示参加考试的人数。
  第二行,一个整数m,表示接下来有m行数据
  以下m行每行的格式为:两个整数a,b,用空格分开 (1<=a,b<=n) 表示第a个人与第b个人认识。
输出格式
  一行一个整数,表示最少分几个考场。
样例输入
5
8
1 2
1 3
1 4
2 3
2 4
2 5
3 4
4 5
样例输出
4
/
package hard;
import java.util.
;
import java.math.*;
public class 分考场 {

public static void main(String[] args) {
	Scanner in =new Scanner(System.in);
	number=in.nextInt();
	int m=in.nextInt();
	init();
	for(int i=0;i<m;i++) {
		int a=in.nextInt();
		int b=in.nextInt();
		result[a][b]=false;
		result[b][a]=false;
}
	dfs(1,0);
	System.out.println(ans);
}
static int number=0;
static int ans=100;
static int[][] cla=new int[101][101];
static boolean[][] result=new boolean[101][101];
public static void init() {
	for(int i=0;i<result.length;i++) {
		for(int j=0;j<result.length;j++) {
			cla[i][j]=0;
			result[i][j]=true;
		}

	}
}
public static void dfs(int id,int room) {
	if(room>=ans) {
		return;
	}
	if(id==number+1) {
		ans=Math.min(ans, room);
		return;
	}
	int i=0;
	for(i=1;i<=room;i++) {	
		int j=0;
		while(cla[i][j]!=0&&result[id][cla[i][j]]==true) {//找房间
			j++;
		}
		if(cla[i][j]==0) {
			cla[i][j]=id;
			dfs(id+1,room);
			cla[i][j]=0;
		}
		
	}
	cla[i][0]=id;
	dfs(id+1,room+1);
	cla[i][0]=0;
}

}
还不是特别了解dfs,还是得多多练习
参考:https://blog.csdn.net/qq_41923622/article/details/80405277

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值