poj-1469-COURSES-二分图匹配-匈牙利算法(模板)
题意: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-二分图匹配-匈牙利算法(模板)的更多相关文章
- POJ1469 COURSES 二分图匹配 匈牙利算法
原文链接http://www.cnblogs.com/zhouzhendong/p/8232649.html 题目传送门 - POJ1469 题意概括 在一个大矩阵中,有一些障碍点. 现在让你用1*2 ...
- USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)
The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...
- HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))
Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...
- poj 1469 COURSES (二分图模板应用 【*模板】 )
COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18454 Accepted: 7275 Descript ...
- P3386 【模板】二分图匹配(匈牙利算法)
题目背景 二分图 题目描述 给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数 输入输出格式 输入格式: 第一行,n,m,e 第二至e+1行,每行两个正整数u,v,表示u,v有一条连边 ...
- [洛谷P3386] [模板] 二分图匹配 (匈牙利算法)
题目传送门 毒瘤出题人zzk出了个二分图匹配的题(18.10.04模拟赛T2),逼我来学二分图匹配. 网络流什么的llx讲完之后有点懵,还是匈牙利比较好理解(绿与被绿). 对于左边的点一个一个匹配,记 ...
- luogu3386 【模板】二分图匹配 匈牙利算法 hdu2063 过山车 dinic
luogu 匈牙利算法 #include <iostream> #include <cstring> #include <cstdio> using namespa ...
- Codevs 1222 信与信封问题 二分图匹配,匈牙利算法
题目: http://codevs.cn/problem/1222/ 1222 信与信封问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 ...
- (转)二分图匹配匈牙利算法与KM算法
匈牙利算法转自于: https://blog.csdn.net/dark_scope/article/details/8880547 匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名 ...
随机推荐
- CSS样式补充代码
CSS符号属性: list-style-type:none; /*不编号*/ list-style-type:decimal; /*阿拉伯数字*/ list-style-type:lower-roma ...
- Leetcode#151 Reverse Words in a String
原题地址 将单词按空格分词,然后倒序拼接即可 代码: void reverseWords(string &s) { vector<string> words; ; ; ; i &l ...
- UML架构(转载)
任何真正的世界系统是由不同的用户使用.用户可以是开发人员,测试人员,商务人士,分析师和等等.所以在设计一个系统的体系结构是用不同的角度心态.最重要的部分是从不同的观看者的角度来看,以可视化的系统.我们 ...
- 一道PK赛题
Problem Description I think that you might have played the traditional Chinese ring game: The Chines ...
- Hbase的连接池--HTablePool被Deprecated之后
说明: 最近两天在调研HBase的连接池,有了一些收获,特此记录下来. 本文先将官方文档(http://hbase.apache.org/book.html)9.3.1.1节翻译,方便大家阅读,然 ...
- POJ 3258 River Hopscotch (binarysearch)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5193 Accepted: 2260 Descr ...
- .NET中JSON的序列化和反序列化
.NET 中有两种方法进行JSON的操作分别需要引用不同的命名空间 1.System.Runtime.Serialization.Json(System.Runtime.Serialization.d ...
- eclipse编译项目jar,git管理代码
2015-3-10 settings.xml文件: <localRepository>C:\Users\xxx\.m2\repository</localRepository> ...
- Sqli-labs less 34
Less-34 本关是post型的注入漏洞,同样的也是将post过来的内容进行了 ' \ 的处理.由上面的例子可以看到我们的方法就是将过滤函数添加的 \ 给吃掉.而get型的方式我们是以url形式提交 ...
- 远程数据源Combobox
Ext.define('bookInfo', { extend: 'Ext.data.Model',//新类继承自model fields: [{ name: 'b ...