zoukankan      html  css  js  c++  java
  • HDU1845Jimmy’s Assignment(无向图,最大匹配)

    题意:就是求最大匹配

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<set>
    #include<map>
    #include<string>
    #include<cstring>
    #include<stack>
    #include<queue>
    #include<vector>
    #include<limits>
    #include<ctime>
    #include<cassert>
    #include<cstdlib>
    #define lson (rt<<1),L,M
    #define rson (rt<<1|1),M+1,R
    #define M ((L+R)>>1)
    #define cl(a,b) memset(a,b,sizeof(a));
    #define LL long long
    #define P pair<int,int>
    #define X first
    #define Y second
    #define pb push_back
    #define fread(a) freopen(a,"r",stdin);
    #define fwrite(a) freopen(a,"w",stdout);
    using namespace std;
    const int maxn=5005;
    const int inf=999999;
    
    vector<int> G[maxn];
    int matching[maxn];
    bool vis[maxn];
    int num;
    bool dfs(int u){
        int N=G[u].size();
        for(int i=0;i<N;i++){
            int v=G[u][i];
            if(vis[v])continue;
            vis[v]=true;
            if(matching[v]==-1||dfs(matching[v])){
                matching[v]=u;
                return true;
            }
        }
        return false;
    }
    int hungar(){
        int ans=0;
        cl(matching,-1);
        for(int i=0;i<num;i++){
            cl(vis,false);
            if(dfs(i))ans++;
        }
        return ans/2;//无向边,算了所有。除2
    }
    
    
    int main(){
        int T;
        scanf("%d",&T);
        while(T--){
            int n;
            scanf("%d",&n);
            for(int i=0;i<=n;i++)G[i].clear();
            int m=n*3/2;
            while(m--){
                int x,y;
                scanf("%d%d",&x,&y);
                x--;y--;
                G[x].pb(y);//这里是无向边
                G[y].pb(x);
            }
            num=n;
            int ans=hungar();
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    C到C++的升级
    设计模式分类
    对模式的十大误解
    MAC帧和IP包的分析
    python之路—从入门到放弃
    选课系统
    Scrapy框架详解
    爬虫系列
    Python从入门到断气
    常用模块
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7236728.html
Copyright © 2011-2022 走看看