HNU13377:Book Club(DFS)
来源:互联网 发布:减肥淘宝排行榜 编辑:程序博客网 时间:2024/06/01 22:35
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 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). 2<=N<=10 000
1<=M<=20 000 and M<=N^2-N.
Output
You should output YES if we can find a new book for every club member and NO if that is not possible.
Sample Input
9 90 11 22 03 44 35 66 77 88 5Sample Output
YESProblem SourceHNU Contest
题意:
有n个人,m种需求,给出m行,每行a,b代表a想要的书在b那里,问能不能通过交换的方法来满足每个人的需求
#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <algorithm>#include <climits>using namespace std;#define ls 2*i#define rs 2*i+1#define UP(i,x,y) for(i=x;i<=y;i++)#define DOWN(i,x,y) for(i=x;i>=y;i--)#define MEM(a,x) memset(a,x,sizeof(a))#define W(a) while(a)#define gcd(a,b) __gcd(a,b)#define LL long long#define N 20005#define INF 0x3f3f3f3f#define EXP 1e-8#define rank rank1const int mod = 1000000007;int n,m,vis[N],tem[N];vector<int> a[N];int dfs(int u){ for(int i=0; i<a[u].size(); i++) { int v = a[u][i]; if(!vis[v]) { vis[v]=1; if(tem[v]==-1||dfs(tem[v])) { tem[v] = u; return 1; } } } return 0;}int main(){ int i,j,k,x,y; while(~scanf("%d%d",&n,&m)) { for(i = 0; i<=n; i++) a[i].clear(); for(i = 0; i<m; i++) { scanf("%d%d",&x,&y); a[x].push_back(y); } MEM(tem,-1); for(i = 0; i<n; i++) { MEM(vis,0); dfs(i); } for(i = 0; i<n; i++) { if(tem[i]==-1) break; } if(i==n) printf("YES\n"); else printf("NO\n"); } return 0;}
- HNU13377:Book Club(DFS)
- hnuoj 13377 Book Club(匈牙利算法)
- swerc2014 Book Club(二分图匹配)
- Book Club 二分图最大匹配
- http://club.book.csdn.net/book/183400.html
- HNU 13377 Book Club (最大流 判环)
- Codeforces Gym 100783D Book Club(匈牙利算法)
- CF337D Book of Evil【dfs】
- Book of Evil 树双向DFS
- book
- book
- book
- book
- book
- Book
- book
- Book
- book
- JavaScript笔记
- NYOJ 168 房间安排 (贪心)
- Mac git指令无法执行相关问题
- 【笔试】17、猴子吃桃问题
- HTML DOM笔记
- HNU13377:Book Club(DFS)
- HDU1503:Advanced Fruits 【LCS】
- 微信开发时,接收微信回调时,如果是传递流,请不要用参数接收
- C语言-Loop
- POJ 2352 Stars
- 消息机制
- hdu 1829 A Bug's Life(带权并查集)
- jQuery笔记
- CSU 1328 近似回文词