HDU - 1845 Jimmy’s Assignment (二分匹配)
来源:互联网 发布:淘宝儿童女模特名字 编辑:程序博客网 时间:2024/06/05 19:59
Description
Jimmy is studying Advanced Graph Algorithms at his university. His most recent assignment is to find a maximum matching in a special kind of graph. This graph is undirected, has N vertices and each vertex has degree 3. Furthermore, the graph is 2-edge-connected (that is, at least 2 edges need to be removed in order to make the graph disconnected). A matching is a subset of the graph’s edges, such that no two edges in the subset have a common vertex. A maximum matching is a matching having the maximum cardinality.
Given a series of instances of the special graph mentioned above, find the cardinality of a maximum matching for each instance.
Given a series of instances of the special graph mentioned above, find the cardinality of a maximum matching for each instance.
Input
The first line of input contains an integer number T, representing the number of graph descriptions to follow. Each description contains on the first line an even integer number N (4<=N<=5000), representing the number of vertices. Each of the next 3*N/2 lines contains two integers A and B, separated by one blank, denoting that there is an edge between vertex A and vertex B. The vertices are numbered from 1 to N. No edge may appear twice in the input.
Output
For each of the T graphs, in the order given in the input, print one line containing the cardinality of a maximum matching.
Sample Input
241 21 31 42 32 43 441 21 31 42 32 43 4
Sample Output
22
Source
Politehnica University of Bucharest Local Team Contest 2007
题意:给你双向边,求最多留下多少条边使得每条边都没有共有顶点
思路:二分图匹配的定义,对于双向的要/2,用vector会超时,要用邻接表
#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;const int MAXN = 5010;const int MAXM = 50010;struct Edge {int to, next;} edge[MAXM];int head[MAXN], tot;int linker[MAXN];bool used[MAXN];int n, m;void init() {tot = 0;memset(head,-1,sizeof(head));}void addEdge(int u, int v) {edge[tot].to = v; edge[tot].next = head[u];head[u] = tot++;}bool dfs(int u) {for (int i = head[u]; i != -1; i = edge[i].next) {int v = edge[i].to;if (!used[v]) {used[v] = true;if (linker[v] == -1 || dfs(linker[v])) {linker[v] = u;return true;}}}return false;}int solve() {int ans = 0;memset(linker, -1, sizeof(linker));for (int i = 0; i < n; i++) {memset(used, false, sizeof(used));if (dfs(i))ans++;}return ans;}int main() {int t;scanf("%d",&t);while (t--) {scanf("%d", &n);m = n*3/2;int u,v;init();while (m--) {scanf("%d%d", &u, &v);u--; v--;addEdge(u,v);addEdge(v,u);}printf("%d\n", solve()/2);}return 0;}
0 0
- HDU - 1845 Jimmy’s Assignment (二分匹配)
- hdu 1845 Jimmy’s Assignment(最大二分匹配)
- hdu 1845 Jimmy’s Assignment (二分匹配)
- HDU 1845 Jimmy’s Assignment(二分图匹配)
- HDU 1845 Jimmy’s Assignment(二分图匹配)
- HDU 1845 Jimmy’s Assignment【二分匹配,三正则图】
- hdu 1845 Jimmy’s Assignment (二分图)
- hdu 1845 Jimmy’s Assignment 二分匹配最小点集覆盖
- hdu_1845 Jimmy’s Assignment 二分图匹配
- Poj 3683 Jimmy’s Assignment【二分匹配】
- HDU1845 Jimmy’s Assignment(最大匹配)卡时间
- HDU1845 Jimmy’s Assignment 984ms 飘过啊 汗死 二分匹配最大匹配数
- HDOJ 1845 Jimmy’s Assignment (求最大匹配数 匈牙利算法)
- hdu1845 Jimmy’s Assignment --- 完备匹配
- HDOJ 1845 - Jimmy’s Assignment 左右侧点代表的是相同点的二分图最大匹配/利用性质的算法
- hdoj 1845 Jimmy’s Assignment 【HK】
- HDU1845 Jimmy’s Assignment
- hdu1845 Jimmy’s Assignment (最大匹配Hopcroft-Karp算法)
- 2014 CSDN博文大赛最终获奖名单公布
- linux内存管理
- UVa10985 Rings'n'Ropes
- 如何将access mdb数据库导入到mysql中 .mdb转mysql
- OC笔记心得存档---深浅复制
- HDU - 1845 Jimmy’s Assignment (二分匹配)
- oscache.properties文件配置
- Andriod APK 文件结构详解
- 实用新型专利和发明专利有什么区别?
- 软件分享
- java 重定向和转发的区别
- 关于arcmap启动出现c++ runtime error的问题,都是360若得祸
- 暑假第7天 7月12日
- 基督教的基本的半截大兵