zoukankan      html  css  js  c++  java
  • HDU 1845 Jimmy’s Assignment

    Jimmy’s Assignment



    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
     
    #include<cstdio>
    #include<queue>
    #include<cctype>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    #define pb push_back
    using namespace std;
    const int INF=0x3f3f3f3f;
    int n,k,vis[5005],mx[5005],my[5005],dx[5005],dy[5005],dis;
    vector<int>G[5005];
    bool dfs(int u)
    {
        for(int len=G[u].size(),i=0;i<len;i++)
        {
            int v=G[u][i];
            if(!vis[v]&&dy[v]==dx[u]+1)
            {
                vis[v]=1;
                if(my[v]!=-1&&dy[v]==dis)continue;
                if(my[v]==-1||dfs(my[v]))
                {
                    my[v]=u;
                    mx[u]=v;
                    return 1;
                }
            }
        }
        return 0;
    }
    bool bfs()
    {
        queue<int>Q;
        dis=INF;
        memset(dx,-1,sizeof(dx));
        memset(dy,-1,sizeof(dy));
        for(int i=1;i<=n;i++)
        {
            if(mx[i]==-1)
                Q.push(i),dx[i]=0;
        }
        while(!Q.empty())
        {
            int u=Q.front();
            Q.pop();
            if(dx[u]>dis)break;
            for(int i=0,len=G[u].size();i<len;i++)
            {
                int v=G[u][i];
                if(dy[v]==-1)
                {
                    dy[v]=dx[u]+1;
                    if(my[v]==-1)dis=dy[v];
                    else
                    {
                        dx[my[v]]=dy[v]+1;
                        Q.push(my[v]);
                    }
                }
            }
        }
        return dis!=INF;
    }
    int match()
    {
        int ans=0;
        memset(mx,-1,sizeof(mx));
        memset(my,-1,sizeof(my));
        while(bfs())
        {
            memset(vis,0,sizeof(vis));
            for(int i=1;i<=n;i++)
            {
                if(mx[i]==-1&&dfs(i))
                    ans++;
            }
        }
        return ans;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            for(int i=1;i<=n;i++)G[i].clear();
            for(int i=0;i<3*n/2;i++)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                G[u].pb(v),G[v].pb(u);
            }
            printf("%d
    ",match()>>1);
        }
        return 0;
    }
  • 相关阅读:
    扫雷游戏:C++生成一个扫雷底板
    为《信息学奥赛一本通》平反(除了OJ很差和代码用宋体)
    关于最大公约数欧几里得算法辗转相除及最小公倍数算法拓展C++学习记录
    【Python】如何用较快速度用Python程序增加博客访问量——CSDN、博客园、知乎(应该包括简书)
    STL学习笔记之next_permutation函数
    C++集合容器STL结构Set
    11111111111
    express + mysql实践
    express实践
    ws:一个 Node.js WebSocket 库
  • 原文地址:https://www.cnblogs.com/homura/p/5884067.html
Copyright © 2011-2022 走看看