POJ
来源:互联网 发布:青岛优创数据英语 编辑:程序博客网 时间:2024/05/20 04:29
最小边覆盖问题 也是匹配问题
这里用到了 匈牙利算法,
还用到了结构体数组存邻接表的做法
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <cmath>#include <set>#include <map>#include <stack>#include <queue>#include <ctype.h>#include <vector>#include <algorithm>#include <sstream>#define PI acos(-1.0)#define in freopen("in.txt", "r", stdin)#define out freopen("out.txt", "w", stdout)using namespace std;typedef long long ll;const int maxn = 1500+7, INF = 0x3f3f3f3f;int n, cur, ans;int f[maxn];bool vis[maxn];int next[maxn];struct edge { int v, bef;}e[maxn*2];void add(int x, int y) { e[cur].v = y; e[cur].bef = next[x]; next[x] = cur++;}void init() { memset(next, -1, sizeof(int)*(n+1)); cur = 1; for(int j = 0; j < n; ++j) { int u, v, cnt; scanf("%d:(%d)", &u, &cnt); for(int i = 0; i < cnt; ++i) { scanf("%d", &v); add(u, v); add(v, u); //a[u].push_back(v); //a[v].push_back(u); } } memset(f, -1, sizeof(int)*(n+1));}bool dfs(int id) { for(int i = next[id]; i != -1; i = e[i].bef) { if(!vis[e[i].v]) { vis[e[i].v] = 1; if(f[e[i].v] == -1 || dfs(f[e[i].v])) { f[id] = e[i].v; f[e[i].v] = id; return true; } } } return false;}int main() { while(scanf("%d", &n) != EOF && n) { init(); ans = 0; for(int i = 0; i < n; ++i) { if(f[i] == -1) { memset(vis, false, sizeof(bool)*(n+1)); if(dfs(i)) ans++; } } printf("%d\n", ans); } return 0;}
阅读全文
1 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 线程中用Thread还是runable
- android配置集成CI持续化开发工具Jenkins
- curl常见返回错误码<转载>
- js基础-引用类型-Array对象 ★★★☆
- ssm(spring+spring mvc+mybatis+maven)高仿bilibili视频网站项目实例
- POJ
- python根据字典中值的大小,对字典中的项排序
- Android权限申请哪些需要动态申请
- 网页的兼容性解决办法
- iOS runtime ~ objc_setAssociatedObject 函数关键策略
- 链表题_2
- SpringBoot学习笔记之异常处理
- 隐马尔可夫模型的Viterbi解码算法
- 小问题集