POJ 1469 解题报告
2015-09-23 00:48
302 查看
这道题是求最大二分匹配,有匈牙利算法(感觉就是DFS)。这里照搬了之前3041和1274的代码,用的还是邻接矩阵,直接就过了。
thestoryofsnow | 1469 | Accepted | 192K | 469MS | C++ | 2659B |
/* ID: thestor1 LANG: C++ TASK: poj1469 */ #include <iostream> #include <fstream> #include <cmath> #include <cstdio> #include <cstring> #include <limits> #include <string> #include <vector> #include <list> #include <set> #include <map> #include <queue> #include <stack> #include <algorithm> #include <cassert> using namespace std; // See http://www.geeksforgeeks.org/maximum-bipartite-matching/ for more detailed explanation const int MAXP = 100; const int MAXN = 300; // A DFS based recursive function that returns true if a // matching for vertex u is possible bool DFS(int u, const bool adjMatrix[][MAXN], const int N, const int M, int match[], bool visited[]) { // Try every job one by one for (int v = 0; v < M; ++v) { // If applicant u is interested in job v and v is // not visited if (adjMatrix[u][v] && !visited[v]) { // Mark v as visited visited[v] = true; // If job 'v' is not assigned to an applicant OR // previously assigned applicant for job v (which is match[v]) // has an alternate job available. // Since v is marked as visited in the above line, match[v] // in the following recursive call will not get job 'v' again if (match[v] < 0 || DFS(match[v], adjMatrix, N, M, match, visited)) { match[v] = u; return true; } } } return false; } // Returns maximum number of matching from N to M // match: An array to keep track of the applicants assigned to jobs. // The value of match[i] is the applicant number // assigned to job i, the value -1 indicates nobody is assigned. int maximumBipartiteMatching(const bool adjMatrix[][MAXN], const int N, const int M, int match[], bool visited[]) { // Count of jobs assigned to applicants int mbm = 0; // Initially all jobs are available memset(match, -1, M * sizeof(int)); for (int u = 0; u < N; ++u) { // Mark all jobs as not seen for next applicant. memset(visited, false, M * sizeof(bool)); // Find if the applicant 'u' can get a job if (DFS(u, adjMatrix, N, M, match, visited)) { mbm++; } } return mbm; } int main() { bool adjMatrix[MAXP][MAXN]; int match[MAXN]; bool visited[MAXN]; int T; scanf("%d", &T); for (int t = 0; t < T; ++t) { int P, N; scanf("%d%d", &P, &N); for (int p = 0; p < P; ++p) { memset(adjMatrix[p], false, N * sizeof(bool)); } for (int p = 0; p < P; ++p) { int C; scanf("%d", &C); for (int c = 0; c < C; ++c) { int s; scanf("%d", &s); adjMatrix[p][s - 1] = true; } } if (N >= P && maximumBipartiteMatching(adjMatrix, P, N, match, visited) == P) { printf("YES\n"); } else { printf("NO\n"); } } return 0; }
相关文章推荐
- iOS项目开发实战——屏幕适配、单位点和分辨率
- APACHE服务实验
- JavaScript----函数深入理解
- Leetcode Move Zeroes
- Shell 常用命令总结
- Using a Comparison Function for the Key Type
- POJ 1719 Shooting Contest 二分匹配
- C++ STL之stack queue 作者csdn账号 liuhmmjj
- java反射机制
- Nginx配置文件nginx.conf中文详解(总结)
- Hadoop家族
- mac jdk 6设置
- const常量和#define宏常量的区别
- 关于规划的寓言故事:把一张纸折叠51次 ------意义不在折纸,而在规划,重在规划!!!
- MAC 如何在MAC系统中保存txt格式文本
- 进程监控程序的开发(程序运行监控)
- HDU_3667_Transportation(最小费用流)
- JavaScript----数字转换字符串&字符串转发数字
- (奶油小刀插件) android-butterknife-zelezny 插件使用
- Android 网络编程