hdoj 1845 Jimmy’s Assignment 【HK】



Jimmy’s Assignment

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


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
 



以前没A的题,今天KO了。


题意:给你n个点和n*3/2条边,求出最大匹配。


HK,注意设置numx = numy = n后,最大匹配结果除2。



AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAXN 5000+10
using namespace std;
int mx[MAXN], my[MAXN];//记录X和Y集元素的匹配点
int dx[MAXN], dy[MAXN];//记录距离
bool used[MAXN];//标记是否使用
vector<int> G[MAXN];//存储二分图
int n, m;//点数 边数
int numx, numy;//记录X集元素个数 Y集元素个数
int DFS(int u)
{
    for(int i = 0; i < G[u].size(); i++)
    {
        int v = G[u][i];
        if(!used[v] && dy[v] == dx[u] + 1)
        {
            used[v] = true;
            if(my[v] == -1 || DFS(my[v]))
            {
                my[v] = u;
                mx[u] = v;
                return 1;
            }
        }
    }
    return 0;
}
void HK_match()
{
    memset(mx, -1, sizeof(mx));
    memset(my, -1, sizeof(my));
    int ans = 0;
    while(1)
    {
        bool flag = false;//标记是否找到可增广路径
        memset(dx, 0, sizeof(dx));
        memset(dy, 0, sizeof(dy));
        queue<int> Q;
        for(int i = 1; i <= numx; i++)
            if(mx[i] == -1) Q.push(i);
        while(!Q.empty())//寻找增广路
        {
            int u = Q.front(); Q.pop();
            for(int i = 0; i < G[u].size(); i++)
            {
                int v = G[u][i];
                if(!dy[v])
                {
                    dy[v] = dx[u] + 1;
                    if(my[v] == -1)//未匹配 说明找到增广路
                        flag = true;
                    else
                    {
                        dx[my[v]] = dx[u] + 1;
                        Q.push(my[v]);
                    }
                }
            }
        }
        if(!flag)//未找到增广路
            break;
        memset(used, false, sizeof(used));
        for(int i = 1; i <= numx; i++)
            if(mx[i] == -1)
                ans += DFS(i);
    }
    printf("%d\n", ans / 2);
}
void getMap()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        G[i].clear();
    m = 3*n/2;
    numx = numy = n;
    while(m--)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        G[a].push_back(b);
        G[b].push_back(a);
    }
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        getMap();
        HK_match();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值