题目大意:一共有N个学生跟P门课程,一个学生可以任意选一门或多门课,问是否达成:

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

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

输入为:

  P N(课程数跟学生数)

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

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

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

  输出为:

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

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 possible to form a committee of exactly P students that satisfies simultaneously the conditions: 

. every student in the committee represents a different course (a student can represent a course if he/she visits that course) 

. each course has a representative in the committee 

Your program should read sets of data from a text file. The first line of the input file contains the number of the data sets. Each data set is presented in the following format: 

P N 
Count1 Student1 1 Student1 2 ... Student1 Count1 
Count2 Student2 1 Student2 2 ... Student2 Count2 
...... 
CountP StudentP 1 StudentP 2 ... StudentP CountP 

The first line in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence of the courses . from course 1 to course P, each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you'll find the Count i students, visiting the course, each two consecutive separated by one blank. Students are numbered with the positive integers from 1 to N. 

There are no blank lines between consecutive sets of data. Input data are correct. 

The result of the program is on the standard output. For each input data set the program prints on a single line "YES" if it is possible to form a committee and "NO" otherwise. There should not be any leading blanks at the start of the line. 

An example of program input and output:

Input

2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1

Output

YES
NO

Sample Input

2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1

Sample Output

YES
NO

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 310
int dis[310],vis[310],dp[310][310];
int n,m,t;
int find(int x)
{
for(int i=1;i<=n;i++)
{
if(!vis[i]&&dp[x][i])
{
vis[i]=1;
if(dis[i]==0||find(dis[i]))
{
dis[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
cin>>t;
while(t--)
{
int v,x;
cin>>m>>n;
int ans=0;
memset(dp,0,sizeof(dp));
memset(dis,0,sizeof(dis));
for(int i=1;i<=m;i++)
{
cin>>v;
while(v--)
{
cin>>x;
dp[i][x]=1;
}
}
for(int i=1;i<=m;i++)
{
memset(vis,0,sizeof(vis));
if(find(i))
ans++;
}
if(ans==m) printf("YES\n");
else printf("NO\n");
}
}

F - Courses (学生选课(匈牙利算法模板))的更多相关文章

  1. poj 1274 The Perfect Stall【匈牙利算法模板题】

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20874   Accepted: 942 ...

  2. 匈牙利算法模板 hdu 1150 Machine Schedule(二分匹配)

    二分图:https://blog.csdn.net/c20180630/article/details/70175814 https://blog.csdn.net/flynn_curry/artic ...

  3. hdu 2063 过山车 (最大匹配 匈牙利算法模板)

    匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最 ...

  4. 匈牙利 算法&模板

    匈牙利 算法 一. 算法简介 匈牙利算法是由匈牙利数学家Edmonds于1965年提出.该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法. 二分图的定义: 设G=(V,E)是一个 ...

  5. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  6. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...

  7. POJ:3041-Asteroids(匈牙利算法模板)

    传送门:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Description Bes ...

  8. POJ 1325 && 1274:Machine Schedule 匈牙利算法模板题

    Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12976   Accepted: 5529 ...

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

    http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

随机推荐

  1. php 去除路由中index.php 通过 .htaccess 文件

    首先在入口文件index.php得的当前目录下 创建 .htaccess文件. 然后将下面一段代码放进去: <IfModule mod_rewrite.c> RewriteEngine o ...

  2. 常用 .gitignore 模板

    前言 每次建项目的时候可以直接复制了,也算是方便自己,以后发现少的会更新 正文 作用 git提交时忽略文件 文件名 .gitignore Python # Byte-compiled / optimi ...

  3. kubernets之服务资源

    一  服务集群内部或者客户端与pod的通信桥梁   kubernets集群的内部pod访问为啥不能使用传统的IP:PORT的形式? pod是短暂的,它们会随时启动或者关闭,原因可能是pod所在的节点下 ...

  4. leetcode 940. 不同的子序列 II (动态规划 ,字符串, hash,好题)

    题目链接 https://leetcode-cn.com/problems/distinct-subsequences-ii/ 题意: 给定一个字符串,判断里面不相同的子串的总个数 思路: 非常巧妙的 ...

  5. LeetCode-P53题解【动态规划】

    本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: https://leetcode.com/problems/maximum-subarray/ ...

  6. 入门OJ:Coin

    题目描述 你有n个硬币,第i硬币面值为ai,现在总队长想知道如果丢掉了某个硬币,剩下的硬币能组成多少种价值?(0价值不算) 输入格式 第一行一个整数n 第二行n个整数.,a1,a2-an. 1< ...

  7. 常用的hadoop和yarn的端口总结

    节点 默认端口 用途说明 HDFS DataNode 50010 datanode服务端口,用于数据传输 50075 http服务的端口 50475 https服务的端口 50020 ipc服务的端口 ...

  8. jmeter-登录获取cookie后参数化,或手动添加cookie, 再进行并发测试

    以下情况其实并不适用于直接登录可以获取cookie情况,直接可以登录成功,直接添加cookie管理,cookie可以直接使用用于以下请求操作. 如果登录一次后,后续许多操作,可以将cookie管理器放 ...

  9. Boyer-Moore 投票算法

    Boyer-Moore 投票算法 http://theory.stanford.edu/~trevisan/cs154-12/notestream.pdf 众数

  10. LeetCode上并发题目无Go版本:台湾同胞试水 — 交替打印FooBar

    https://mp.weixin.qq.com/s/I5va3PI1oGIj8R_n3Nw2yw