Book Club 二分图最大匹配

来源:互联网 发布:adsafe mac版 编辑:程序博客网 时间:2024/06/07 13:56

Porto’s book club is buzzing with excitement for
the annual book exchange event! Every year, members
bring their favorite book and try to find another book
they like that is owned by someone willing to trade
with them.
I have been to this book exchange before, and I definitely
do not want to miss it this year, but I feel that
the trading should be improved. In the past, pairs of
members interested in each other’s books would simply
trade: imagine that person A brought a book that
person B liked and vice-versa, then A and B would
exchange their books.
I then realized that many members were left with the same book they walked-in with… If instead
of looking for pairs I looked for triplets, I could find more valid exchanges! Imagine that member A
only likes member B’s book, while B only likes C’s book and C likes A’s book. These 3 people could
trade their books in a cycle and everyone would be happy!
But why stop at triplets? Cycles could be bigger and bigger! Could you help me find if it is possible
for everyone to go out with a new book? Be careful, because members will not give their book without
receiving one they like in return.
Given the members of the book club and the books they like, can we find cycles so that everyone
receives a new book?
Input
The input file contains several test cases, each of them as described below.
The first line has two integers: N, the number of people, and M, the total number of “declarations
of interest”. Each of the following M lines has two integers, A and B, indicating that member A likes
the book that member B brought (0 ≤ A, B < N). Numbers A and B will never be the same (a
member never likes the book he brought).
Output
For each test case, you should output ‘YES’ if we can find a new book for every club member and ‘NO’
if that is not possible.
Constraints
2 ≤ N ≤ 10 000
1 ≤ M ≤ 20 000 and M ≤ N2 − N.
Sample Input
9 9
0 1
1 2
2 0
3 4
4 3
5 6
6 7
7 8
8 5
Sample Output
YES

n个人,每一个都不喜欢自己的书而喜欢别人的书,问每个人都能换到一本自己喜欢的书吗?

#include <bits/stdc++.h>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 20100;const int Mod = 1e9 + 7;#define ll long long#define mem(x, y) memset(x, y, sizeof(x))#define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);int N,M,vis[maxn],ans = 1,c[maxn];vector<int> nd[maxn];bool path(int u){        for(int i = 0; i < nd[u].size(); i++) {                int v = nd[u][i];                if(vis[v] == 1) continue;                vis[v] = 1;                if(c[v] == -1 || path(c[v])) {                        c[v] = u;                        return 1;                }        }        return 0;}int main(){        cin >> N >> M;        int a,b;        for(int i = 1; i <= M; i++) {                cin >> a >> b;                nd[a].push_back(b);        }        mem(c,-1);        for(int i = 0; i < N; i++) {                mem(vis,0);                path(i);        }        for(int i = 0; i < N; i++) {                if(c[i] == -1) {                        ans = 0;                        break;                }        }        if(ans) cout << "YES" << endl;        else cout << "NO" << endl;}
原创粉丝点击