B1192 [HNOI2006]超级英雄Hero 二分图匹配

先检讨一下,前一段时间开学,做题懒得发博客,也不总结。。。现在捡起来。

这个题一看是裸的二分图匹配,但是仔细一看还有一些区别,就是必须要连续的连接,否则直接退出。因为前一道题答不出来的话后面的题就没有机会了。

顺便练一下匈牙利算法,跑二分图还是很好写的。

题干:

Description
现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的
多少获得不同数目的奖品或奖金。主持人问题准备了若干道题目,只有当选手正确回答一道题后,才能进入下一题
,否则就被淘汰。为了增加节目的趣味性并适当降低难度,主持人总提供给选手几个“锦囊妙计”,比如求助现场
观众,或者去掉若干个错误答案(选择题)等等。这里,我们把规则稍微改变一下。假设主持人总共有m道题,选
手有n种不同的“锦囊妙计”。主持人规定,每道题都可以从两种“锦囊妙计”中选择一种,而每种“锦囊妙计”
只能用一次。我们又假设一道题使用了它允许的锦囊妙计后,就一定能正确回答,顺利进入下一题。现在我来到了
节目现场,可是我实在是太笨了,以至于一道题也不会做,每道题只好借助使用“锦囊妙计”来通过。如果我事先
就知道了每道题能够使用哪两种“锦囊妙计”,那么你能告诉我怎样选择才能通过最多的题数吗?
Input
输入文件的一行是两个正整数n和m(0 < n <1001,0 < m < 1001)表示总共有n中“锦囊妙计”,编号为0~n-1,总共有m个问题。
以下的m行,每行两个数,分别表示第m个问题可以使用的“锦囊妙计”的编号。
注意,每种编号的“锦囊妙计”只能使用一次,同一个问题的两个“锦囊妙计”可能一样。
Output
第一行为最多能通过的题数p
Sample Input
5 6 
3 2
2 0 
0 3 
0 4
3 2
3 2
Sample Output
4
HINT
Source

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = 1 << 30;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
int line[1005][1005];
int n,m,len = 0,p;
int g[1005];
int vis[1005];
bool find(int i)
{
    duke(j,0,n - 1)
    {
        if(line[i][j] && !vis[j])
        {
            vis[j] = 1;
            if(!g[j] || find(g[j]))
            {
                g[j] = i;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    read(n);read(m);
    duke(i,1,m)
    {
        int x,y;
        read(x);read(y);
        line[i][x] = line[i][y] = 1;
    }
    int ans = 0;
    duke(i,1,m)
    {
        clean(vis);
        if(find(i))
        ans++;
        else
        break;
    }
    write(ans);
    printf("
");
    duke(i,1,ans)
    {
        printf("%d
",g[i]);
    }
    return 0;
}
/*
5 6 
3 2
2 0 
0 3 
0 4
3 2
3 2
*/
原文地址:https://www.cnblogs.com/DukeLv/p/9634020.html