C - Courses - hdu 1083(模板)

一共有N个学生跟P门课程,一个学生可以任意选一

门或多门课,问是否达成:

1.每个学生选的都是不同的课(即不能有两个学生选同一门课)

2.每门课都有一个代表(即P门课都被成功选过)

输入为:

P N(课程数跟学生数)

接着有P行,格式为Count studenti studenti+1 ……studentcount

(Count表示对课程1感兴趣的学生数,接着有Count个学生)

如第一行2 1 2表示学生1跟学生2对课程1感兴趣

输出为:

若能满足上面两个要求这输出”YES”,否则为”NO”

注意:是课程匹配的学生,学生没课上没事.....

********************************************************************

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int MAXN = 1005;
const int oo = 1e9;

bool G[MAXN][MAXN], used[MAXN];
int p[MAXN], N, M;///n门课,M个学生

bool Find(int u)
{
    for(int i=1; i<=M; i++)
    {
        if(G[u][i] && used[i] == false)
        {
            used[i] = true;
            if(p[i] == false || Find(p[i]))
            {
                p[i] = u;
                return true;
            }
        }
    }

return false;
}

int main()
{
    int T;

scanf("%d", &T);

while(T--)
    {
        int i, v, Q;

memset(G, 0, sizeof(G));
        memset(p, 0, sizeof(p));

scanf("%d%d", &N, &M);

for(i=1; i<=N; i++)
        {
            scanf("%d", &Q);

while(Q--)
            {
                scanf("%d", &v);
                G[i][v] = true;
            }
        }

for(i=1; i<=N; i++)
        {
            memset(used, false, sizeof(used));
            if( Find(i) == false )
                break;
        }

if(i <= N)
            printf("NO\n");
        else
            printf("YES\n");
    }

return 0;

}

时间: 2024-12-25 10:30:22

C - Courses - hdu 1083(模板)的相关文章

Hdu 1083 Courses(匈牙利算法模版题)

Hdu 1083 题意:给你一个p表示测试组数,给你n和m表示课的个数和学生的个数,接下来n行首数字i表示该堂课的学生代表人数,之后为i个学生编码,问能否为每堂课找到一个学生课代表且不冲突: 题解:匈牙利算法模版 另附简单易懂匈牙利算法讲解:传送门 #include<cstring> #include<cstdio> const int N =305; using namespace std; bool h[N][N]; bool vis[N]; int link[N]; int

HDU 1083 裸的二分匹配

Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3424    Accepted Submission(s): 1650 Problem Description Consider a group of N students and P courses. Each student visits zero, one or

hdu 1083 最大匹配

题意:N个学生 P 个课程  求最大匹配 3 3 //学生 课程 3 1 2 3 //课程1 匹配学生1 2 3 2 1 2 1 1典型的匹配没什么好说的 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include<iostream

hdu 1083 Courses(二分图匹配)

Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4278    Accepted Submission(s): 2036 Problem Description Consider a group of N students and P courses. Each student visits zero, one or

hdu 1083 Courses

描述:有p门的课,每门课都有若干学生,现在要为每个课程分配一名课代表,每个学生只能担任一门课的课代表,如果每个课都能找到课代表,则输出"YES",否则"NO". 二分匹配 #include<iostream> #include<cstring> #define maxn 305 using namespace std; int c,s; int rem[maxn][maxn]; int visit[maxn],map[maxn]; int d

hdu 1083 匈牙利算法模板题

题意:有p个课程,n个学生,每个课程有x个学生喜欢.如果每个课程都至少可以分配一个学生就输出YES,否则输出NO. 题解:匈牙利算法裸题. 代码: #include <cstdio>#include <cstring>#define N 400int Map[N][N],S[N],vis[N],n;bool dfs(int u){    for(int i=1;i<=n;i++){        if(Map[u][i]&&!vis[i]){         

HDU 1083 Courses(最大匹配模版题)

题目大意: 一共有N个学生跟P门课程,一个学生可以任意选一 门或多门课,问是否达成: 1.每个学生选的都是不同的课(即不能有两个学生选同一门课) 2.每门课都有一个代表(即P门课都被成功选过) 输入为: 第一行一个T代表T组数据 P N(P课程数, N学生数) 接着P行: 第几行代表第几门课程,首先是一个数字k代表对这门课程感兴趣的同学的个数,接下来是k个对这门课程感兴趣同学的编号. 输出为: 若能满足上面两个要求这输出”YES”,否则为”NO” 注意:是课程匹配的学生,学生没课上没事.....

HDU - 1083 Courses (二分图最大匹配模板)

题意:给出P门课程,N个学生.每一门课程可能有多个学生感兴趣然后我们需要匹配,使得每一门课程都只包含一名对其感兴趣的学生问:能否匹配成立思路:这个就是典型的二分图匹配问题.常用匈牙利算法 完整代码:(一开始写成了无向图....)写成有向图是因为学生是可以剩余的 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; c

HDU 1083 网络流之二分图匹配

http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #include<cstdio> #include<cstring> #define maxm 410 using namespace std; int p,n; int master[maxm]; int linking[maxm][maxm]; int has[maxm]; int sol