# BZOJ 1191 超级英雄Hero 二分图匹配

https://www.lydsy.com/JudgeOnline/problem.php?id=1191

，否则就被淘汰。为了增加节目的趣味性并适当降低难度，主持人总提供给选手几个“锦囊妙计”，比如求助现场

 1 #include<bits/stdc++.h>
2 #define IOS ios::sync_with_stdio(false);//不可再使用scanf printf
3 #define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数，会超时
4 #define Min(a, b) ((a) < (b) ? (a) : (b))
5 #define Mem(a) memset(a, 0, sizeof(a))
6 #define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1))
7 #define MID(l, r) ((l) + ((r) - (l)) / 2)
8 #define lson ((o)<<1)
9 #define rson ((o)<<1|1)
11 using namespace std;
13 {
14     int x=0,f=1;char ch=getchar();
15     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
16     while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
17     return x*f;
18 }
19
20 typedef long long ll;
21 const int maxn = 1000 + 10;
22 const int MOD = 1000000007;//const引用更快，宏定义也更快
23 const int INF = 1e9 + 7;
24 const double eps = 1e-6;
25 vector<int>Map[maxn];
26 int cx[maxn], cy[maxn];
27 bool vis[maxn];
28 int nx, ny;
29 bool dfs(int u)
30 {
31     for(int i = 0; i < Map[u].size(); i++)
32     {
33         int v = Map[u][i];
34         if(!vis[v])
35         {
36             vis[v] = 1;
37             if(cy[v] == -1 || dfs(cy[v]))
38             {
39                 cx[u] = v;
40                 cy[v] = u;
41                 return 1;
42             }
43         }
44     }
45     return 0;
46 }
47 int maxmatch()
48 {
49     int ans = 0;
50     memset(cx, -1, sizeof(cx));
51     memset(cy, -1, sizeof(cy));
52     for(int i = 0; i < ny; i++)
53     {
54         if(cx[i] == -1)
55         {
56             memset(vis, 0, sizeof(vis));
57             if(dfs(i))ans++;
58             else return ans;//题目要求按顺序匹配从0到ny-1
59         }
60     }
61     return ans;
62 }
63
64 int main()
65 {
66     scanf("%d%d", &nx, &ny);
67     int x, y;
68     for(int i = 0; i < ny; i++)
69     {
70         scanf("%d%d", &x, &y);
71         Map[i].push_back(x);
72         Map[i].push_back(y);
73     }
74     printf("%d\n", maxmatch());
75     return 0;
76 }

posted @ 2018-09-19 22:33  _努力努力再努力x  阅读(129)  评论(0编辑  收藏  举报