hdu 1084 course
来源:互联网 发布:打谱软件 mac 编辑:程序博客网 时间:2024/05/07 23:27
完美匹配题
完美匹配肯定是最大匹配,根据题意,应该是每个学生匹配到一门课。
所以学生作为左边集合。
最大匹配=最小点覆盖。
最小路径覆盖=顶点数-最大匹配数
算法匈牙利算法:
bool dfs(int i){ int n=stu[i].size(); for(int j=0;j<n;j++){ if(visited[stu[i][j]]) continue; visited[stu[i][j]]=true; if(match[stu[i][j]]==-1||dfs(match[stu[i][j]])){ match[stu[i][j]]=i; return true; } } return false;}int hungrain(int n){ memset(match,-1,sizeof(match)); // how many stu int result=0; for(int i=1;i<=n;i++){ memset(visited,false,sizeof(visited)); if(dfs(i)) result++; } return result;}
全部代码:
#include <iostream>#include<vector>#include<cstring>#include<cstdio>using namespace std;#define MAX 105vector<int> stu[305];vector<int> cour[105];bool visited[105];int p,n;int match[105];bool dfs(int i){ int n=stu[i].size(); for(int j=0;j<n;j++){ if(visited[stu[i][j]]) continue; visited[stu[i][j]]=true; if(match[stu[i][j]]==-1||dfs(match[stu[i][j]])){ match[stu[i][j]]=i; return true; } } return false;}int hungrain(int n){ memset(match,-1,sizeof(match)); // how many stu int result=0; for(int i=1;i<=n;i++){ memset(visited,false,sizeof(visited)); if(dfs(i)) result++; } return result;}int main(){ int t; scanf("%d",&t); int cnt; int id; while(t--){ scanf("%d%d",&p,&n); //p is the number of course //init for(int i=0;i<305;i++) stu[i].clear(); for(int i=1;i<=p;i++){ cnt=0; scanf("%d",&cnt); for(int j=0;j<cnt;j++){ scanf("%d",&id); stu[id].push_back(i); } } //handle int total=0; total=hungrain(n); if(total==p)cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0;}
阅读全文
0 0
- hdu 1084 course
- HDU 3152 Obstacle Course
- hdu 3152 Obstacle Course
- hdu 3152 Obstacle Course
- HDU 6186 CS Course
- CS Course HDU
- hdu 3152 Obstacle Course
- hdu 3152 Obstacle Course (bfs)
- 【搜索】 HDU 3152 Obstacle Course
- HDU--3152Obstacle Course【BFS】
- Course
- course
- HDU 3152 Obstacle Course (BFS+优先队列)
- HDU 3152 Obstacle Course(优先队列)
- hdu 3152Obstacle Course bfs+优先队列
- HDU 3152Obstacle Course (BFS ,记忆化???)
- HDU 6186 && 2017广西邀请赛:CS Course
- HDU 6186 CS Course 简单思维题
- Android UI性能优化
- 判断一颗树是不是完全二叉树
- Referenced file contains errors 解决方法
- 山东省第八届acm省赛 HEX
- codeforces Petya and Exam(模拟+哈希+细心分析)
- hdu 1084 course
- 线程池这一篇就够了
- halcon中数据格式
- jquery的表单操作------第二十六天
- eclipse安卓配置和安装
- 转载:shiro应用,注销掉后让系统跳转到后台登陆页
- H5基础知识第十二课时(节点的查看、添加和删除)
- FPGA学习
- 依存语法:从短语结构树转换为依存树