poj 1469 COURSES
来源:互联网 发布:skrillex用什么软件 编辑:程序博客网 时间:2024/04/30 07:43
题意:给出p个课程,n个学生,问能否满足每个学生选一门不同的课,且每个课程有一个学生。
分析:把这个问题看成二分图来做的话,简直就是裸题~。求出最大匹配看是否和p相等,相等输出YES,否则输出NO。
第一道二分图,用了匈牙利算法求出了最大匹配。
以下附上代码:
#include <algorithm>#include <iostream>#include <sstream>#include <fstream>#include <cstring>#include <cstdio>#include <vector>#include <cctype>#include <cmath>#include <stack>#include <queue>#include <list>#include <map>#include <set>using namespace std;typedef long long ll;typedef unsigned long long ull;const int maxn = 305;int g[maxn][maxn];int p,n;int t;int cx[maxn],cy[maxn];//cx->student cy->coursebool vis[maxn];// void input(){ int i,j; int k; int v; for(i = 0; i < maxn; i++){ for(j = 0; j < maxn; j++){ g[i][j] = 0; } } scanf("%d%d",&p,&n); for(i = 1; i <= p; i++){ scanf("%d",&k); for(j = 0; j < k; j++){ scanf("%d",&v); g[v][i] = 1; } } }//寻找可增广轨增广 int path(int u){ for(int v = 1; v <= p; v++){ if(g[u][v] && !vis[v]){ vis[v] = 1; if(cy[v] == -1 || path(cy[v])){ cx[u] = v; cy[v] = u; return 1;//找到可增广轨 } } } return 0;//不存在可增广轨 }//匈牙利算法 int maxMatch(){ int res = 0; fill(cx,cx+maxn,-1); fill(cy,cy+maxn,-1); for(int i = 1; i <= n; i++){ if(cx[i] == -1){//当前没匹配 fill(vis,vis+maxn,0); res += path(i); } } return res;}bool solve(){ return maxMatch() == p;}int main(){ scanf("%d",&t); while(t--){ input(); puts(solve() ? "YES" : "NO"); }return 0;}
0 0
- POJ 1469 courses
- POJ-1469 COURSES
- POJ 1469 COURSES
- poj 1469 && hdu1083 COURSES
- POJ 1469 COURSES
- poj(1469)COURSES
- POJ-1469-COURSES
- POJ 1469 COURSES
- poj 1469 COURSES
- POJ 1469 COURSES
- poj 1469 COURSES
- POJ 1469:COURSES
- POJ 1469 COURSES
- poj 1469 COURSES 题解
- POJ 1469 COURSES
- poj 1469 COURSES
- POJ 1469 COURSES
- poj 1469COURSES
- 三种嵌入式web服务器(Boa / lighttpd / shttpd)的 linux移植笔记
- Spark/Java8 lambda表达式
- const char*, char const*, char*const使用时的区别
- js字符串加密的几种方法
- 甲亢不能吃什么?
- poj 1469 COURSES
- excel转换成PDF的软件
- VA02修改销售订单的BAPI举例
- MIT公开课: Python 笔记7 列表及可变性,字典,效率
- Python学习笔记(四)
- enumerateObjectsUsingBlock 和 for(... in ...)
- 给表加数据的函数
- linux内存分配详解
- HDOJ-1385Minimum Transport Cost(记录路径且有点权的FLOYD)