版权声明:小白有写的不好不对的地方请指出 https://blog.csdn.net/smoggyxhdz/article/details/88670797
历届试题 分考场
时间限制: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
样例输入
5
10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
样例输出
5
图的着色问题,np问题,没什么好办法,暴力就行了
#include<bits/stdc++.h>
using namespace std;
int mp[105][105];
int color[105];
int n,m;
bool judge(int cur,int col)
{
for(int i=1;i<=n;i++)
{
if(mp[cur][i]==1&&color[i]==col)
return false;
}
return true;
}
bool dfs(int cur,int col)
{
if(cur>n)
{
return true;
}
for(int i=1;i<=col;i++)
{
if(judge(cur,i))
{
color[cur]=i;
if(dfs(cur+1,col))
return true;
}
}
return false;
}
int main()
{
cin >> n >> m;
for(int i=0;i<m;i++)
{
int a,b;
cin >> a >> b;
mp[a][b]=1;
mp[b][a]=1;
}
for(int i=1;i<=n;i++)
{
memset(color,0,sizeof(color));
if(dfs(1,i))
{
cout << i << endl;
break;
}
}
return 0;
}