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. Be careful, because members will not give their book without receiving one they like in return. |
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 |
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 |
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;
}
HNU13377:Book Club(DFS)的更多相关文章
- HNU13377:Book Club 二分图
题意:有n个人,m种需求,给出m行,每行a,b代表a想要的书在b那里,问能不能通过交换的方法来满足每个人的需求 思路:要符合题意的话一定是二分图.网上还一种dfs #include<cstdio ...
- POJ 2250 (LCS,经典输出LCS序列 dfs)
题目链接: http://poj.org/problem?id=2250 Compromise Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)A模拟 B三分 C dfs D map
A. Andryusha and Socks time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
随机推荐
- Linux学习总结(17)——Linux新手必须学会的12个命令
今天的用户可以根据自己的意愿选择是否使用作为Linux象征的命令行,确切的证明了Linux已经有了很大的发展.现在很多Linux发行版的图形用户界面已经非常强大,不再需要命令行. 但是命令行在Linu ...
- poj2528 Mayor's posters(线段树,离散化)
离散化的思想: 对于这样的数据 (3,10000). (9,1000000). (5.100000), (1,1000). (7,1000000) 我们能够将其处理为 (2,7). (5,9). (3 ...
- 深搜解Riding the Fences
Riding the Fences Farmer John owns a large number of fences that must be repairedannually. He traver ...
- bzoj2190: [SDOI2008]仪仗队(欧拉)
2190: [SDOI2008]仪仗队 题目:传送门 题解: 跟着企鹅大佬做题! 自己瞎搞搞就OK,不难发现,如果以C作为原点建立平面直角坐标系,那么在这个坐标系中,坐标为(x,y)且GCD(x,y) ...
- [JZOJ 5893] [NOIP2018模拟10.4] 括号序列 解题报告 (Hash+栈+map)
题目链接: https://jzoj.net/senior/#main/show/5893 题目: 题解: 考虑暴力怎么做,我们枚举左端点,维护一个栈,依次加入元素,与栈顶元素和栈内第二个元素相同时弹 ...
- springboot shiro 多realm配置认证、授权
shiro进行登录认证和权限管理的实现.其中需求涉及使用两个角色分别是:门店,公司.现在要两者实现分开登录.即需要两个Realm——MyShiroRealmSHOP和MyShiroRealmCOMPA ...
- C语言学习小记
2013年1月31日 今天试着编程为报文去头去尾. #include #include #define MAX_LENTH 1024 int main() { char *path = &quo ...
- javascript常用代码(不完整版)
求大神指点 Javascript嵌入式 <script typt:javascript>代码</script> 注释 //或者/*内容*/ 变量名赋值 Var 变量名 = 值 ...
- 并发设计模式之Guarded Suspension模式
- 原文链接: http://www.joyhwong.com/2016/11/19/并发设计模式之guarded-suspension模式/ Guarded Suspension意为保护暂停,其核心 ...
- 线程与cpu
A thread is a basic unit of CPU utilization, consisting of a program counter, a stack, and a set of ...