HDU1803 Courses(二分图匹配,匈牙利算法,dfs)
来源:互联网 发布:new event js 位置 编辑:程序博客网 时间:2024/06/15 10:59
Problem Description
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 a text file. The first line
of the input file 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 CountPThe 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.An example of program input and output:
Sample Input
23 33 1 2 32 1 21 13 32 1 32 1 31 1
Sample Output
YESNO
思路
二分图匹配模板题,首先是T组数据,然后一个p和n代表有p门课和n个学生
接下来p行每行有一个数m,然后有m个数,代表m个学生对这门课感兴趣
问的是能不能有最大匹配,有的话输出YES,匈牙利算法模板题
代码
#include <bits/stdc++.h>using namespace std;#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3fconst int N=200+20;int p,n,m;vector<int>G[N];int e[N][N];int vis[N],match[N];int dfs(int u){ for(int i=0; i<G[u].size(); i++) { int v=G[u][i]; if(!vis[v]) { vis[v]=1; if(!match[v]||dfs(match[v])) { match[v]=u; return 1; } } } return 0;}int main(){ int t,x; scanf("%d",&t); while(t--) { for(int i=0; i<N; i++)G[i].clear(); mem(match,0); scanf("%d%d",&p,&n); for(int i=1; i<=p; i++) { scanf("%d",&m); while(m--) { scanf("%d",&x); G[i].push_back(x); } } int sum=0; for(int i=1; i<=p; i++) { mem(vis,0); if(dfs(i))sum++; } puts(sum==p?"YES":"NO"); } return 0;}
- HDU1803 Courses(二分图匹配,匈牙利算法,dfs)
- 【二分图匹配-匈牙利算法】Courses HDU
- poj2239 Selecting Courses(最大二分图匹配 (匈牙利算法) 实现 )
- hdu 1082 Courses 二分图最大匹配(匈牙利算法)
- poj题目1469 COURSES (二分图匹配,匈牙利算法)
- hdu 1083 Courses(二分图匹配 匈牙利算法)
- 【二分图最大匹配】【匈牙利算法】poj1469 COURSES && poj2446 Chessboard
- POJ 1469 COURSES (二分图最大匹配 匈牙利算法)
- poj 1469 COURSES 二分图最大匹配 匈牙利算法
- hdoj 1083 Courses 【匈牙利算法&&二分图匹配】
- POJ 1469 COURSES(匈牙利算法二分图最大匹配)
- HDU 1083 Courses(二分图最大匹配【匈牙利算法】)
- POJ 1469-COURSES(二分图匹配入门-匈牙利算法)
- HDU 1082 COURSES (二分图匹配之匈牙利算法)
- POJ1469[COURSES] 二分图最大匹配 匈牙利算法
- POJ2239 Selecting Courses(二分图匹配,匈牙利算法)
- 【二分图匹配】最大匹配-匈牙利算法BFS && DFS写法
- HDU 1083 Courses(二分图,匈牙利算法)
- H3C-QoS实例二:设备管理中应用ACL
- 如何在IIS7或IIS7.5中导入导出站点及应用程序池.
- 看到的比较好的文章 关于工期
- socket、fsockopen、curl、stream 区别
- LSI 硬 RAID 激活(storcli命令)
- HDU1803 Courses(二分图匹配,匈牙利算法,dfs)
- windows 安装vim过程记录
- 文章标题 三大框架整合---你必须知道的面试题
- 庆祝新的AM335x板子PCB Layout完工
- 房地产税将结束房地产黄金期,中产阶级将赢得未来
- 在Maven仓库中添加Oracle JDBC驱动
- linux常用命令大全1
- 统计学习方法---感知机算法拓展(神经网络)
- 第9章 路由(Route)