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

若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. XSS 跨站脚本攻击之构造剖析(一)

    1.XSS-Filter:跨站脚本过滤器,用于分析用户提交的输入,并消除潜在的跨站脚本攻击 (1)XSS Filter实际上是一段精心编写的过滤函数作用是过滤XSS跨站脚本代码: (2)绕过XSS F ...

  2. PHP 设置代码执行时间

    <?php ini_set('max_execution_time', '0'); set_time_limit(0); ?>

  3. FastMM配置文件详解

    FastMM最新版本提供了中文语言包,可方便国内使用.下载地址为 http://sourceforge.net/projects/fastmm/ 配置文件为:FastMM4Options.inc, 中 ...

  4. sed 字符串替换

    1. sed替换的基本语法为: sed 's/原字符串/替换字符串/' 单引号里面,s表示替换,三根斜线中间是替换的样式,特殊字符需要使用反斜线”\”进行转义. 2. 单引号” ‘ ’”是没有办法用反 ...

  5. 使用Soapui测试webservice接口

    本来想自己写一个的,但下面这个资料比较详细(截图和步骤细节),就省事了,具体内容见: http://blog.csdn.net/oracle_microsoft/article/details/568 ...

  6. PAT (BL) 1001

    链接:http://pat.zju.edu.cn/contests/pat-b-practise/1001 /* 1001. 害死人不偿命的(3n+1)猜想 (15) 卡拉兹(Callatz)猜想: ...

  7. 获取ItemsControl中当前item的binding数据

    直接用 {Binding} 就可以了,如下: <ItemsControl ItemsSource="{Binding Path=ProcessItems}"> < ...

  8. 安装SQL2008 提示 创建usersettings/microsoft.sqlserver.configuration.landingpage.properties.se

    System.Configuration.ConfigurationErrorsException: 创建 userSettings/Microsoft.SqlServer.Configuration ...

  9. java 程序访问hdfs错误 hadoop2.2.0

    很奇怪的问题,程序在eclipse上跑没问题: 这就代码:FileSystem fs = FileSystem.get(URI.create(hdfs_file),  conf , "use ...

  10. Oracle 存储过程学习

    转自:http://blog.chinaunix.net/uid-20495387-id-174394.html http://www.cnblogs.com/rootq/articles/11000 ...