POJ1469

二分图的最大匹配模板题

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <string.h>
 4 using namespace std;
 5 bool map[105][305],vis[305],flag;
 6 int match[305];
 7 int p,n;
 8 bool dfs(int a){
 9     for(int i=1;i<=n;i++){
10         if(map[a][i]&&!vis[i]){
11             vis[i]=1;
12             if(match[i]==-1||dfs(match[i])){
13                 match[i]=a;
14                 return true;
15             }
16         }
17     }
18     return false;
19 }
20 int main(){
21     int t,ans,k,a;
22     scanf("%d",&t);
23     while(t--){
24         flag=1;
25         memset(map,false,sizeof(map));
26         memset(match,-1,sizeof(match));
27         scanf("%d%d",&p,&n);
28         for(int i=1;i<=p;i++){
29             scanf("%d",&k);
30             if(!k)flag=0;
31             while(k--){
32                 scanf("%d",&a);
33                 map[i][a]=1;
34             }
35         }
36         if(flag){
37             ans=0;
38             for(int i=1;i<=p;i++){
39                 memset(vis,false,sizeof(vis));
40                 if(dfs(i))ans++;
41             }
42             if(ans==p)puts("YES");
43             else puts("NO");
44         }
45         else puts("NO");
46     }
47     return 0;
48 }
时间: 2024-08-04 15:51:27

POJ1469的相关文章

裸二分图匹配poj1469

poj1496 题意: 给你p门课程和n个学生,一个学生可以选0门,1门,或者多门课程, 现在要求一个由p个学生组成的集合,满足下列2个条件: 1.每个学生选择一个不同的课程 2.每个课程都有不同的代表 如果满足,就输出YES POJ 3041 Asteroids 问题: 假如你现在正处在一个N*N的矩阵中,这个矩阵里面有K个障碍物. 你拥有一把武器,一发弹药一次能消灭一行或一列的障碍物,求最小的弹药消灭全部障碍物 输入为: N K 接下来有K行, 每行包含障碍物的坐标, 即r行c列; 如: 3

poj1469 二分图匹配(匈牙利算法)

poj1469 course code: #include<cstdio> #include<cstring> using namespace std; #define P 110 #define N 310 int map[P][N]; int match[N]; bool use[N]; int p, n; bool find(int u) //u是课程 { for(int i = 1; i <= n; ++i) { if(!use[i] && map[u

poj1469二分图

题意: 给你一个二分图,求最大匹配是否是 课程这边集合的顶点数. 数组开错了 wa了数次. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <

POJ1469 COURSES 【二分图最大匹配&amp;#183;HK算法】

COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17777   Accepted: 7007 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是列. 这个毒性深重<差评> 然后考虑算法.读者可以参考笔者的前一篇博客. 对于相邻的非障碍点我们来回都建边.然后我们给原图按照到某一

POJ1469 COURSES 【二分图最大匹配&#183;HK算法】

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

最小点覆盖,二分图最大匹配—POJ1274 POJ1469 POJ1469

二分图最大匹配常用的匈牙利算法,之前写的很幼稚,虽然也过了,但是平白的比别人多开了两倍的空间. 本来就是在填加边的时候把左边的点和右边的点分开算都加在图里面储存,然后匹配的时候就互相匹配 match[u]=v; match[v]=u; 然后看了模板之后才发现其实完全不用加上,只记录match[v]=u就可以了 三题的代码都是一样的,需要注意的是1469题是要求求最小点覆盖数,最小点覆盖数就等于最大匹配数. 简单说下. 首先,最小点覆盖数肯定不会小于匹配数,因为那样连已经匹配的边都没法覆盖掉,何谈

poj1469 COURSES 二分匹配模板水题

#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 310; int match[maxn]; int line[maxn][maxn]; int vis[maxn]; int N , P; int find(int start) { for(int i = 1;i <= N;i++) { if(!vis[i]&&li

【二分图匹配入门专题1】F - COURSES poj1469【最大匹配--匈牙利算法模板题】

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 stude