HNU13377:Book Club 二分图
题意:有n个人,m种需求,给出m行,每行a,b代表a想要的书在b那里,问能不能通过交换的方法来满足每个人的需求
思路:要符合题意的话一定是二分图。网上还一种dfs
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<memory.h>
#include<vector>
using namespace std;
const int maxn = ;
int uN,vN;
vector<int> g[maxn];
int linker[maxn];
int n;
bool used[maxn];
bool dfs(int u) {
for(int i=;i<g[u].size(); i++) {
int v=g[u][i];
if(!used[v]) {
used[v]=true;
if(linker[v]==-||dfs(linker[v])) {
linker[v]=u;
return true;
}
} }
return false;
} int main() {
int m,i;
int x,y;
while(~scanf("%d%d",&n,&m)) {
int ans=;
for(i=; i<=n; i++)
g[i].clear();
for(i=; i<m; i++) {
scanf("%d%d",&x,&y);
g[x].push_back(y);
}
memset(linker,-,sizeof(linker));
for(i=; i<n; i++) {
memset(used,,sizeof(used));
if(dfs(i))ans++;
}
// cout<<ans<<endl;
if(ans==n)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
// for(i=0; i<n; i++) {
// if(linker[i]==-1) {
// break;
// }
// }
// if(i==n) {
// printf("YES\n");
// } else
// printf("NO\n");
}
}
HNU13377:Book Club 二分图的更多相关文章
- HNU13377:Book Club(DFS)
Problem description Porto's book club is buzzing with excitement for the annual book exchange event! ...
- CF1139E Maximize Mex 题解【二分图】
我发现我有道叫[SCOI2010]连续攻击游戏的题白写了.. Description There are \(n\) students and \(m\) clubs in a college. Th ...
- UVALive - 6887 Book Club 有向环的路径覆盖
题目链接: http://acm.hust.edu.cn/vjudge/problem/129727 D - Book Club Time Limit: 5000MS 题意 给你一个无自环的有向图,问 ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
- bzoj4025 二分图
支持加边和删边的二分图判定,分治并查集水之(表示我的LCT还很不熟--仅仅停留在极其简单的模板水平). 由于是带权并查集,并且不能路径压缩,所以对权值(到父亲距离的奇偶性)的维护要注意一下. 有一个小 ...
- hdu 1281 二分图最大匹配
对N个可以放棋子的点(X1,Y1),(x2,Y2)......(Xn,Yn);我们把它竖着排看看~(当然X1可以对多个点~) X1 Y1 X2 Y2 X3 Y3 ..... Xn Yn ...
- POJ 2226二分图最大匹配
匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是二部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图 ...
- 二分图&网络流&最小割等问题的总结
二分图基础: 最大匹配:匈牙利算法 最小点覆盖=最大匹配 最小边覆盖=总节点数-最大匹配 最大独立集=点数-最大匹配 网络流: 技巧: 1.拆点为边,即一个点有限制,可将其转化为边 BZOJ1066, ...
随机推荐
- 【9】了解Bootstrap栅格系统基础案例(4)
这次我们来说下嵌套列: 为了使用内置的栅格系统将内容再次嵌套,可以通过添加一个新的 .row 元素和一系列 .col-sm-* 元素到已经存在的 .col-sm-* 元素内.被嵌套的行(row)所包含 ...
- python基础学习(二)--函数
return返回值: python函数都有返回值,函数体内无return,默认返回值None, 函数参数: 1.普通参数 严格按照顺序,将实际参数赋值给形式参数,一一对应. 例: def send(x ...
- 洛谷1890 gcd区间
题目描述 给定一行n个正整数a[1]..a[n].m次询问,每次询问给定一个区间[L,R],输出a[L]..a[R]的最大公因数. 输入输出格式 输入格式: 第一行两个整数n,m.第二行n个整数表示a ...
- C# xml2json
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- csuoj 1352: New Sorting Algorithm
因为每个元素都是移动到比它小1位的元素的后面: 这样的话以后的一定就可以把他们两个打包: 所以用这种方法最多扫一遍就可以了: 但是最小的那个数要不要移动呢? 如果最小的数后面的数都是升序的,那么一直扫 ...
- res\menu\main.xml:6: error: No resource identifier found for attribute 'showAsAction' in package 'com.xxx.xxxx'
res\menu\main.xml:6: error: No resource identifier found for attribute 'showAsAction' in package 'co ...
- 1101-Trees on the Level
描述 Trees are fundamental in many branches of computer science. Current state-of-the art parallel com ...
- http://blog.csdn.net/zhang_xinxiu/article/details/38655311
一.Activiti下载及简介 1.1.Activiti下载 官网下载地址:http://activiti.org/download.html Note:下载时不一定要使用最新版本的,最 ...
- Styles and Themens(5)样式文件Style.xml中各元素的含义
Style Resource See also Styles and Themes A style resource defines the format and look for a UI. A s ...
- .Net中C#的DllImport的用法
大家在实际工作学习C#的时候,可能会问:为什么我们要为一些已经存在的功能(比如 Windows中的一些功能,C++中已经编写好的一些方法)要重新编写代码,C#有没有方法可以直接都用这些原本已经存在的功 ...