POJ1469 COURSES

一.原题链接:http://poj.org/problem?id=1469

二.题目大意:有P个课程,N个学生,问能不能在每个课程都有课代表的情况下,保证每个是课代表的学生代表不同的课。(可以有学生不是课代表,英语不好读错题)

三,思路:二分匹配裸题,不能再裸了。图都直接给了啊。

四.代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cstdlib>

using namespace std;

const int INF = 0x3f3f3f3f,
          MAX_SIZE = 300;

class maxMatch
{
public:
    bool connected[MAX_SIZE][MAX_SIZE],
         visited[MAX_SIZE];
    int xMatch[MAX_SIZE], yMatch[MAX_SIZE],
        xNum, yNum;

    void init()
    {
        xNum = yNum = 0;
        memset(connected, 0, sizeof(connected));
        memset(xMatch, -1, sizeof(xMatch));
        memset(yMatch, -1, sizeof(yMatch));
    }

    bool path(int u)
    {
        int v;
        for(v = 1; v <= yNum; v++)
        if(connected[u][v] && !visited[v]){
            visited[v] = true;
            if(-1 == yMatch[v] || path(yMatch[v])){
                yMatch[v] = u;
                xMatch[u] = v;
                return true;
            }
        }
        return false;
    }

    int getRes()
    {
        int i, res = 0;
        for(i = 1; i <= xNum; i++)
        if(-1 == xMatch[i]){
            memset(visited, 0, sizeof(visited));
            if(path(i))
                res++;
        }
        return res;
    }

}G;

// student: y;
// course : x;

int main()
{
    //freopen("in.txt", "r", stdin);
    int test, i, j, stuId, num;
    scanf("%d", &test);
    while(test--){
        G.init();
        scanf("%d%d", &G.xNum, &G.yNum);
        for(i = 1; i <= G.xNum; i++){
            scanf("%d", &num);
            for(j = 1; j <= num; j++){
                scanf("%d", &stuId);
                G.connected[i][stuId] = true;
            }
        }

        if(G.getRes() != G.xNum)
            printf("NO\n");
        else
            printf("YES\n");
    }
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值