zoukankan      html  css  js  c++  java
  • 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): 464    Accepted Submission(s): 224


    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
     
    Recommend
    lcy
     

    水题来一发

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    
    const int MAXN = 5010;//点数的最大值
    const int MAXM = 50010;//边数的最大值
    struct Edge
    {
        int to,next;
    }edge[MAXM];
    int head[MAXN],tot;
    void init()
    {
        tot = 0;
        memset(head,-1,sizeof(head));
    }
    void addedge(int u,int v)
    {
        edge[tot].to = v; edge[tot].next = head[u];
        head[u] = tot++;
    }
    int linker[MAXN];
    bool used[MAXN];
    int uN;
    bool dfs(int u)
    {
        for(int i = head[u]; i != -1 ;i = edge[i].next)
        {
            int v = edge[i].to;
            if(!used[v])
            {
                used[v] = true;
                if(linker[v] == -1 || dfs(linker[v]))
                {
                    linker[v] = u;
                    return true;
                }
            }
        }
        return false;
    }
    int hungary()
    {
        int res = 0;
        memset(linker,-1,sizeof(linker));
        for(int u = 0; u < uN;u++)
        {
            memset(used,false,sizeof(used));
            if(dfs(u))res++;
        }
        return res;
    }
    int main()
    {
        int T;
        int n;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            int m = n*3/2;
            int u,v;
            init();
            uN = n;
            while(m--)
            {
                scanf("%d%d",&u,&v);
                u--; v--;
                addedge(u,v);
                addedge(v,u);
            }
            printf("%d
    ",hungary()/2);
        }
        return 0;
    }
  • 相关阅读:
    android代码签名和混乱的包装
    C# 中对WinForm窗体中的控件快速设置TableIndex次序
    常用的Oracle数据库语句 (待更新完毕)
    [转] C# 键盘中的按键对应的KeyValue
    Oracle 数据库中日期时间的插入操作
    C#操作Excel,对Sheet插入次序的控制 (有待完善)
    Simditor图片上传
    文件类似的推理 -- 超级本征值(super feature)
    leetcode 名单 Insertion Sort List
    汉高澳大利亚sinox2014电影播放flash最好的办法是安装游戏windows文本firefox
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3219178.html
Copyright © 2011-2022 走看看