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 jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 #ifdef WIN32   
14 #define OT "%I64d"
15 #else
16 #define OT "%lld"
17 #endif
18 using namespace std;
19 typedef long long LL;
20 const int MAXM = 2011;
21 int n,m,match[MAXM],ecnt,ans;
22 bool vis[MAXM];
23 int first[MAXM],next[MAXM*10],to[MAXM*10];
24 
25 inline int getint()
26 {
27        int w=0,q=0;
28        char c=getchar();
29        while((c<'0' || c>'9') && c!='-') c=getchar();
30        if (c=='-')  q=1, c=getchar();
31        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
32        return q ? -w : w;
33 }
34 
35 inline void link(int x,int y){
36     next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y;
37     next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x;
38 }
39 
40 inline bool dfs(int x){
41     vis[x]=1;
42     for(int i=first[x];i;i=next[i]) {
43     int v=to[i];
44     if(vis[v]) continue;
45     vis[v]=1;
46     if(!match[v] || dfs(match[v])) {
47         match[v]=x; match[x]=v;
48         return true;
49     }
50     }
51     return false;
52 }
53 
54 inline void work(){
55     n=getint(); m=getint(); int x,y;
56     for(int i=1;i<=m;i++) {
57     x=getint()+1; y=getint()+1;
58     link(i+n,x); link(i+n,y);
59     }
60     for(int i=1;i<=m;i++) {
61     memset(vis,0,sizeof(vis));
62     if(dfs(i+n)) ans++; else break;
63     }
64     printf("%d",ans);
65 }
66 
67 int main()
68 {
69   work();
70   return 0;
71 }

 

posted @ 2016-08-13 11:56  ljh_2000  阅读(261)  评论(0编辑  收藏  举报