题意:N个学生,P个课程,问能不能找到课程的P个匹配。

思路:【早上睡醒了再写】

代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = ;
int n, p;
vector<int> g[maxn];
int from[maxn], tot;
bool use[maxn]; bool match(int x)
{
for (int i = ; i < g[x].size(); i ++) {
if(!use[g[x][i]]) {
use[g[x][i]] = true;
if(from[g[x][i]] == - || match(from[g[x][i]])) {
from[g[x][i]] = x;
return true;
}
}
}
return false;
} int hungary()
{
tot = ;
memset(from, -, sizeof(from));
for(int i = ; i <= n; i ++) {
memset(use, , sizeof(use));
if(match(i)) tot ++;
}
return tot;
} int main()
{
int t; cin>>t;
while(t--) {
for(int i = ; i < maxn; i++) g[i].clear();
scanf("%d%d", &p, &n);
for(int i = ; i <= p; i++) {
int k; scanf("%d", &k);
for(int j = ; j < k; j++) {
int a; scanf("%d", &a);
g[i].push_back(a);
}
}
int res = hungary();
//cout<<res<<endl;
if(res == p) printf("YES\n");
else printf("NO\n");
}
}

poj-1469-COURSES-二分图匹配-匈牙利算法(模板)的更多相关文章

  1. POJ1469 COURSES 二分图匹配 匈牙利算法

    原文链接http://www.cnblogs.com/zhouzhendong/p/8232649.html 题目传送门 - POJ1469 题意概括 在一个大矩阵中,有一些障碍点. 现在让你用1*2 ...

  2. USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

    The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...

  3. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

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

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

  5. poj 1469 COURSES (二分图模板应用 【*模板】 )

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18454   Accepted: 7275 Descript ...

  6. P3386 【模板】二分图匹配(匈牙利算法)

    题目背景 二分图 题目描述 给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数 输入输出格式 输入格式: 第一行,n,m,e 第二至e+1行,每行两个正整数u,v,表示u,v有一条连边 ...

  7. [洛谷P3386] [模板] 二分图匹配 (匈牙利算法)

    题目传送门 毒瘤出题人zzk出了个二分图匹配的题(18.10.04模拟赛T2),逼我来学二分图匹配. 网络流什么的llx讲完之后有点懵,还是匈牙利比较好理解(绿与被绿). 对于左边的点一个一个匹配,记 ...

  8. luogu3386 【模板】二分图匹配 匈牙利算法 hdu2063 过山车 dinic

    luogu 匈牙利算法 #include <iostream> #include <cstring> #include <cstdio> using namespa ...

  9. Codevs 1222 信与信封问题 二分图匹配,匈牙利算法

    题目: http://codevs.cn/problem/1222/ 1222 信与信封问题   时间限制: 1 s   空间限制: 128000 KB   题目等级 : 钻石 Diamond 题解 ...

  10. (转)二分图匹配匈牙利算法与KM算法

    匈牙利算法转自于: https://blog.csdn.net/dark_scope/article/details/8880547 匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名 ...

随机推荐

  1. URAL 1920 Titan Ruins: the Infinite Power of Magic

    大意: 有一张N*N的网格,你每次可以走一步,每格只能走一次,有没有一种方法让走了L步后回到一个距原点1步远的格子?  没有输出Unsuitable device,否则输出Overwhelming p ...

  2. ExtJS登陆页面涉及到的几个问题

    1.如何在文本框中增加提示信息      输入框中无法直接使用tooltip,需要使用单独的代码 var tip = Ext.create('Ext.tip.ToolTip', { target : ...

  3. zoj 1450 Minimal Circle 最小覆盖圆

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=450 You are to write a program to fi ...

  4. 【BZOJ】【1492】【NOI207】货币兑换Cash

    DP/CDQ分治 orz Hzwer copy了下他的代码……结果在while(j<top......)这一句中把一个括号的位置打错了……找了我一个多小时才找到TAT 很神奇……顺便贴下CDQ的 ...

  5. STMPClient 发送邮件显示 不允许使用邮件名称.

      在.net 2.0,3.5, 针对某些邮箱(还不清楚是什么样的邮件) , 使用微软自带的DLL发送邮件会提示不允许使用邮件名称 .... 使用Jmail可以发送.     解决方案:     1. ...

  6. IP分片浅析

    在TCP/IP分层中,数据链路层用MTU(Maximum Transmission Unit,最大传输单元)来限制所能传输的数据包大小,MTU是指一次传送的数据最大长度,不包括数据链路层数据帧的帧头, ...

  7. SQLServer中查询的数字列前面补0返回指定长度的字符串

    SQLServer中查询的数字列前面补0返回指定长度的字符串: 如: 角本如下: /****** Script for SelectTopNRows command from SSMS ******/ ...

  8. YARN-RPC

    运行在YARN平台上面的RPC. 当前存在非常多的开源RPC框架,比较著名的有Thrift.Protocol Buffers 和 AVRO.他们均有两部分构成:对象序列化和远程过程调用. 重要类: Y ...

  9. Sqli-labs less 41

    Less-41 此处与less-39是一致的,区别在于41错误不回显.所以我们称之为盲注. Payload: http://192.168.11.189/sqli-labs/Less-41/index ...

  10. Extjs整体加载树节点

    Ext.onReady(function () {             Ext.define('company', {                 extend: 'Ext.data.Mode ...