http://poj.org/problem?id=1469

这道题我绝壁写过但是以前没有mark过二分图最大匹配的代码mark一下。

匈牙利 O(mn)

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<queue>
using namespace std;
#define LL long long
const int maxn=;
int n,m;
struct nod{
int y,next;
}e[maxn*maxn];
int head[maxn]={},tot=;
int p[maxn]={},vis[maxn]={};
inline void init(int x,int y){e[++tot].y=y;e[tot].next=head[x];head[x]=tot;}
bool dfs(int x){
for(int i=head[x];i;i=e[i].next){
if(!vis[e[i].y]){
vis[e[i].y]=;
if((!p[e[i].y])||dfs(p[e[i].y])){
p[e[i].y]=x;
return ;
}
}
}return ;
}
int main(){
int T;scanf("%d",&T);
while(T-->){
scanf("%d%d",&n,&m);
memset(p,,sizeof(p));
memset(vis,,sizeof(vis));
memset(head,,sizeof(head));tot=;
int x,y;
for(int i=;i<=n;i++){
scanf("%d",&x);
for(int j=;j<=x;j++){scanf("%d",&y);init(i,y);}
}
int ans=;
for(int i=;i<=n;i++){memset(vis,,sizeof(vis));if(dfs(i))++ans;}
if(ans==n)printf("YES\n");
else printf("NO\n");
}
return ;
}

hopcroft-karp O(sqrt(n)m)

https://blog.csdn.net/wall_f/article/details/8248373

是一种魔改版匈牙利,魔改之后竟然有点像网络流。不过我用网络流找最大匹配差不多也是这个复杂度吧,这个算法有什么意义吗。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<queue>
using namespace std;
#define LL long long
const int maxn=;
const int minf=;
int n,m,dis;
struct nod{
int y,next;
}e[maxn*];
int head[maxn]={},tot=;
int vis[maxn]={};
int cx[maxn]={},cy[maxn]={},dx[maxn]={},dy[maxn]={};
inline void init(int x,int y){e[++tot].y=y;e[tot].next=head[x];head[x]=tot;}
bool bfs(){
memset(dx,-,sizeof(dx));memset(dy,-,sizeof(dy));
dis=minf;queue<int>q;
for(int i=;i<=n;i++){if(!cx[i]){q.push(i);dx[i]=;}}
while(!q.empty()){
int x=q.front();q.pop();
if(dx[x]>dis)break;
for(int i=head[x];i;i=e[i].next){
int y=e[i].y;
if(dy[y]==-){
dy[y]=dx[x]+;
if(!cy[y])dis=dy[y];
else {dx[cy[y]]=dy[y]+;q.push(cy[y]);}
}
}
}
return dis!=minf;
}
int dfs(int x){
for(int i=head[x];i;i=e[i].next){
int y=e[i].y;
if((!vis[y])&&dy[y]==dx[x]+){
vis[y]=;
if(cy[y]&&dy[y]==dis)continue;
if((!cy[y])||dfs(cy[y])){
cy[y]=x;cx[x]=y;
return ;
}
}
}
return ;
}
int main(){
int T;scanf("%d",&T);
while(T-->){
scanf("%d%d",&n,&m);
memset(head,,sizeof(head));tot=;
int x,y;
for(int i=;i<=n;i++){
scanf("%d",&x);
for(int j=;j<=x;j++){scanf("%d",&y);init(i,y);}
}
int ans=;
memset(cx,,sizeof(cx));
memset(cy,,sizeof(cy));
while(bfs()){
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
if(!cx[i])ans+=dfs(i);
}
}
if(ans==n)printf("YES\n");
else printf("NO\n");
}
return ;
}

POJ 1469 COURSES 二分图最大匹配 二分图的更多相关文章

  1. POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配

    两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...

  2. poj 1469 COURSES 题解

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21515   Accepted: 8455 Descript ...

  3. poj 1469 COURSES(匈牙利算法模板)

    http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  4. poj 1469 COURSES (二分图模板应用 【*模板】 )

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18454   Accepted: 7275 Descript ...

  5. poj——1469 COURSES

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24192   Accepted: 9426 Descript ...

  6. POJ 1469 COURSES

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20478   Accepted: 8056 Descript ...

  7. HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)

    [题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...

  8. poj 1469 COURSES (二分匹配)

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16877   Accepted: 6627 Descript ...

  9. poj 1469 COURSES 解题报告

    题目链接:http://poj.org/problem?id=1469 题目意思:有 N 个人,P个课程,每一个课程有一些学生参加(0个.1个或多个参加).问 能否使得 P 个课程 恰好与 P 个学生 ...

随机推荐

  1. 使用TS+Sequelize实现更简洁的CRUD

    如果是经常使用Node来做服务端开发的童鞋,肯定不可避免的会操作数据库,做一些增删改查(CRUD,Create Read Update Delete)的操作,如果是一些简单的操作,类似定时脚本什么的, ...

  2. ORB_SLAM2 源码阅读 ORB_SLAM2::Initializer::ComputeF21 (OpenCV 细节)

    ORB_SLAM2 计算 F21 的代码是这样的. cv::Mat Initializer::ComputeF21(const vector<cv::Point2f> &vP1,c ...

  3. Android获取手机分辨率DisplayMetircs类

    关于Android中手机分辨率的使用 Android 可设置为随着窗口大小调整缩放比例,但即便如此,手机程序设计人员还是必须知道手机屏幕的边界,以避免缩放造成的布局变形问题. 手机的分辨率信息是手机的 ...

  4. Centos7安装FTP突然无法登录

    vi /etc/pam.d/vsftpd //注释掉auth required pam_shells.so session optional pam_keyinit.so force revokeau ...

  5. jenkins 入门教程(上)【转】

    转自:https://www.cnblogs.com/yjmyzz/p/jenkins-tutorial-part-1.html jenkins是一个广泛用于持续构建的可视化web工具,持续构建说得更 ...

  6. SQl 跨服务器查询脚本示例

    1.采用OPENDATASOURCE select top 10 *from OPENDATASOURCE('SQLOLEDB','Data Source=IP地址;User ID=连接用户名称;Pa ...

  7. centos6 yum方式升级内核【转】

    最近没有时间好久没有写文章了,今天由于需要安装docker学习虚拟容器的知识,需要升级OS的内核.目前我这边使用的OS是centos6.5,内核是2.6版本的,如下: cat /etc/issue u ...

  8. 十三、springboot集成定时任务(Scheduling Tasks)

    定时任务(Scheduling Tasks) 在springboot创建定时任务比较简单,只需2步: 1.在程序的入口加上@EnableScheduling注解. 2.在定时方法上加@Schedule ...

  9. 二、springboot配置

    一.启动类 在包根目录下添加启动类,必须包含main方法,再添加Spring Boot启动方法: SpringApplication.run(SampleController.class, args) ...

  10. poj1056

    简单题 #include <iostream> #include <string> using namespace std; struct cnode { cnode *pze ...