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.pswp.cn/news/250610.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/250610.shtml
英文地址,請注明出處:http://en.pswp.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