TZOJ 3030 Courses(二分图匹配)

描述

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

样例输入

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

样例输出

YES
NO

题意

有n个学生和p门课,每门课有对应的学生要求,判断能否选出p个学生刚好上p门课

题解

一道很裸的二分图匹配题,刚好拿来熟悉下算法

这里用匈牙利算法,判断p门课程是否都能成功匹配

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N=305,P=105;
 5 int G[P][N],vis[N],match[P];
 6 int n,p;
 7 int Find(int u)
 8 {
 9     for(int i=1;i<=n;i++)
10     {
11         if(G[u][i]&&!vis[i])
12         {
13             vis[i]=1;
14             if(!match[i]||Find(match[i]))
15             {
16                 match[i]=u;
17                 return 1;
18             }
19         }
20     }
21     return 0;
22 }
23 int main()
24 {
25     int k,t,stu;
26     cin>>t;
27     while(t--)
28     {
29         memset(G,0,sizeof(G));
30         cin>>p>>n;
31         for(int i=1;i<=p;i++)
32         {
33             cin>>k;
34             for(int j=0;j<k;j++)
35             {
36                 cin>>stu;
37                 G[i][stu]=1;
38             }
39         }
40         int flag=1;
41         memset(match,0,sizeof(match));
42         for(int i=1;i<=p;i++)
43         {
44             memset(vis,0,sizeof(vis));
45             if(!Find(i))
46             {
47                 flag=0;break;
48             }
49         }
50         printf("%s\n",flag?"YES":"NO");
51     }
52 }

转载于:https://www.cnblogs.com/taozi1115402474/p/8724297.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/250610.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue --- configureWebpack模拟后台数据

初识 使用vue/cli搭建的项目可以在vue.config.js中,模拟一个后台(express写法)vue.config.js configureWebpack: {devServer: {// 模拟后台服务器 express写法before(app) {app.get(/api/login, function(req, res) {const { username, passwd } req.query;console.log(user…

TCP和UDP的优缺点及区别

转自&#xff1a;http://www.cnblogs.com/xiaomayizoe/p/5258754.html TCP的优点&#xff1a; 可靠&#xff0c;稳定 TCP的可靠体现在TCP在传递数据之前&#xff0c;会有三次握手来建立连接&#xff0c;而且在数据传递时&#xff0c;有确认、窗口、重传、拥塞控制机制&#xff…

e.getMessage 为空NULL

e.getMessage 为空NULL 在日常代码中免不了要try catch 切忌用try catch 去try 整个方法。 在对象操作之前尽量写上if 空判断。 反例&#xff1a; public void send(){ try{ 代码1&#xff1a;获取对象 代码2&#xff1a;操作代码1 代码3&#xff1a;操作代码2 代码4&#xff1…

Linux:客户端的实现

写了一个简单的服务器软件&#xff0c;但是没有写客户端。现在我将客户端实现了&#xff0c;其实昨天已经说了客户端的实现步骤了。 步骤&#xff1a; socket() 初始化 connet()链接 从标准输入读数据fgets() 传数据到服务器write() 读从服务器返回的数据read() 写数据到屏幕上…

vue --- http拦截,登录登出的逻辑设计

设计 在src目录下创建一个interceptor.js登录逻辑 设置拦截,在发起请求前,先判断用户是否登录(在本栗中,即是否能够在浏览器缓存中找到token). 登出逻辑 对服务端传过来的数据进行拦截,判断其状态码是否为401(未登录或token过期)清空浏览器缓存中的token重定向到登入页面 inte…

循环分支循环语句

# 三大结构 - 循环 - 分支 - 循环 . . .In [ ]:# 分支 - 分支的基本语法 - if 条件表达式&#xff1a; 语句1 语句2 语句3 ..... - 条件表达式就是计算结果必须是布尔值的表达式 - 表达式后面的冒号觉对不能少 - 注意 if 后面出现的语句&#xff0c;如果属于 if 语句块&…

HTTP 1.1与HTTP 1.0的比较

HTTP 1.1与HTTP 1.0的比较 一个WEB站点每天可能要接收到上百万的用户请求&#xff0c;为了提高系统的效率&#xff0c;HTTP 1.0规定浏览器与服务器只保持短暂的连接&#xff0c;浏览器的每次请求都需要与服务器建立一个TCP连接&#xff0c;服务器完成请求处理后立即断开TCP连接…

vue --- 前端代理发送http请求

后端 端口在3000使用jsonwebtoken和koa-jwt生成令牌并返回对’/api/userinfo’端口,先验证令牌是否通过,若通过返回数据 const Koa require(koa); const Router require(koa-router); // 生成令牌、验证令牌 const jwt require(jsonwebtoken); const jwtAuth require(koa…

python全栈开发-json和pickle模块(数据的序列化)

一、什么是序列化&#xff1f; 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化&#xff0c;在Python中叫pickling&#xff0c;在其他语言中也被称之为serialization&#xff0c;marshalling&#xff0c;flattening等等&#xff0c;都是一个意思。 为什么要序列化…

Gale-Shapley---婚姻匹配算法算法

原文链接&#xff1a;http://blog.csdn.net/cscmaker/article/details/8291131 &#xff08;一&#xff09;问题的引出&#xff1a; 有N男N女&#xff0c;每个人都按照他对异性的喜欢程度排名。现在需要写出一个算法安排这N个男的、N个女的结婚&#xff0c;要求两个人的婚姻应该…

大数据排重

注意用来排重的那个集合放到Set中&#xff0c; 可以是HashSet,或者其他Set(推荐使用HashSet),因为Set的contains效率更高&#xff0c;比list高很多 -----------------------------------------------------------------------------------------------------------------------…

大前端成长路径

路径(持续更新): 以下是我不同时期的博客链接可以和我的GitHub共同食用大家可以对比一下,我学的过程是缓慢型的… learning: 0个月 2018年09月开始接触前端,前端三剑客一个不知道一个不懂,于是对着W3C、菜鸟教程.一个一个敲开始啃红宝书《JavaScript高级程序设计》(第3版) le…

工具:meson+ninja(安装问题解决)

问题1&#xff1a;Python版本问题 报错信息&#xff1a; NOTICE: You are using Python 3.6 which is EOL. Starting with v0.62.0, Meson will require Python 3.7 or newer ubuntu 18默认的python3是3.6. 解决方案1&#xff1a;从源码安装python 3.7 wget https://www.pyth…

ListMapSet的操作和遍历

List&Map&Set的操作和遍历 Java的三大集合即&#xff1a;Set、List、Map。 Set&#xff1a;代表无序、不可重复的集合&#xff0c;常用的有HashSet&#xff08;哈希表实现&#xff09;、TreeSet&#xff08;红黑树实现&#xff09;&#xff1b;List&#xff1a;代表有序…

PHP中的魔术方法

概述 在面向对象编程中&#xff0c;PHP提供了一系列的魔术方法&#xff0c;这些魔术方法为编程提供了很多便利。PHP中的魔术方法通常以__(两个下划线)开始&#xff0c;并且不需要显示的调用而是由某种特定的条件出发。这篇文章简单总结了PHP中提供的魔术方法。 开始之前 在总结…

执行caffe的draw_net.py出现“GraphViz's executable dot not found”的解决方法

执行caffe的draw_net.py出现“GraphVizs executable "dot" not found”的解决方法 控制台输入如下指令画网络图&#xff1a;python ../../../python/draw_net.py train.prototxt train.png --rankdirTB &#xff08;Top-Bottom形式&#xff0c;纵向图&#xff09;pyt…

配置 --- vscode自定义代码段Snippets

目标 在vscode中输入vbs-vue 然后产生一个自己想要的模板 写好模板 在线上写好模板传送门: https://snippet-generator.app/ 1是标题,对应 2是前缀.对应在vue中使用的快捷键 vbs-vue3就是需要显示的代码段了 在vscode中配置 1.ctrlshiftp2.选择 Preferences: Configure U…

centos6安装composer

需要使用到curl&#xff0c;没有的话需要 yum -y install curl ###安装一、下载&#xff1a;curl -sS https://getcomposer.org/installer | php &#xff08;如果是网络原因多试几次&#xff09; 二、移动composer.phar移动到环境下让其变成可执行&#xff1a;mv compose…

透明图与元素居中

1,定位让元素居中 1. 透明度 opacity 默认值是1 不透明 0是全透明转载于:https://www.cnblogs.com/Shinigami/p/9709382.html

配置 --- vscode中react格式化解决方案

选择右下角的语言 在弹出框搜react选择 JavaScript React(或者根据需求选择 TypeScript React) 快捷键, windows下 Alt SHIFT F