蓝桥杯 历届试题 分考场

问题描述

  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

思路:

暴力搜索教室数目,然后注意剪枝,用vector会超时。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=1e2+5;
int m,n;
int edge[maxn][maxn];
int num[maxn];
int cal[maxn][maxn];
int ans=0x3f3f3f3f;
void dfs (int x,int tnum)
{
    if(tnum>=ans) return;
    if(x>n)
    {
        ans=min(ans,tnum);
        return;
    }
    for (int i=0;i<tnum;i++)
    {
        int flag=0;
        int len=num[i];
        for (int j=0;j<len;j++)
        {
            if(edge[x][cal[i][j]]==0)
            {
                flag++;
            }
        }
        if(flag==len)
        {
            cal[i][num[i]++]=x;
            dfs(x+1,tnum);
            num[i]--;
        }
    }
    cal[tnum][num[tnum]++]=x;
    dfs(x+1,tnum+1);
    num[tnum]--;
}
int main()
{
    memset (num,0,sizeof(num));
    memset (cal,0,sizeof(cal));
    memset (edge,0,sizeof(edge));
    scanf("%d%d",&n,&m);
    while (m--)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        edge[x][y]=edge[y][x]=1;
    }
    dfs(1,0);
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/88019418
今日推荐