匈牙利算法,求二分图最大匹配。

若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的更多相关文章

  1. [开发笔记] Graph Databases on developing

    TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...

  2. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  3. POJ 2125 Destroying the Graph 二分图最小点权覆盖

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8198   Accepted: 2 ...

  4. [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), ...

  5. [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), ...

  6. [LeetCode] Clone Graph 无向图的复制

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  7. 讲座:Influence maximization on big social graph

    Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...

  8. zabbix利用api批量添加item,并且批量配置添加graph

    关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...

  9. Theano Graph Structure

    Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...

随机推荐

  1. JetS3t使用说明

    http://blog.csdn.net/hitmediaman/article/details/6636402

  2. Centos搭建Linux测试环境,几个基本的设置项

    一.添加sudo 权限 1.su - 切换至root,需要rootmim 2.修改 /etc/sudoers 的权限 chmod 644 /etc/sudoers 3.vi /etc/sudoers ...

  3. Golang 安装及配置教程 for Mac

    1.到golang.org下载golang 并安装. 2.安装sublimetext ,打开之后 按ctrl+` 打开命令行,输入以下内容: import urllib2,os; pf='Packag ...

  4. Redis开启持久化

    配置appendonly 打开redis.conf找到appendonly. 将 appendonly no 改为 appendonly yes 配置appendfsync appendfsync a ...

  5. CF# Educational Codeforces Round 3 E. Minimum spanning tree for each edge

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

  6. jquerymobile页面跳转和参数传递

    http://blog.csdn.net/chen052210123/article/details/7481578 页面跳转: 页面跳转时pagebeforechange事件会被触发两次,通过$(d ...

  7. Hadoop建立IPC连接和数据读写

    建立IPC连接 IPC Client通过调用getConnection获取IPC连接,具体流程图如下: 服务器端的IPC连接代码分散在Listener和Server.Connection中. List ...

  8. js 过滤敏感词

    <html> <head>     <title>Bad Words Example</title>     <script type=" ...

  9. jquery属性过滤选择器

    http://www.jb51.net/article/46279.htm   $("div[id]").addClass("highlight"); //查找 ...

  10. Codeforces Round #246 (Div. 2) A. Choosing Teams

    给定n k以及n个人已参加的比赛数,让你判断最少还能参加k次比赛的队伍数,每对3人,每个人最多参加5次比赛 #include <iostream> using namespace std; ...