题意:John先生晚上写了n封信,并相应地写了n个信封将信装好,准备寄出。但是,第二天John的儿子Small John将这n封信都拿出了信封。不幸的是,Small John无法将拿出的信正确地装回信封中了。
将Small John所提供的n封信依次编号为1,2,…,n;且n个信封也依次编号为1,2,…,n。假定Small John能提供一组信息:第i封信肯定不是装在信封j中。请编程帮助Small John,尽可能多地将信正确地装回信封。
思路:同hdoj1281,枚举删除关系,看最大匹配数是否发生变化,如果是,那这个关系就是关键关系,可以确定的。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 205;
int match[maxn], n, m;
bool book[maxn][maxn];
bool vis[maxn];
vector<int> g[maxn];
bool dfs(int x)
{
for(int i = 0; i < g[x].size(); i++)
{
int v = g[x][i];
if(!vis[v])
{
vis[v] = 1;
if(match[v] == -1 || dfs(match[v]))
{
match[v] = x;
return 1;
}
}
}
return 0;
}
int Hungary()
{
int res = 0;
for(int i = 1; i <= n; i++)
{
memset(vis, 0, sizeof(vis));
res += dfs(i);
}
return res;
}
int main(void)
{
while(cin >> n)
{
memset(match, -1, sizeof(match));
memset(book, 0, sizeof(book));
for(int i = 0; i < maxn; i++)
g[i].clear();
int a, b;
while(cin >> a >> b, a+b)
book[a][b] =1;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(!book[i][j])
g[i].push_back(j);
int ans = Hungary();
bool have = 0;
for(int i = 1; i <= n; i++)
for(int j = 0; j < g[i].size(); j++)
{
int tmp = *(g[i].begin());
g[i].erase(g[i].begin());
memset(match, -1, sizeof(match));
int cur = Hungary();
if(cur != ans)
{
printf("%d %d\n", i, tmp);
have = 1;
}
g[i].push_back(tmp);
}
if(!have) puts("none");
}
return 0;
}