Jimmy’s Assignment
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1525 Accepted Submission(s): 661
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
2 4 1 2 1 3 1 4 2 3 2 4 3 4 4 1 2 1 3 1 4 2 3 2 4 3 4
Sample Output
2 2
Author
Mugurel Ionut Andreica
Source
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1845
邝斌说是水题!!!
AC代码1:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=5000+5;
int n;
int link[maxn];
bool vis[maxn];
vector<int>a[maxn];
bool Find(int x)
{
for(int i=0;i<a[x].size();i++)
{
int v=a[x][i];
if(!vis[v])
{
vis[v]=true;
if(link[v]==-1||Find(link[v]))
{
link[v]=x;
return true;
}
}
}
return false;
}
int Hungery()
{
int ans=0;
memset(link,-1,sizeof(link));
for(int i=1;i<=n;i++)
{
memset(vis,false,sizeof(vis));
if(Find(i))
ans++;
}
return ans;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n;
for(int i=1;i<=n;i++)
a[i].clear();
int m=3*n/2;
int x,y;
while(m--)
{
scanf("%d%d",&x,&y);
a[x].push_back(y);
a[y].push_back(x);
}
cout<<Hungery()/2<<endl;
}
return 0;
}
以下转自:http://blog.sina.com.cn/s/blog_677a3eb30100llyn.html
给一n个点的三正则图,求最大匹配。
根据握手定理,n一定是偶数。
由于三正则图,而且题目提示是2边连通,所以图中不存在桥,也就是一定可以找到一条回路经过每个顶点至少一次(强连通的定义:强连通图一定存在一条回路记过每个顶点至少一次)由于是三则图,每个顶点的度是3,如果这条回路经过某个顶点2次,那么这个顶点的度就是4,这个和条件矛盾。
这条经过每个顶点一次的交错路就可以作出n/2匹配。
AC代码2:
#include<stdio.h>
int main(){
int T,x,y,n,i;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(i=1; i<=n*3/2; i++)scanf("%d%d",&x,&y);
printf("%d\n",n/2);
}
return 0;
}
尊重原创,转载请注明出处:http://blog.csdn.net/hurmishine