hdu1845Jimmy’s Assignment
来源:互联网 发布:python做app 编辑:程序博客网 时间:2024/06/18 18:08
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
传说中的水题就是这样的,测试模板。
#include<map> #include<set> #include<list> #include<queue> #include<stack> #include<vector> #include<cmath> #include<cstdio> #include<string> #include<iostream> #include<algorithm> #include<iterator> using namespace std; const int maxn=5060; struct node { int to; int next; }edge[maxn*maxn]; int head[maxn]; int mark[maxn]; bool used[maxn]; int tot; int n; struct node2 { int t; int a,b,c,d; }p[maxn]; void addedge(int from,int to) { edge[tot].to=to; edge[tot].next=head[from]; head[from]=tot++; } bool dfs(int x) { for(int i=head[x];i!=-1;i=edge[i].next) { if(!used[edge[i].to]) { used[edge[i].to]=1; if(mark[edge[i].to]==-1 || dfs(mark[edge[i].to])) { mark[edge[i].to]=x; return true; } } } return false; } int hungary() { memset(mark,-1,sizeof(mark)); int ans=0; for(int i=1;i<=n;i++) { memset(used,0,sizeof(used)); if(dfs(i)) ans++; } return ans; }int main(){int t,x,y;scanf("%d",&t);while(t--){scanf("%d",&n);memset(head,-1,sizeof(head));tot=0;for(int i=1;i<=3*n/2;i++){scanf("%d%d",&x,&y);addedge(x,y);addedge(y,x);}printf("%d\n",hungary()/2);}return 0;}
0 0
- hdu1845Jimmy’s Assignment
- hdu1845Jimmy’s Assignment
- HDU1845Jimmy’s Assignment(无向图,最大匹配)
- HDU1845 Jimmy’s Assignment
- hdu_1845 Jimmy’s Assignment 二分图匹配
- hdu1845 Jimmy’s Assignment --- 完备匹配
- hdu 1845 Jimmy’s Assignment (二分图)
- hdoj 1845 Jimmy’s Assignment 【HK】
- Poj 3683 Jimmy’s Assignment【二分匹配】
- Assignment
- hdu1845 Jimmy’s Assignment (最大匹配Hopcroft-Karp算法)
- HDU - 1845 Jimmy’s Assignment (二分匹配)
- HDU 1845 Jimmy’s Assignment(二分图匹配)
- HDU1845 Jimmy’s Assignment(最大匹配)卡时间
- hdu 1845 Jimmy’s Assignment(最大二分匹配)
- HDU 1845 Jimmy’s Assignment(二分图匹配)
- 003:Java's +=, -=, *=, /= compound assignment operators
- hdu 1845 Jimmy’s Assignment (二分匹配)
- asp.net中哪些情况会导致应用程序重启
- 给自己的requestProxy写个教程
- ios--UITabBar的基本概念
- vector中erase用法注意事项
- UVA 10061 How many zero's and how many digits ?(数论)
- hdu1845Jimmy’s Assignment
- poj2503Babelfish
- 计算1-1/x+1/x*x
- 04-4. 猜数字游戏(15)
- 黑马程序员——网络编程
- hdu 1028 Ignatius and the Princess III
- 友元类
- codeforces 258B Little Elephant and Elections 数位DP+排列组合
- 【九度OJ】1014【快速排序】