poj1274 二分匹配

  今天复习二分匹配,A 了一道模板题。

  二分匹配需要理解增广路的寻找。用dfs来更新最大匹配。注意一些点:赋初值;愚蠢地把==写成了= ; 然后match的记值;每个点都要重新走一遍。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define INF 80005
 6 #define maxn 405
 7 using namespace std;
 8 int n,m,ans,match[maxn];
 9 int tot,he[maxn],to[INF],ne[INF];
10 bool check[maxn];
11 void add(int a,int b)
12 {
13     tot++;to[tot]=b;ne[tot]=he[a];he[a]=tot;
14 }
15 bool findway(int x)
16 {
17     for (int i=he[x];i;i=ne[i])//dfs
18     if (!check[to[i]]){
19         check[to[i]]=true;
20         if (match[to[i]]==-1||findway(match[to[i]])){  //not findway(to[i])
21             match[to[i]]=x;//<qwq> == not =
22             return true;
23         }
24     }
25     return false;
26 }
27 int KM ()//hungray
28 {
29     ans=0;
30     memset(match,-1,sizeof (match));
31     for (int i=1;i<=n;i++)    //if (match[i]=-1)
32     {
33         memset(check,0,sizeof (check));
34         if (findway(i)) ans++;
35     }
36     return ans;
37 }
38 int main()
39 {
40     freopen("poj1274.in","r",stdin);
41     while (cin>>n>>m){
42         memset(ne,0,sizeof(ne));
43         memset(to,0,sizeof (to));
44         memset(he,0,sizeof (he));
45         tot=0;//赋初值
46         for (int i=1;i<=n;i++)
47         {
48             int a;scanf("%d",&a);
49             for (int j=1;j<=a;j++)
50             {
51                 int b;scanf("%d",&b);
52                 b+=n;//!!!
53                 add(i,b);add(b,i);
54             }
55         }
56         printf("%d\n",KM());
57     }
58     return 0;
59 }
时间: 2024-08-07 03:17:59

poj1274 二分匹配的相关文章

HDU 3861 The King’s Problem (强连通+二分匹配)

题目地址:HDU 3861 这题虽然是两个算法结合起来的.但是感觉挺没意思的..结合的一点也不自然,,硬生生的揉在了一块...(出题者不要喷我QAQ.) 不过这题让我发现了我的二分匹配已经好长时间没用过了..都快忘了..正好在省赛之前又复习了一下. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm>

hdu 4185 Oil Skimming(二分匹配)

Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 883    Accepted Submission(s): 374 Problem Description Thanks to a certain "green" resources company, there is a new profitable

【DFS求树的最大二分匹配+输入外挂】HDU 6178 Monkeys

http://acm.hdu.edu.cn/showproblem.php?pid=6178 [题意] 给定一棵有n个结点的树,现在有k个猴子分布在k个结点上,我们可以删去树上的一些边,使得k个猴子每个猴子都至少和其他一个猴子相连 问树上最少保留多少条边 [思路] 每个猴子要至少和一个猴子相连,考虑保留的边最少,那么最优的情况一定是一条边的两个顶点放两个猴子,这些边的顶点都不重合 我们现在要找到给定的树中最多有多少条这样的边,即最大二分匹配 O(n)的DFS,对于每个结点,优先与叶子结点形成一条

hdu 5093 Battle ships 最大二分匹配

Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 589    Accepted Submission(s): 233 Problem Description Dear contestant, now you are an excellent navy commander, who is responsible

二分匹配最大匹配 PKU1469

一个学生可以有多种选择,问能否每个学生刚好选一门课,但是每门课最多只有一个学生可以选择 典型的二分匹配最大匹配,直接套模板 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17878   Accepted: 7048 Description Consider a group of N students and P courses. Each student visits zero, one or more

hdoj 2063 过山车 【二分匹配之匈牙利算法】

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11520    Accepted Submission(s): 5072 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做pa

hdu-----(1179)Ollivanders: Makers of Fine Wands since 382 BC.(二分匹配)

Ollivanders: Makers of Fine Wands since 382 BC. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 935    Accepted Submission(s): 523 Problem Description In Diagon Alley ,there is only one Wand-se

hdu2444 The Accomodation of Students(判断二分匹配+最大匹配)

//判断是否为二分图:在无向图G中,如果存在奇数回路,则不是二分图.否则是二分图. //判断回路奇偶性:把相邻两点染成黑白两色,如果相邻两点出现颜色相同则存在奇数回路.也就是非二分图. # include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int vis[210],map[210][210],cott[210]; int c[210]; int flag,n

HDU 2063:过山车(二分匹配,匈牙利算法)

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9745    Accepted Submission(s): 4294 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做par