历届试题-分考场[蓝桥杯][暴力dfs][着色原理]

6 篇文章 0 订阅

分考场[暴力dfs]

time limit per testmemory limit per testinputoutput
1 seconds256 megabytesstandard inputstandard output

Description:

n个人参加某项特殊考试。
为了公平,要求任何两个认识的人不能分在同一个考场。
求是少需要分几个考场才能满足条件。

Input

第一行,一个整数n(1<n<100),表示参加考试的人数。
第二行,一个整数m,表示接下来有m行数据
以下m行每行的格式为:两个整数a,b,用空格分开 (1<=a,b<=n) 表示第a个人与第b个人认识。

Output

一行一个整数,表示最少分几个考场

One Example input

5
8
1 2
1 3
1 4
2 3
2 4
2 5
3 4
4 5

One Example output

4

Two Example input

5
10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

Two Example output

5

分析:
题意:
暴力 d f s dfs dfs,不行再回溯
有一个我不理解的是。。数组开 100 + 100+ 100+ r e re re,开 200 + 200+ 200+就过了,但是他不是只到 100 100 100吗(╯‵□′)╯︵┻━┻

Code:

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long ll;

const int maxn = 200 + 5;
const int inf = 0x3f3f3f3f;

bool mp[maxn][maxn];
int linker[maxn][maxn]; // 第i个房间有cnt[i]个人,每个人是j
int cnt[maxn]; // 每个房间几个人
int ans, n;

void dfs(int u, int num) { // 编号 房间个数
    if(num >= ans)  return ;
    if(u == n + 1) {
        ans = min(ans, num);
        return ;
    }
    for(int i = 0; i < num; ++i) { // 遍历房间
        int tmp = 0; // 可以放一起的人
        for(int j = 0; j < cnt[i]; ++j)
            if(!mp[u][linker[i][j]])
                tmp++;
        if(tmp == cnt[i]) {
            linker[i][cnt[i]++] = u;
            dfs(u + 1, num);
            linker[i][--cnt[i]] = 0;
        }
    }
    linker[num][0] = u;
    cnt[num]++;
    dfs(u + 1, num + 1);
}

int main() {
    int m, u, v;
    scanf("%d%d", &n, &m);
    ans = n;
    memset(cnt, 0, sizeof(cnt));
    memset(mp, false, sizeof(mp));
    while(m--) {
        scanf("%d%d", &u, &v);
        mp[u][v] = mp[v][u] = true;
    }
    dfs(1, 0);
    printf("%d\n", ans);
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值