HNU13377:Book Club(DFS)

Problem description

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 9
0 1
1 2
2 0
3 4
4 3
5 6
6 7
7 8
8 5
Sample Output
YES
Problem Source
HNU 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 rank1
const 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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 00:06:20

HNU13377:Book Club(DFS)的相关文章

HNU13377 Book Club(二分匹配)

题意:有n个人,m种需求,给出m行,每行a,b代表a想要的书在b那里,问能不能通过交换的方法来满足每个人的需求 思路:这题有好多做法..刚上来思路也有好多 想到了判环,但是如果两环相切这判断很罗嗦,干脆用二分匹配来的直接 就是n与n匹配,想清楚这点就很简单了,版子题了 就是人跟人的最大匹配数嘛.. /* *********************************************** Author :devil Created Time :2016/4/4 14:36:38 ***

8ujyhg

http://weibo.com/p/1001603792889590244409http://club.baby.sina.com.cn/viewthread.php?tid=4850958&pid=30365034&extra=page%3D2&frombbs=1http://club.baby.sina.com.cn/viewthread.php?tid=4850959&pid=30365035&extra=page%3D2&frombbs=1http

HDU-4924-Football Manager(DFS+DP)

Problem Description Football Manager is a series of football management simulation games developed by Sports Interactive and published by Sega. In this game, you will play a role of a football club manager and conduct your team to chase championship

uva 646 - The Gourmet Club(暴力)

题目链接:uva 646 - The Gourmet Club 题目大意:有16个人参加聚会,聚会一共5天,每天有4桌,每桌4个人,一起吃饭的4个人会互相认识.现在要安排座位使得16个任意两个人都互相认识.给出前三天的安排,求后两天的安排. 解题思路:任意两个人之间肯定只能同桌一次.所以根据这个条件,只要枚举出第4天的第1桌的情况,就可推导出所有的,或者是矛盾. 在Poj和Zoj上都过了,uva上过不了,求大神指教. #include <stdio.h> #include <string

解救小哈——DFS算法举例

一.问题引入 有一天,小哈一个人去玩迷宫.但是方向感不好的小哈很快就迷路了.小哼得知后便去解救无助的小哈.此时的小哼已经弄清楚了迷宫的地图,现在小哼要以最快的速度去解救小哈.那么,问题来了... 二.问题的分析 首先我们用一个二维数组来存储这个迷宫,刚开始的时候,小哼处于迷宫的入口处(1,1),小哈在(p,q).其实这道题的的本质就在于找从(1,1)到(p,q)的最短路径. 此时摆在小哼面前的路有两条,我们可以先让小哼往右边走,直到走不通的时候再回到这里,再去尝试另外一个方向. 在这里我们规定一

【BZOJ4942】[Noi2017]整数 线段树+DFS(卡过)

[BZOJ4942][Noi2017]整数 题目描述去uoj 题解:如果只有加法,那么直接暴力即可...(因为1的数量最多nlogn个) 先考虑加法,比较显然的做法就是将A二进制分解成log位,然后依次更新这log位,如果最高位依然有进位,那么找到最高位后面的第一个0,将中间的所有1变成0,那个0变成1.这个显然要用到线段树,但是复杂度是nlog2n的,肯定过不去. 于是我在考场上yy了一下,这log位是连续的,我们每次都要花费log的时间去修改一个岂不是很浪费?我们可以先在线段树上找到这段区间

uva1103(dfs)

UVA - 1103 还是没写好,,看的别人的 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <stack> 8 #include <cctype> 9 #include <str

poj 1088 滑雪 DP(dfs的记忆化搜索)

题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 求最大的下滑路径 分析:因为只能从高峰滑到低峰,无后效性,所以每个点都可以找到自己的最长下滑距离(只与自己高度有关).记忆每个点的最长下滑距离,当有另一个点的下滑路径遇到这个点的时候,直接加上这个点的最长下滑距离. dp递推式是,dp[x][y] = max(dp[x][y],dp[x+1][y]+

蓝桥杯 大臣的旅费_树的最长度_两次DFS

#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <functional> #include <vector> using namespace std; const int maxn = 1000000 + 10; const int INF = 10000000