链接:https://vjudge.net/problem/HDU-1845
题意:
给一个有向图,求最大匹配。
思路:
有相图的最大匹配,可以通过加上反向边, 求这个无向图的最大匹配,
原图的最大匹配就是无向图的最大匹配除2.
详细解释:https://xwk.iteye.com/blog/2129301
https://blog.csdn.net/u013480600/article/details/38638219
代码:
#include <iostream> #include <memory.h> #include <string> #include <istream> #include <sstream> #include <vector> #include <stack> #include <algorithm> #include <map> #include <queue> #include <math.h> #include <cstdio> #include <set> #include <iterator> #include <cstring> using namespace std; typedef long long LL; const int MAXN = 5e3+10; int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; vector<int> G[MAXN]; int Link[MAXN], Vis[MAXN]; int n, m, k; bool Dfs(int x) { for (int i = 0;i < G[x].size();i++) { int node = G[x][i]; if (Vis[node] == 0) { Vis[node] = 1; if (Link[node] == -1 || Dfs(Link[node])) { Link[node] = x; return true; } } } return false; } int Solve() { memset(Link, -1, sizeof(Link)); int cnt = 0; for (int i = 1;i <= n;i++) { memset(Vis, 0, sizeof(Vis)); if (Dfs(i)) cnt++; } return cnt; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 1;i <= n;i++) G[i].clear(); int l, r; for (int i = 1;i <= 3*n/2;i++) { scanf("%d%d", &l, &r); G[l].push_back(r); G[r].push_back(l); } int cnt = Solve(); printf("%d ", cnt/2); } return 0; }