hdu 1845 Jimmy’s Assignment (二分图)

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.

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

这题真的很坑!!937ms过的,期间有的代码好像搞错了,竟然速度只400+ms.其实答案就是n/2。

主要是bool数组真的很省时间!!

#include"stdio.h"
#include"string.h"
#include"queue"
#include"vector"
using namespace std;
#define N 5005
#define M 30005
int 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;
}

hdu 1845 Jimmy’s Assignment (二分图),布布扣,bubuko.com

时间: 2024-11-03 22:59:39

hdu 1845 Jimmy’s Assignment (二分图)的相关文章

HDU - 1845 Jimmy’s Assignment (二分匹配)

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 gr

POJ3189_Steady Cow Assignment(二分图多重匹配/网络流)

解题报告 http://blog.csdn.net/juncoder/article/details/38340447 题目传送门 题意: B个猪圈,N头猪,每头猪对每个猪圈有一个满意值,要求安排这些猪使得最大满意和最小满意的猪差值最小 思路: 二分图的多重匹配问题; 猪圈和源点连边,容量为猪圈容量,猪与汇点连边,容量1; 猪圈和猪之间连线取决所取的满意值范围; 二分查找满意值最小差值的范围. #include <iostream> #include <cstring> #incl

hdu1845 Jimmy’s Assignment --- 完备匹配

题意: 要求在一个特殊的图上找最大匹配,该图特点是:无向图,每个节点度数为3,是一个边双连通分量(the graph is 2-edge-connected (that is, at least 2 edges need to be removed in order to make the graph disconnected) 这一点是这样理解的把..) 思路: 一般想法就直接建图求最大匹配,点的范围是5000,不优化可能超时,下面代码是890ms过的. 另一种思路: 完备匹配的条件: 1.

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

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 mos

HDU 2063 过山车 二分图题解

一个男女搭配的关系图,看可以凑成多少对,基本和最原始的一个二分图谜题一样了,就是 一个岛上可以凑成多少对夫妻的问题. 所以是典型的二分图问题. 使用匈牙利算法,写成两个函数,就非常清晰了. 本程序还带分配释放程序,当然oj一般不需要.但是好的程序一定要. #include <stdio.h> #include <stdlib.h> int K, M, N, a, b; int *linker; bool **gra, *used; void initGraph() { gra =

POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)

解题报告 http://blog.csdn.net/juncoder/article/details/38340447 题目传送门 题意: B个猪圈,N头猪.每头猪对每一个猪圈有一个惬意值.要求安排这些猪使得最大惬意和最小惬意的猪差值最小 思路: 二分图的多重匹配问题; 猪圈和源点连边,容量为猪圈容量.猪与汇点连边,容量1; 猪圈和猪之间连线取决所取的惬意值范围; 二分查找惬意值最小差值的范围. #include <iostream> #include <cstring> #inc

[HDU] 2063 过山车(二分图最大匹配)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2063 女生为X集合,男生为Y集合,求二分图最大匹配数即可. 1 #include<cstdio> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 #include<math.h> 6 #include<stdbool.h> 7 #include<ti

[HDU] 1068 Girls and Boys(二分图最大匹配)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1068 本题求二分图最大独立点集.因为最大独立点集=顶点数-最大匹配数.所以转化为求最大匹配.因为没有给出男女,所以每个人都用了两遍,所以结果应该除以2. 1 #include<cstdio> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 #include<math.h&g

HDU 1068 Girls and Boys (二分图最大独立集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 有n个同学,格式ni:(m) n1 n2 n3表示同学ni有缘与n1,n2,n3成为情侣,求集合中不存在有缘成为情侣的同学的最大同学数. 独立集(图的顶点集的子集,其中任意两点不相邻) 二分图中 最大独立集 = 顶点个数 - 最大匹配数 因为男女不知道,将一个人拆成两个性别,求最大匹配后,除以2就行了. 这种做法比较难理解. 1 #include <iostream> 2 #include