Jimmy’s Assignment
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1460 Accepted Submission(s): 619
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.
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
Politehnica University of Bucharest Local Team Contest 2007
题目大意:
求一个无向图的最大匹配。
Ac 代码1:950+ms
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int head[5050];
struct EdgeNode
{
int to;
int w;
int next;
}e[50050];
int cont;
int match[5050];
int vis[5050];
void add(int from,int to)
{
e[cont].to=to;
e[cont].next=head[from];
head[from]=cont++;
}
int find(int u)
{
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(vis[v]==0)
{
vis[v]=1;
if(match[v]==-1||find(match[v]))
{
match[v]=u;
return 1;
}
}
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
cont=0;
memset(head,-1,sizeof(head));
memset(match,-1,sizeof(match));
for(int i=0;i<3*n/2;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
int output=0;
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(find(i))
{
output++;
}
}
printf("%d\n",output/2);
}
}
2、可以根据代码找找规律,发现这样的图,输出的值永远都是n/2.
Ac代码2:
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int head[5050];
struct EdgeNode
{
int to;
int w;
int next;
}e[50050];
int cont;
int match[5050];
int vis[5050];
void add(int from,int to)
{
e[cont].to=to;
e[cont].next=head[from];
head[from]=cont++;
}
int find(int u)
{
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(vis[v]==0)
{
vis[v]=1;
if(match[v]==-1||find(match[v]))
{
match[v]=u;
return 1;
}
}
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
cont=0;
memset(head,-1,sizeof(head));
memset(match,-1,sizeof(match));
for(int i=0;i<3*n/2;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
int output=0;
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(find(i))
{
output++;
}
}
printf("%d\n",output/2);
}
}