解题思路:
AC代码:
// Test1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//DAG的最小路径覆盖数 = DAG图中的节点数 - 相应二分图中的最大匹配数
//因为不同路径可以含有相同的点,需要给能到达的点之间都连上边才能转化为裸的路径覆盖
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 510;
int T, n, m;
int graph[maxn][maxn], match[maxn], vis[maxn];
void Init() {
int x, y;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
graph[x][y] = 1;
}
}
void aftermath() {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
graph[i][j] = 0;
}
bool dfs(int x) {
for (int i = 1; i <= n; i++) {
if (graph[x][i] && !vis[i]) {
vis[i] = 1;
if (!match[i] || dfs(match[i])) {
match[i] = x;
return true;
}
}
}
return false;
}
int Maxmatch() {
int sum = 0;
memset(match, 0, sizeof(match));
for (int i = 1; i <= n; i++) {
memset(vis, 0, sizeof(vis));
if (dfs(i)) sum++;
}
return sum;
}
void Floyd() {
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (graph[i][k] && graph[k][j])
graph[i][j] = 1;
}
int main() {
while (cin >> n >> m && (n || m)) {
Init();
Floyd();
int ans = n - Maxmatch();
cout << ans << endl;
aftermath();
}
return 0;
}
类似题目:P6061 [加油武汉]疫情调查
本文地址:https://blog.csdn.net/weixin_45691711/article/details/107284242
如对本文有疑问, 点击进行留言回复!!
M - Jamie‘s Contact Groups POJ - 2289(二分 + 多重匹配)
H - Antenna Placement POJ - 3020(最小边覆盖)
Unity组件【Camera && Flare Layer】
K - Treasure Exploration POJ - 2594
网友评论