COURSES(poj 1249)

题意:

给你p门课程和n个学生,一个学生可以选0门,1门,或者多门课程,现在要求一个由p个学生组成的集合,满足下列2个条件:

1.每个学生选择一个不同的课程

2.每个课程都有不同的代表

如果满足,就输出YES

否则,输出NO

#include<cstdio>
#include<iostream>
#include<cstring>
#define M 310
using namespace std;
int used[M],belong[M],a[M][M],p,n;
int find(int i)
{
    for(int j=1;j<=n;j++)
      if(!used[j]&&a[i][j])
      {
          used[j]=1;
          if(!belong[j]||find(belong[j]))
          {
              belong[j]=i;
              return 1;
        }
      }
    return 0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(a,0,sizeof(a));
        memset(belong,0,sizeof(belong));
        memset(used,0,sizeof(used));
        scanf("%d%d",&p,&n);
        for(int i=1;i<=p;i++)
        {
            int x,y;
            scanf("%d",&x);
            for(int j=1;j<=x;j++)
            {
                scanf("%d",&y);
                a[i][y]=1;
            }
        }
        int tot=0;
        for(int i=1;i<=p;i++)
          if(find(i))
          {
              memset(used,0,sizeof(used));
              tot++;
          }
        if(tot==p)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

时间: 2024-08-10 23:29:18

COURSES(poj 1249)的相关文章

Selecting Courses POJ - 2239(我是沙雕吧 按时间点建边 || 匹配水题)

呃呃呃呃呃 把每个课给了INF个容量....我是沙雕把....emm....这题就是做着玩...呃呃呃别当真.... #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #inclu

poj 1469 COURSES 解题报告

题目链接:http://poj.org/problem?id=1469 题目意思:略 for 循环中遍历的对象要特别注意,究竟是遍历课程数P 还是 学生数N,不要搞混! 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 300 + 5; 7 int match[maxn], map[maxn][maxn];

poj 1469 COURSES (二分匹配)

COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16877   Accepted: 6627 Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is poss

poj 1469 COURSES(匈牙利算法模板)

http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19419   Accepted: 7642 Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is

POJ 2239 Selecting Courses(二分匹配)

题目链接:http://poj.org/problem?id=2239 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Ming is a student who loves study every much, and at the beginning

poj 1469 COURSES(二分匹配模板)

题目链接:http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17135   Accepted: 6730 Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your tas

POJ 1469 COURSES (二分图最大匹配 匈牙利算法)

COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18892   Accepted: 7455 Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is poss

POJ 2239 Selecting Courses【最大匹配】

大意: 有一些课程,每个课程的上课时间在每周可能会有多节,你可以从中任选一个时间去听课, 每周七天一天12节课  告诉你每节课的上课时间问在不冲突的情况下最多上几节课? 分析: 左集合课程 右集合时间 边为该节课对应的上课时间 求最大匹配就行了 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std

poj COURSES(二分匹配)

题目链接:http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17135   Accepted: 6730 Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your tas