bzoj1191 [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

正解:二分图最大匹配。

匈牙利算法即可,但是题面巨坑,要按顺序答题,也就是说,如果当前没办法匹配就要break。

 

 1 //It is made by wfj_2048~
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <complex>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <cmath>
10 #include <queue>
11 #include <stack>
12 #include <map>
13 #include <set>
14 #define inf (1<<30)
15 #define il inline
16 #define RG register
17 #define ll long long
18  
19 using namespace std;
20  
21 struct edge{ int nt,to; }g[4010];
22  
23 int head[2010],vis[2010],lk[2010],n,m,num,cnt,ans;
24  
25 il int gi(){
26     RG int x=0,q=1; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
27     if (ch=='-') q=-1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q*x;
28 }
29  
30 il void insert(RG int from,RG int to){ g[++num]=(edge){head[from],to},head[from]=num; return; }
31  
32 il int match(RG int x){
33     for (RG int i=head[x];i;i=g[i].nt){
34     RG int v=g[i].to; if (vis[v]==cnt) continue;
35     vis[v]=cnt; if (!lk[v] || match(lk[v])){ lk[v]=x; return 1; }
36     }
37     return 0;
38 }
39  
40 il int maxmatch(){ for (RG int i=1;i<=n;++i){ cnt++; if (match(i)) ans++; else break; } return ans; }
41  
42 il void work(){
43     m=gi(),n=gi(); RG int x,y;
44     for (RG int i=1;i<=n;++i) x=gi()+1,y=gi()+1,insert(i,x),insert(i,y);
45     printf("%d\n",maxmatch()); return;
46 }
47  
48 int main(){
49     work();
50     return 0;
51 }

 

posted @ 2017-02-23 13:52  wfj_2048  阅读(145)  评论(0编辑  收藏  举报