COURSES
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19458   Accepted: 7658

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 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

Input

Your program should read sets of data from the std input. The first line of the input 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抣l 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. 

Output

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.

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

选课。给了每一门课有那些学生选。要问的是在给定的关系的情况下,能不能求出一个学生集合,这个集合里面能够满足这样的关系:

1.每门课都能派出一个学生代表。

2.每个学生都代表了一门课。

不能更匈牙利了。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int grid[305][305];
int link[305];
int visit[305];
int n,k,V1,V2;
int result; bool dfs(int x)
{
int i;
for(i=1;i<=V2;i++)
{
if(grid[x][i]==1&&visit[i]==0)
{
visit[i]=1;
if(link[i]==-1||dfs(link[i]))
{
link[i]=x;
return true;
}
}
}
return false;
} bool Magyarors()
{
int i; result=0;
memset(link,-1,sizeof(link));//!!这里不能是0 for(i=1;i<=V1;i++)
{
memset(visit,0,sizeof(visit));
if(!dfs(i))
{
return false;
}
}
return true;
} int main()
{
int Test,i,j,num,temp;
cin>>Test; while(Test--)
{
memset(grid,0,sizeof(grid));
cin>>V1>>V2; for(i=1;i<=V1;i++)
{
scanf("%d",&num);
for(j=1;j<=num;j++)
{
scanf("%d",&temp);
grid[i][temp]=1;
}
}
if(V1>V2)
{
cout<<"NO"<<endl;
}
else
{
if(Magyarors())
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1469:COURSES的更多相关文章

  1. POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配

    两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...

  2. poj——2239 Selecting Courses

    poj——2239   Selecting Courses Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10656   A ...

  3. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  4. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  5. 二分图匹配 最大匹配数+最大点覆盖 POJ 1469+POJ 3041

    最大匹配数就等于最大点覆盖,因为在图里面,凡是要覆盖的点必定是连通的,而最大匹配之后,若还有点没有覆盖到,则必定有新的匹配,与最大匹配数矛盾,如果去掉一些匹配,则必定有点没有覆盖到. POJ 1469 ...

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

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

  7. HDU - 1083 Courses /POJ - 1469

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1083 http://poj.org/problem?id=1469 题意:给你P个课程,并且给出每个课 ...

  8. poj 1469 COURSES 解题报告

    题目链接:http://poj.org/problem?id=1469 题目意思:有 N 个人,P个课程,每一个课程有一些学生参加(0个.1个或多个参加).问 能否使得 P 个课程 恰好与 P 个学生 ...

  9. POJ 1469 COURSES 二分图最大匹配 二分图

    http://poj.org/problem?id=1469 这道题我绝壁写过但是以前没有mark过二分图最大匹配的代码mark一下. 匈牙利 O(mn) #include<cstdio> ...

随机推荐

  1. Unity游戏开发面试基础知识

    面试第一次知识总结: 一.Unity基本操作 1.unity提供哪几种光源? 点光源.平行光.聚光灯.区域光. 2.物体发生碰撞的必要条件什么? 两个物体必须有碰撞体Collider组件,一个物体上必 ...

  2. DNS 访问 Service【转】

    在 Cluster 中,除了可以通过 Cluster IP 访问 Service,Kubernetes 还提供了更为方便的 DNS 访问. kubeadm 部署时会默认安装 kube-dns 组件. ...

  3. chart 目录结构【转】

    chart 是 Helm 的应用打包格式.chart 由一系列文件组成,这些文件描述了 Kubernetes 部署应用时所需要的资源,比如 Service.Deployment.PersistentV ...

  4. delphi对ZIP解压

    Delphi 对GZIP解压 作者:admin 来源:未知 日期:2010/5/9 13:08:46 人气:获取失败 标签: QQ空间新浪微博腾讯微博腾讯朋友QQ收藏百度空间百度贴吧更多0 呵呵,终于 ...

  5. app1----攻防世界

    啥也不说把题目下载下来,在模拟器里运行一下 输入正确的key就是flag 继续下一步分析,可以使用Androidkiller分析,我喜欢使用jeb这里我就使用jeb进行分析 找到MainActivit ...

  6. 功耗极低非接触 13.56mhz读卡芯片:SI522

    众所周知13.56mhz是高频,一般用于防伪,做到成品非常薄.在智能门锁以及RFID读卡器是非常多人采用的,不管是在读卡距离.读卡灵敏度都是非常好的.现在智能门锁的竞争性很大,大多厂商及方案商都慢慢往 ...

  7. jmeter里面Dug Sampler 和json提取器的用法

    1.编写用户详情请求 2.查看结果树 一级一级往上查找父集 3.添加json提取器 步骤:点击[用户详情]请求->添加->后置处理器->json提取器 把查看结果树里面的JSON P ...

  8. linux 域名

    Linux 安装好后,其默认的主机名是 localhost.   1.修改 /etc/sysconfig/network  配置文件 vi  /etc/sysconfig/network 修改HOST ...

  9. [Android]如何导入已有的外部数据库

    转自:http://www.cnblogs.com/xiaowenji/archive/2011/01/03/1925014.html 我们平时见到的android数据库操作一般都是在程序开始时创建一 ...

  10. 6.安装telnet服务

    yum list telnet* 列出telnet相关的安装包 yum install telnet-server 安装telnet服务 yum install telnet.* 安装telnet客户 ...