hihocoder 1122最大二分匹配匈牙利算法

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <stack>
#include <set>
#include <map>
#include <vector>

using namespace std;
#define INF 0x2fffffff
#define LL long long
#define MAX(a,b) ((a)>(b))?(a):(b)
#define MIN(a,b) ((a)<(b))?(a):(b)
struct node{
    int from,to,next;
};
node es[10005];
int head[1005];
int cnt;
int vis[1005];
int c[1005];
int n,m;
int add(int from,int to){
    es[cnt].from = from ;
    es[cnt].to = to;
    es[cnt].next = head[from];
    head[from] = cnt++;
}
int path(int x){
    for(int t = head[x];t != 0;t = es[t].next){
        if(!vis[es[t].to]){
            vis[es[t].to] = 1;
            if(!c[es[t].to] || path(c[es[t].to])){
                c[es[t].to] = x;
                c[x] = es[t].to;
                return 1;
            }
        }
    }
    return 0;
}
int max_flow(){
    memset(c,0,sizeof(c));
    int ans = 0;
    for(int i = 1;i <= n;i++){
        if(!c[i]){
            memset(vis,0,sizeof(vis));
            ans += path(i);
        }
    }
    return ans;
}
int main(){
    while(cin >> n >> m){
        memset(head,0,sizeof(head));
        cnt = 1;
        for(int i = 0;i < m;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        cout << max_flow() << endl;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-14 14:11:01

hihocoder 1122最大二分匹配匈牙利算法的相关文章

hihocoder 1122 二分图最大匹配之匈牙利算法

题目链接:http://hihocoder.com/problemset/problem/1122 , 匈牙利算法裸题. 刚刚学的二分匹配,还是要多刷题. 这道题可以直接套模板,我是根据题目上面的来做的,所以就先加了个染色优化,效果一般吧. #include <iostream> #include <cstdio> #include <vector> #include <queue> #include <cmath> #include <s

最大二分匹配匈牙利算法的python实现

二分图匹配是很常见的算法问题,一般用匈牙利算法解决二分图最大匹配问题,但是目前网上绝大多数都是C/C++实现版本,没有python版本,于是就用python实现了一下深度优先的匈牙利算法,本文使用的是递归的方式以便于理解,然而迭代的方式会更好,各位可以自行实现. 1.二分图.最大匹配 什么是二分图:二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(

HDU 1068 Girls and Boys(二分匹配--匈牙利算法)

Problem Description the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to

【01染色法判断二分匹配+匈牙利算法求最大匹配】HDU The Accomodation of Students

http://acm.hdu.edu.cn/showproblem.php?pid=2444 [DFS染色] 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<cmath> 6 #include<algorithm> 7 8 using namespace std; 9 const int maxn=2e2

HDU 3729 二分匹配匈牙利算法

I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1482    Accepted Submission(s): 740 Problem Description After this year’s college-entrance exam, the teacher did a survey in

HDU-3729 二分匹配 匈牙利算法

题目大意:学生给出其成绩区间,但可能出现矛盾情况,找出合理组合使没有说谎的人尽可能多,并按maximum lexicographic规则输出组合. //用学生去和成绩匹配,成绩区间就是学生可以匹配的成绩 #include <iostream> #include <queue> #include <vector> #define N 100005 using namespace std; struct Node { int f,t; }; Node lis[65]; in

hdu 2063 (二分匹配 匈牙利算法)

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 22693    Accepted Submission(s): 9797 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做par

USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John r

HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 49    Accepted Submission(s): 14 Problem Description There is a kindom of obsession, so people in this kingdom do things very