标签:图
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281
题意:中文题目,不描述了。
解法:棋盘的行x看成二分图左边的点,列y看成二分图右边的点,那么就把可以放车的位置看成是一条边,而二分图的最大匹配中x互不相同,y互不相同,所以每个匹配都是不同行不同列,所以最大匹配就是最多可以放的车的数量。而要判断有多少个点是必须放的,只要在得出最大匹配后,每次去掉一个匹配,再去运算看得出的结果是否与原来的最大匹配数相同,若相同就不是必须的,若不相同就是必须的。
代码:
#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
using namespace std;
struct node
{
int x;
int y;
}it[10010];
int p[510][510];
int n, m, k;
int a, b;
int book[510];
int match[510];
bool dfs(int u)
{
for (int i = 1; i <= m; i++)
{
if (book[i] == 0 && p[u][i] == 1)
{
book[i] = 1;
if (match[i] == 0 || dfs(match[i]))
{
match[i] = u;
return true;
}
}
}
return false;
}
int main()
{
int ca = 1;
while (scanf("%d%d%d", &n, &m, &k) != EOF)
{
memset(p, 0, sizeof(p));
memset(match, 0, sizeof(match));
for (int i = 0; i < k; i++)
{
scanf("%d%d", &a, &b);
it[i].x = a;
it[i].y = b;
p[a][b] = 1;
}
int ans = 0;
for (int i = 1; i <= n; i++)
{
memset(book, 0, sizeof(book));
if (dfs(i))
ans++;
}
int tmp = 0;
for (int j = 0; j < k; j++)
{
p[it[j].x][it[j].y] = 0;
int ans1 = 0;
memset(match, 0, sizeof(match));
for (int i = 1; i <= n; i++)
{
memset(book, 0, sizeof(book));
if (dfs(i))
ans1++;
}
if (ans1 != ans)
tmp++;
p[it[j].x][it[j].y] = 1;
}
printf("Board %d have %d important blanks for %d chessmen.\n", ca++,tmp,ans);
}
return 0;
}
标签:图
原文地址:http://blog.csdn.net/u014427196/article/details/46537185