【Link】:https://hihocoder.com/problemset/problem/1122

【Description】

【Solution】



二分图匹配,匈牙利算法模板题;

这里我先把染成0的放在一个vector里面,然后再进行匈牙利算法.



【NumberOf WA】

【Reviw】

【Code】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x+1)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1000; vector <int> g[N+100],v[2];
int pre[N+10],Try[N+10],color[N+10];
int n,m; bool hungary(int x){
int len = g[x].size();
for (int i = 0;i <= len-1;i++){
int y = g[x][i];
if (!Try[y]){
Try[y] = 1;
if ( pre[y] == -1 || hungary(pre[y])){
pre[y] = x;
return 1;
}
}
}
return 0;
} void dfs(int x,int c){
color[x] = c;v[c].pb(x);
int len = g[x].size();
rep1(i,0,len-1){
int y = g[x][i];
if (color[y]==-1) dfs(y,1-c);
}
} int main(){
//Open();
//Close();
ri(n),ri(m);
rep1(i,1,m){
int x,y;
ri(x),ri(y);
g[x].pb(y),g[y].pb(x);
} ms(color,255);
rep1(i,1,n)
if (color[i] == -1)
dfs(i,0); ms(pre,255);
int ans = 0,len = v[0].size();
rep1(i,0,len-1){
ms(Try,0);
if (hungary(v[0][i])) ans++;
} oi(ans);puts("");
return 0;
}

【hihocoder 1122】二分图二•二分图最大匹配之匈牙利算法的更多相关文章

  1. HihoCoder 1122二分图二 ---最大匹配之匈牙利算法

    二分图二•二分图最大匹配之匈牙利算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上一回我们已经将所有有问题的相亲情况表剔除了,那么接下来要做的就是安排相亲了.因为过 ...

  2. hihoCoder #1127 : 二分图二·二分图最小点覆盖和最大独立集

    #1127 : 二分图二·二分图最小点覆盖和最大独立集 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 在上次安排完相亲 ...

  3. 二分图最大匹配:匈牙利算法的python实现

    二分图匹配是很常见的算法问题,一般用匈牙利算法解决二分图最大匹配问题,但是目前网上绝大多数都是C/C++实现版本,没有python版本,于是就用python实现了一下深度优先的匈牙利算法,本文使用的是 ...

  4. 51nod 2006 飞行员配对(二分图最大匹配) 裸匈牙利算法 求二分图最大匹配题

    题目: 题目已经说了是最大二分匹配题, 查了一下最大二分匹配题有两种解法, 匈牙利算法和网络流. 看了一下觉得匈牙利算法更好理解, 然后我照着小红书模板打了一遍就过了. 匈牙利算法:先试着把没用过的左 ...

  5. "《算法导论》之‘图’":不带权二分图最大匹配(匈牙利算法)

    博文“二分图的最大匹配.完美匹配和匈牙利算法”对二分图相关的几个概念讲的特别形象,特别容易理解.本文介绍部分主要摘自此博文. 还有其他可参考博文: 趣写算法系列之--匈牙利算法 用于二分图匹配的匈牙利 ...

  6. 二分图最大匹配(匈牙利算法)简介& Example hdu 1150 Machine Schedule

    二分图匹配(匈牙利算法) 1.一个二分图中的最大匹配数等于这个图中的最小点覆盖数 König定理是一个二分图中很重要的定理,它的意思是,一个二分图中的最大匹配数等于这个图中的最小点覆盖数.如果你还不知 ...

  7. 【模板】二分图最大匹配(匈牙利算法)/洛谷P3386

    题目链接 https://www.luogu.com.cn/problem/P3386 题目大意 给定一个二分图,其左部点的个数为 \(n\),右部点的个数为 \(m\),边数为 \(e\),求其最大 ...

  8. 无权二分图最大匹配 HDU2063 匈牙利算法 || Hopcroft-Karp

    参考两篇比较好的博客 http://www.renfei.org/blog/bipartite-matching.html http://blog.csdn.net/thundermrbird/art ...

  9. HDU 2063 过山车 (最大匹配,匈牙利算法)

    题意:中文题目 思路:匈牙利算法解决二分图最大匹配问题. #include <bits/stdc++.h> using namespace std; ; int mapp[N][N]; / ...

随机推荐

  1. JS动态创建表单post提交

    <script> //@创建表单方法 function post(URL, PARAMS) { var temp = document.createElement("form&q ...

  2. [洛谷P3932]浮游大陆的68号岛

    题目大意:有一行物品,每两个物品之间有一个距离.每个物品有一个价值.现在问你若干问题,每个问题问你把l~r所有物品全部搬到物品x处需要多少价值. 把物品a搬到物品b处的价值为物品a的价值乘a到b的距离 ...

  3. [NOI2016]优秀的拆分(SA数组)

    [NOI2016]优秀的拆分 题目描述 如果一个字符串可以被拆分为 \(AABB\) 的形式,其中 A和 B是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串 \(aabaaba ...

  4. Java基础学习总结(15)——java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  5. 怎样查看电脑的IP地址

    在DOW窗体 :cmd->ipconfig 见截图:

  6. An internal error occurred during: &quot;Checking tomcat state&quot;. Error while reading server.xml

    An internal error occurred during: "Checking tomcat state". Error while reading server.xml ...

  7. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

  8. .Net商品管理(注释,百度,提问,对比,总结)

    管理控制器 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sys ...

  9. 24.桌面移动qq

    #include <stdlib.h> #include <Windows.h> #include <stdio.h> #include <math.h> ...

  10. java 文件读写demo

    分析错误日志: import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public ...