NYOJ 1291 不是匹配 【二分图匹配】

不是匹配

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
 
描述
 有N个人,N个活动, 每个人只会对2个或者3个活动感兴趣,
 每个活动也只有两个人或者两个活动对它兴趣,每个人参加一个
 感兴趣的活动需要一天 ,且当天该活动被参加时,其他的人不能参加
 如果每个人都参加完自己有兴趣的活动,应当怎样安排使得所用总天数时间最短
2<= N <=1000, 1<=m<=1000;

 
输入
一个数T 表示T 组数据
每组一个N表示人数,编号1 -- N , 一个数 m ,接下来m 行每个两个数
x,y, 表示第 x 个人对第y个活动感兴趣
输出
每组输出一个整数,表示最少天数
样例输入
1
3 6 
1 1 
1 2 
2 2
2 3 
3 1 
3 3 
样例输出
2 
来源
某校校赛
上传者

MQLYES

题意坑死我了,我以为每个活动可以两个人参见。。。。。

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
bool Link[1005][1005], vis[1005];
int Match[1010], n, m;
bool Dfs(int x) {
    for (int i = 0; i <= n; i++) {
        if (Link[x][i] && !vis[i]) {
            vis[i] = true;
            if (!Match[i] || Dfs(Match[i])) {
                Match[i] = x; return true;
            }
        }
    }
    return false;
}
int main() {
    int t, a, b;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        memset(Link, false, sizeof(Link));
        for (int i = 0; i < m; i++) {
            scanf("%d%d", &a, &b);
            Link[a][b] = true;
        }
        int cnt = 1, ans = 0;
        while (cnt <= m) {
            ans++;
            memset(Match, 0, sizeof(Match));
            for (int i = 1; i <= n; i++) {
                memset(vis, 0, sizeof(vis));
                if (Dfs(i)) cnt++;

            }
            for (int i = 1; i <= n; i++) {
                if (Match[i]) {
                    Link[Match[i]][i] = false;
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

 



 

posted @ 2016-08-20 21:20  zprhhs  阅读(115)  评论(0编辑  收藏  举报
Power by awescnb