Bzoj 1854(二分图最大匹配)

BZOJ 1854
这题要把属性放到左边,武器编号放右边来二分图最大匹配,不同于Bzoj 1191

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
using namespace std;
const int MAXN = 1000000 + 5;
int msx, n, cnt, lk[MAXN], vis[MAXN];
vector<int> G[MAXN];
bool hungary(int u) {
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (vis[v] != cnt) {
vis[v] = cnt;
if (!lk[v] || hungary(lk[v])) {
lk[v] = u;
return true;
}
}
}
return false;
}
void clean() {
msx = 0;
for (int i = 0; i <= max(n, 10000 + 5); i++) lk[i] = 0, vis[i] = 0, G[i].clear();
}
void solve() {
clean();
for (int a, b, i = 1; i <= n; i++) {
scanf("%d%d", &a, &b);
msx = max(msx, max(a, b));
G[a].push_back(i), G[b].push_back(i);
//把属性放到左边,武器编号放右边来二分图最大匹配
}
for (int i = 1; i <= msx; i++) {
if (!hungary(cnt = i)) {
printf("%d\n", i - 1);
return ;
}
}
printf("%d\n", msx);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);freopen("1.out", "w", stdout);
#endif
scanf("%d", &n), solve();
return 0;
}

------ 本文结束 ------