graph | hungary
匈牙利算法,求二分图最大匹配。
若P是图G中一条连通两个未匹配顶点的路径,并且属于M的边和不属于M的边(即已匹配和待匹配的边)在P上交替出现,则称P为相对于M的一条增广路径。(M为一个匹配)
由增广路的定义可以推出下述三个结论:
- P的路径长度必定为奇数,第一条边和最后一条边都不属于M。所以Line 25-27从first part出发,不从二分图的另一部分出发。Line 12实现了交替出现的逻辑;node->neig匹配,当且仅当neig没有被其他点匹配,或者neig被first中的其他点matches[neig]匹配,并且从matches[neig]能够找到一条增广路径。这里就实现了交替的逻辑了。
- 将M和P进行异或操作(去同存异)可以得到一个更大的匹配M’。这是因为,属于M的边和不属于M的边交替出现,且第一和最后一条边都不属于M,所以增广路径中,不属于M的边比属于M的边多1,去同存异之后,一定会得到一个更大的匹配(加1了)。Line 13实现的是去同存异的逻辑。如果从node到neig存在一条增广路径,那么中间这些相同的部分直接省略。
- M为G的最大匹配当且仅当不存在M的增广路径。
#include <iostream>
#include <cstdio>
#include <vector> using namespace std; bool augment(vector<vector<int> > &adj, int node,
vector<bool> &visited, vector<int> &matches) {
for (auto neig : adj[node]) {
if (!visited[neig]) {
visited[neig] = true;
if (matches[neig] == - || augment(adj, matches[neig], visited, matches)) {
matches[neig] = node;
return true;
}
}
}
return false;
} int hungary(vector<vector<int> > &adj, vector<int> &first) {
vector<bool> visited;
vector<int> matches(adj.size(), -);
int count = ;
for (auto f : first) {
visited.assign(adj.size(), false);
if (augment(adj, f, visited, matches)) {
count++;
}
}
for (int i = ; i < adj.size(); ++i) {
cout << i << "<->" << matches[i] << endl;
}
return count;
} int main(int argc, char** argv) {
freopen("input.txt", "r", stdin);
int first, n, m;
cin >> n >> first >> m;
vector<vector<int> > adj(n);
vector<int> left;
for (int i = ; i < first; ++i) {
int l;
cin >> l;
left.push_back(l);
}
for (int i = ; i < m; ++i) {
int n1, n2;
cin >> n1 >> n2;
adj[n1].push_back(n2);
adj[n2].push_back(n1);
} cout << hungary(adj, left) << endl;
return ;
}
时间复杂度是O(VE),空间复杂度感觉O(V)就行了啊,为什么其他人都说是O(V+E)?.
graph | hungary的更多相关文章
- [开发笔记] Graph Databases on developing
TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 讲座:Influence maximization on big social graph
Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...
- zabbix利用api批量添加item,并且批量配置添加graph
关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...
- Theano Graph Structure
Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...
随机推荐
- NHibernate中多表(对象)间的查询
一个比较简单的查询代码如下: IList userList=session.Find (" from testMSSql.student as student where student ...
- 并查集(涂色问题) HDOJ 4056 Draw a Mess
题目传送门 题意:给出一个200 * 50000的像素点矩阵,执行50000次操作,每次把一个矩形/圆形/菱形/三角形内的像素点涂成指定颜色,问最后每种颜色的数量. 分析:乍一看,很像用线段树成段更新 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- HBase Shell 常见操作
1.一般操作 status 查看状态 version 查看HBase版本 2.DDL操作 create 'member','member_id','address','info' 创建了一个membe ...
- Robotium编写测试用例如何模拟Junit4的BeforeClass和AfterClass方法1 - 条件判断法
本文来源于:http://blog.csdn.net/zhubaitian/article/details/39293883 Robotium的测试类ActivityInstrumentationTe ...
- Python学习笔记04
语句之后有冒号,表示有一个语句块,且以四个空格的缩进来表示隶属关系. 与C# 相比,没有了{},没有了(),被冒号和缩进取代了 if,while,for,range,continue,break if ...
- tomcat与HTML命令提示符
在tomcatwebapps目录下建立一个新文件夹 命名为my 把第一个学习的HTML文件放到my文件夹内 通过tomcat服务器远程访问该网页 把localhost换成自己的IP地址 先查看自己的I ...
- 424 - Integer Inquiry
Integer Inquiry One of the first users of BIT's new supercomputer was Chip Diller. He extended his ...
- [转]复制虚拟机后linux中的eth0变成eth1问题
为什么原来的eth0会变成eth1? 很多Linux distribution使用udev动态管理设备文件,并根据设备的信息对其进行持久化命名.udev会在系统引导的过程中识别网卡,将mac地址和网卡 ...
- bzoj3083 遥远的国度 题解
题目大意: 给定一棵有根树,每个点有一个权值,提供三种操作: 1.将x节点变为根节点 2.将x到y路径上的点的权值全部改为v 3.询问x的子树中点权的最小值 思路: 用DFS序剖分,记录每个节点入栈出 ...