Problem description |
Porto's book club was 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's owned by someone willing to t Rade with them. I have been to this book exchange before, and I definitely does not want to miss it's this year, but I feel that the trading s Hould be improved. In the past, pairs of all interested in each other's books would simply trade:imagine that person a brought a book th At person B liked and Vice-versa, then A and B would exchange their books. I then realized this many members were left and 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 is only likes member B's book, while B is 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 is stop at triplets? Cycles could be bigger and bigger! Could you help me find if it's possible for everyone to go off with a new book? Be careful, because members won't 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 then everyone receives a new book?
|
Input |
The first line has both integers:n, the number of people, and M, the total number of "declarations of interest". Each of the following M lines have both integers, a and B, indicating that member a likes the book that member B brought (0& Lt;=a,b < N). Numbers A and B would never is the same (a member never likes the book he brought). 2<=n<=10 000 1<=m<=20 and M<=n^2-n.
|
Output |
You should output YES if the we can find a new book for every club member and NO if that's not possible.
|
Sample Input |
9 90 11 22 03 44 35 66 77 88 5 |
Sample Output |
YES |
Problem Source |
Hnu Contest |
Test instructions
There are n people, m demand, given M-line, each line B represents a wanted book in the B. There, ask can be exchanged to meet the needs of everyone
#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 rank1cons t 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hnu13377:book Club (DFS)