码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1845 Jimmy’s Assignment(最大匹配)卡时间

时间:2015-05-12 21:09:42      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:图论

Jimmy’s Assignment

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1093    Accepted Submission(s): 446


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
题意:求最大匹配的边数,每个顶点最多只能用一次。
解题:用二分匹配算法。很卡时间,while 比 for 循环快,所以尽可能的用while ,初始化用 memset 。 889MS   AC
</pre><pre name="code" class="cpp">#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
const int N = 5005;

int head[N], to[N *10], next1[N *10], tot;
int mach[N];
bool vist[N];

void addEdge(const int& u, const int& v) {
  to[tot] = v, next1[tot] = head[u], head[u] = tot++;
}
void addUndirEdge(const int& u, const int& v) {
  addEdge(u, v), addEdge(v, u);
}
void init(){
    tot=0;
    memset(mach,-1,sizeof(mach));
    memset(head,-1,sizeof(head));
}
bool findroad(int u){
    int i=head[u];
    while(i!=-1){
        int v=to[i]; i=next1[i];
        if(!vist[v]){
            vist[v]=1;
            if(mach[v]==-1||findroad(mach[v])){
                mach[v]=u;  return true;
            }
        }
    }
    return false;
}

int main(){
    int n,T,u,v;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        init();
        int m=3*n/2;
       while(m--){
            scanf("%d%d",&u,&v);
            addUndirEdge(u,v);
        }
        int ans=0;
        while(n){
            if(head[n]!=-1) {
                memset(vist,0,sizeof(vist));
                ans+=findroad(n);
            }
            n--;
        }
        printf("%d\n",ans/2);
    }
}


HDU1845 Jimmy’s Assignment(最大匹配)卡时间

标签:图论

原文地址:http://blog.csdn.net/u010372095/article/details/45673537

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!