二分匹配 Selecting Courses
来源:互联网 发布:软件开发案例分析 编辑:程序博客网 时间:2024/05/18 13:47
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX1 305
#define MAX2 305
int n,m,match[MAX2]; //二分图的两个集合分别含有n和m个元素。
bool visit[MAX2],G[MAX1][MAX2]; //G存储邻接矩阵。
bool DFS(int k)
{ int t;
for(int i = 0; i < m; i++)
{ if(G[k][i] && !visit[i])
{ visit[i] = true; t = match[i]; match[i] = k; //路径取反操作。
if(t == -1 || DFS(t)) return true; //整个算法的核心部分
match[i] = t;
}
}
return false;
}
int Max_match ()
{ int ans = 0,i;
memset(match, -1, sizeof(match));
for(i = 0; i <n ;i++)
{ memset(visit,0,sizeof(visit));
if(DFS(i)) ans++;
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,i,j,p,q;
while(scanf("%d",&n)!=EOF)
{
memset(G,false,sizeof(G));
m=305;
for(i=0;i<n;i++)
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&p,&q);
G[i][(p-1)*12+q]=true;
}
}
printf("%d\n",Max_match());
}
return 0;
}
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX1 305
#define MAX2 305
int n,m,match[MAX2]; //二分图的两个集合分别含有n和m个元素。
bool visit[MAX2],G[MAX1][MAX2]; //G存储邻接矩阵。
bool DFS(int k)
{ int t;
for(int i = 0; i < m; i++)
{ if(G[k][i] && !visit[i])
{ visit[i] = true; t = match[i]; match[i] = k; //路径取反操作。
if(t == -1 || DFS(t)) return true; //整个算法的核心部分
match[i] = t;
}
}
return false;
}
int Max_match ()
{ int ans = 0,i;
memset(match, -1, sizeof(match));
for(i = 0; i <n ;i++)
{ memset(visit,0,sizeof(visit));
if(DFS(i)) ans++;
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,i,j,p,q;
while(scanf("%d",&n)!=EOF)
{
memset(G,false,sizeof(G));
m=305;
for(i=0;i<n;i++)
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&p,&q);
G[i][(p-1)*12+q]=true;
}
}
printf("%d\n",Max_match());
}
return 0;
}
- 二分匹配 Selecting Courses
- 2239 Selecting Courses 二分图匹配
- POJ 2239 Selecting Courses (二分匹配)
- poj2239 Selecting Courses --- 二分图最大匹配
- 【POJ】2239 Selecting Courses 二分匹配
- POJ2239 Selecting Courses【二分图最大匹配】
- POJ 2239 - Selecting Courses(二分图匹配)
- Poj 2239 Selecting Courses 【二分匹配】
- Selecting Courses(二分图匹配 Hungary算法)
- Selecting Courses 【最大匹配】
- pku 2239 Selecting Courses(二分图最大匹配)
- poj Selecting Courses(二分图最大匹配)
- poj 匈牙利二分匹配算法2239 Selecting Courses
- poj2239 Selecting Courses(最大二分图匹配 (匈牙利算法) 实现 )
- poj 2239 Selecting Courses 二分图最大匹配数
- poj 2239 Selecting Courses 二分图最大匹配
- POj 2239 Selecting Courses(二分图匹配)
- Poj 2239 Selecting Courses 二分图最大匹配
- Android基础教程(五)之-----Menu功能菜单设计
- Android基础教程(六)之----多选项CheckBox的综合应用
- 在Linux中开机自动运行普通用户脚本程序
- Android基础教程(七)之----单选项框RadioGroup的综合应用
- 第12周实验报告1
- 二分匹配 Selecting Courses
- 快速幂取模
- 安装Apache
- About me 自我介绍网站
- 文件读写的文本方式与二进制方式、文本文件与二进制文件之区别
- 被动的主动性
- (麻烦)Flip Game (CD1488)
- POJ 3157 And Then There Was One
- Quick Linux Server Installation