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 ...
随机推荐
- 转 Delphi Invalidate的用法
1.Invalidate介绍 void Invalidate( BOOL bErase = TRUE ); 该函数的作用是使整个窗口客户区无效.窗口的客户区无效意味着需要重绘,例如,如果一个被其它窗口 ...
- 学习资源asp.net
http://www.runoob.com ajax 同一表单,多部分提交.增加,修改,删除 服务器端控件: http://technet.microsoft.com/zh-cn/library/cc ...
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- 【转】Xcelsius2008 水晶易表问题 部分汇总
要使用 Xcelsius 2008,需要安装 Adobe Flash 吗? 若要正常运行 Xcelsius 2008,必须安装 Adobe Flash Player 版本 9.如果在安装过程中没有安装 ...
- iOS学习17之OC内存管理
1.内存管理的方式 1> iOS应用程序出现Crash(闪退),90%的原因是因为内存问题. 2> 内存问题 野指针异常:访问没有所有权的内存,如果想要安全的访问,必须确保空间还在 内存泄 ...
- Storm中Spout使用注意事项小结
Storm中Spout用于读取并向计算拓扑中发送数据源,最近在调试一个topology时遇到了系统qps低,处理速度达不到要求的问题,经过排查后发现是由于对Spout的使用模式不当导致的多线程同步等待 ...
- IP地址分类整理
什么是IP地址? IP地址就是计算机在网络中地址. IP地址有多少个? IP地址范围是:0.0.0.0~225.225.225.255,这只是人为了方便记录才转为十进制的,ip地址实际是一个32位地址 ...
- bg,fg,ctrl+z组合
使用ctrl + Z 把一个进程挂起 [root@limt ~]# sh Testlsof.sh >111.log ^Z [1]+ Stopped sh Testlsof.sh > 111 ...
- 【bzoj1455】罗马游戏 可并堆
2016-05-31 10:04:41 可并堆的裸题. 左偏树(小根堆为例 性质 1.满足堆的性质,每个节点权值小于左右儿子权值 2.每个节点有dis值,表示子树最浅的叶子深度加1 3.左子树dis ...
- BZOJ3343: 教主的魔法 分块
2016-05-28 10:27:19 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3343 比较显然的分块题,分块后块内排序,维护整块的附 ...