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 ...
随机推荐
- C++模板中的静态
#include <iostream> #include <stdlib.h> using namespace std; template<class T> cla ...
- 【MongoDB】深入了解MongoDB不可不知的十点
一.对象ID的生成 每一个mongoDB文档那个都要求有一个主键.它在每一个集合中对全部的文档必须是唯一的.主键存放在文档_id字段中.由12个字符组成: 4c291856 238d3b ...
- Oracle RAC --安装图解
规划:所用linux系统以虚拟化方式安装在esx上,配置有内网的yum源,各分配有16G存储,下面为简单拓扑图 一,下载软件1,地址:http://www.oracle.com/technology/ ...
- centos6.0 配置SVN
基本步骤: 1.安装必需的subversion 2.创建版本库 3.配置用户和权限 4.钩子和svn常用命令说明 一.安装subversion 在这里我们使用yum来安装subversion,使用以下 ...
- ORACLE里锁的几种模式
0:none 1:null 空 2:Row-S 行共享(RS):共享表锁 3:Row-X 行专用(RX):用于行的修改 4:Share 共享锁(S):阻止其他DML操作 5:S/Row-X ...
- CxImage内存方式转换图像
最近,处于项目需要,需要将Bmp转换为JPEG格式.以前做过,采用的是GDI+的方式,该方式有一个极大地缺陷为无法实现跨平台处理.闲话少说,进入正题. CxImage cxImageBmp(pRGBB ...
- Django shortcut functions
django.shortcuts package提供提供帮助类和函数可以更便捷的操作MVC中的每一部分,包含: render(request, template_name,[dictionary],[ ...
- (转载) Scrollview 嵌套 RecyclerView 及在Android 5.1版本滑动时 惯性消失问题
Scrollview 嵌套 RecyclerView 及在Android 5.1版本滑动时 惯性消失问题 标签: scrollviewandroid滑动嵌套 2015-07-16 17:24 1112 ...
- java 获取线程id
如何获取正在运行的线程的ID? 解决方法 下面的示例演示如何使用getThreadId() 方法一个正在运行线程的ID. public class Main extends Object implem ...
- 设置IE浏览器文档模式版本
浏览器版本问题是所有开发web的程序员共同的诟病,尤其是IE浏览器,笔者上次修改公司的一个项目中出现项目是在IE9没有出来之前,而现在修改时,则发现页面上出现的表格的错乱(因为表格是通过js动态生成的 ...