poj-1469-COURSES-二分图匹配-匈牙利算法(模板)

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

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

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 using namespace std;
 6 const int maxn = 555;
 7 int n, p;
 8 vector<int> g[maxn];
 9 int from[maxn], tot;
10 bool use[maxn];
11
12 bool match(int x)
13 {
14    for (int i = 0; i < g[x].size(); i ++) {
15       if(!use[g[x][i]]) {
16          use[g[x][i]] = true;
17          if(from[g[x][i]] == -1 || match(from[g[x][i]])) {
18             from[g[x][i]] = x;
19             return true;
20          }
21       }
22    }
23    return false;
24 }
25
26 int hungary()
27 {
28    tot = 0;
29    memset(from, -1, sizeof(from));
30    for(int i = 1; i <= n; i ++) {
31       memset(use, 0, sizeof(use));
32       if(match(i)) tot ++;
33    }
34    return tot;
35 }
36
37 int main()
38 {
39    int t; cin>>t;
40    while(t--) {
41       for(int i = 0; i < maxn; i++) g[i].clear();
42       scanf("%d%d", &p, &n);
43       for(int i = 1; i <= p; i++) {
44          int k; scanf("%d", &k);
45          for(int j = 0; j < k; j++) {
46             int a; scanf("%d", &a);
47             g[i].push_back(a);
48          }
49       }
50       int res = hungary();
51       //cout<<res<<endl;
52       if(res == p) printf("YES\n");
53       else printf("NO\n");
54    }
55 }

时间: 2024-08-01 22:47:53

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

POJ 1469 COURSES (二分图最大匹配 匈牙利算法)

COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18892   Accepted: 7455 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 poss

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

原文链接http://www.cnblogs.com/zhouzhendong/p/8232649.html 题目传送门 - POJ1469 题意概括 在一个大矩阵中,有一些障碍点. 现在让你用1*2的小矩形覆盖非障碍点,要求不覆盖到障碍点并且不重复覆盖,问是否可以覆盖所有非障碍点. 题解 本题几乎是裸题. 首先注意读入的表示障碍点的二元组(x,y)中y是行,x是列. 这个毒性深重<差评> 然后考虑算法.读者可以参考笔者的前一篇博客. 对于相邻的非障碍点我们来回都建边.然后我们给原图按照到某一

二分图匹配--匈牙利算法模板

对二分图一边的每一个点 dfs ,寻找它的所有匹配点,若那个匹配点没有匹配过或匹配的另一半能新找一个点与之匹配,那么就重组后分别匹配,否则不能匹配 1 int dfs(int k) 2 { 3 for(int i=head[k];~i;i=next[i]) 4 if(!visit[point[i]]){ 5 int p=point[i]; 6 visit[p]=1; 7 if(match[p]==-1||dfs(match[p])){ 8 match[p]=k; 9 return 1; 10 }

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

The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John r

POJ 1469 COURSES 二分图最大匹配

就是判断一下是不是每一个课程都能找到自己的代表人,做一遍最大匹配看看匹配数是否等于p即可 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cs

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

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 49    Accepted Submission(s): 14 Problem Description There is a kindom of obsession, so people in this kingdom do things very

poj 1469 COURSES (二分匹配)

COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16877   Accepted: 6627 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 poss

HDU1507 Uncle Tom&#39;s Inherited Land* 二分图匹配 匈牙利算法 黑白染色

原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1*2的矩形覆盖所有的不废的点,并且不重叠,问最多可以覆盖多少个1*2的矩形,输出方案,有SPJ. 输入描述: 多组数据,每组首先两个数n,m(如果n和m为0,则结束程序) 然后给出k 然后给出k个二元组(x,y)表示废点的坐标. 题解 按照前两片博文的算法已经不行了,因为方案不对了. 所以我们要进行

POJ - 1469 COURSES [二分图匈牙利算法]

COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24919   Accepted: 9679 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 poss

二分图匹配(匈牙利算法) POJ 3020 Antenna Placement

题目传送门 1 /* 2 题意:*的点占据后能顺带占据四个方向的一个*,问最少要占据多少个 3 匈牙利算法:按坐标奇偶性把*分为两个集合,那么除了匹配的其中一方是顺带占据外,其他都要占据 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <vector> 9 using namespace std; 10 11 const int MAXN = 4e