hdu 1845 Jimmy’s Assignment (二分图)
来源:互联网 发布:淘宝如何完成实名认证 编辑:程序博客网 时间:2024/06/05 23:53
Jimmy’s Assignment
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 896 Accepted Submission(s): 379
Problem 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
主要是bool数组真的很省时间!!
#include"stdio.h"#include"string.h"#include"queue"#include"vector"using namespace std;#define N 5005#define M 30005int lx[N],ly[N];bool mark[N];vector<int>g[N];int find(int k){ int i,v; for(i=0;i<g[k].size();i++) { v=g[k][i]; if(!mark[v]) { mark[v]=1; if(ly[v]==-1||find(ly[v])) { ly[v]=k;lx[k]=v; return 1; } } } return 0;}int main(){ int i,u,v,n,T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=0;i<=n;i++) g[i].clear(); for(i=0;i<n*3/2;i++) { scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } memset(lx,-1,sizeof(lx)); memset(ly,-1,sizeof(ly)); int ans=0; for(i=1;i<=n;i++) { if(lx[i]!=-1) continue; //memset(mark,0,(n+2)*sizeof(int)); memset(mark,0,sizeof(mark)); ans+=find(i); } printf("%d\n",ans/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【二分匹配】
- hdoj 1845 Jimmy’s Assignment 【HK】
- HDU1845 Jimmy’s Assignment
- HDOJ 1845 - Jimmy’s Assignment 左右侧点代表的是相同点的二分图最大匹配/利用性质的算法
- HDU1845 Jimmy’s Assignment 984ms 飘过啊 汗死 二分匹配最大匹配数
- hdu1845 Jimmy’s Assignment --- 完备匹配
- HDOJ 1845 Jimmy’s Assignment (求最大匹配数 匈牙利算法)
- HDU-2853 Assignment【二分图最优匹配】
- HDU 2853 Assignment(二分图最优匹配)
- 自考之软件开发工具
- 数据结构--树和二叉树--二叉树的线索化
- scanf(),printf()和gets(),puts()在输入输出字符串时的区别
- Makefile经典教
- 【HDU】3061 Battle 最大权闭合子图
- hdu 1845 Jimmy’s Assignment (二分图)
- 【拓扑排序】HDU 1285--- 确定比赛名次
- 山东理工OJ【2121】数据结构实验之链表六:有序链表的建立(插排法)
- hdu 1800 Flying to the Mars (map)(暴力)
- 在 Ubuntu 14.04 Server 上安装部署 Ruby on Rails 应用
- 50道Java习题之14
- 老鼠试毒算法
- HDU 4864 Task(贪心,输出利润的最大值)
- UVA10148- Advertisement(区间选点)