# HDU - 3081 Marriage Match II 【二分匹配】

http://acm.hdu.edu.cn/showproblem.php?pid=3081

T T组数据

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
//#define bug
//#define gets gets_s

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;

const double PI = acos(-1.0);
const double EI = exp(1.0);
const double eps = 1e-8;

const int INF = 0x3f3f3f3f;
const int maxn = 7e3 + 5e2 + 10;
const int MOD = 6;

const int MAXN = 110;
int uN, vN;//u,v的数目，使用前面必须赋值
int g[MAXN][MAXN];//邻接矩阵
bool used[MAXN];
bool dfs(int u)
{
for (int v = 0; v < vN; v++)
if (g[u][v] && !used[v])
{
used[v] = true;
{
return true;
}
}
return false;
}
int hungary()
{
int res = 0;
for (int u = 0; u < uN; u++)
{
memset(used, false, sizeof(used));
if (dfs(u))res++;
}
return res;
}

int pre[MAXN];

int n, m, f;

int find(int x)
{
while (x != pre[x])
x = pre[x];
return x;
}

void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (fx != fy)
pre[fx] = fy;
}

void Flyod()
{
for (int i = 0; i < n; i++)
{
for (int k = 0; k < n; k++)
{
if (find(i) == find(k))
{
for (int j = 0; j < n; j++)
g[i][j] = g[k][j] = (g[i][j] || g[k][j]);
}
}
}
}

void clear()
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
g[i][j] = 0;
for (int i = 0; i < n; i++)
pre[i] = i;
}

int main()
{
int t;
cin >> t;
while (t--)
{
scanf("%d%d%d", &n, &m, &f);
clear();
int x, y;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
x--, y--;
g[x][y] = 1;
}
for (int i = 0; i < f; i++)
{
scanf("%d%d", &x, &y);
x--, y--;
join(x, y);
}
Flyod();
int tot = 0;
uN = vN = n;
int vis;
while (true)
{
vis = hungary();
if (vis != n)
break;
tot++;
for (int i = 0; i < n; i++)
}