【HDOJ5971】Wrestling Match(二分图,并查集)
题意:有n个人,m场比赛,x个人为good player,y个人为bad player,
每场比赛两个人分分别为good和bad,问good和bad是否会冲突
1 ≤ N≤ 1000,1 ≤M ≤ 10000
思路:二分图
根据已有的点暴力遍历,判有没有冲突即可
并查集也能写,队友写的搜索
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
#include <limits.h>
#include <string>
#include <iostream>
#include <queue>
#include <math.h>
#include <map>
using namespace std;
typedef long long int lint; const int MAXN = 1e3 + ;
const int MAXM = 1e4 + ; vector<int> g[MAXN];
queue<int> a,b; int n,m,x,y;
int color[MAXN]; void init(){
memset(color,-,sizeof(color));
for(int i = ; i <= n; ++i){ g[i].clear();}
while(!a.empty()){ a.pop();}
while(!b.empty()){ b.pop();}
} bool solve(){
while(!a.empty() || !b.empty()){
while(!a.empty()){
int now = a.front(); a.pop();
for(int i = ; i < g[now].size(); ++i){
if(color[g[now][i]] == -){
color[g[now][i]] = ; b.push(g[now][i]);
}else if(color[g[now][i]] == ){
return false;
}
}
}
while(!b.empty()){
int now = b.front(); b.pop();
for(int i = ; i < g[now].size(); ++i){
if(color[g[now][i]] == -){
color[g[now][i]] = ; a.push(g[now][i]);
}else if(color[g[now][i]] == ){
return false;
}
}
}
}
return true;
} int main(){
while(scanf("%d%d%d%d",&n,&m,&x,&y)!=EOF){
init();
for(int i = ; i <= m; ++i){
int u,v; scanf("%d%d",&u,&v);
g[u].push_back(v); g[v].push_back(u);
}
for(int i = ; i <= x; ++i){
int num; scanf("%d",&num); a.push(num); color[num] = ;
}
for(int i = ; i <= y; ++i){
int num; scanf("%d",&num); b.push(num); color[num] = ;
}
if(!solve()){
printf("NO\n"); continue;
}
bool flag = true;
for(int i = ; i <= n; ++i){
if(color[i] == - && g[i].size() != ){
a.push(i);
if(!solve()){
flag = false; break;
}
}else if(color[i] == - && g[i].size() == ){
flag = false; break;
}
}
if(!flag){
printf("NO\n"); continue;
}else{
printf("YES\n"); continue;
}
}
return ;
}
【HDOJ5971】Wrestling Match(二分图,并查集)的更多相关文章
- hdu 5971 Wrestling Match 二分图染色
题目链接 题意 \(n\)人进行\(m\)场比赛,给定\(m\)场比赛的双方编号:再给定已知的为\(good\ player\)的\(x\)个人的编号,已知的为\(bad\ player\)的\(y\ ...
- bzoj1854 游戏题解(二分图/并查集)
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 5547 Solved: 2229[Submit][Status] ...
- NOIp 2010/Luogu P1525 关押罪犯 【二分图/并查集】 By cellur925
题目传送门 感想:相信自己的想法!继续挖掘! 读完题目后:看到的最大值最小?二分答案啊!再仔细一看:wi达到了1e9,二分可能费点劲.(其实真的是可以的)而且check函数貌似并没有什么行之有效的写法 ...
- HDU 3277 Marriage Match III(并查集+二分答案+最大流SAP)拆点,经典
Marriage Match III Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 5971 Wrestling Match (二分图)
题意:给定n个人的两两比赛,每个人要么是good 要么是bad,现在问你能不能唯一确定并且是合理的. 析:其实就是一个二分图染色,如果产生矛盾了就是不能,否则就是可以的. 代码如下: #pragma ...
- HDU 3081 Marriage Match II (二分图,并查集)
HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...
- HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 3081:Marriage Match II(二分图匹配+并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意:有n个男生n个女生,他们只有没有争吵或者女生a与男生A没有争吵,且女生b与女生a是朋友,因此女生b也 ...
- HDU-3081-Marriage Match II 二分图匹配+并查集 OR 二分+最大流
二分+最大流: 1 //题目大意:有编号为1~n的女生和1~n的男生配对 2 // 3 //首先输入m组,a,b表示编号为a的女生没有和编号为b的男生吵过架 4 // 5 //然后输入f组,c,d表示 ...
随机推荐
- Forbidden You don't have permission to access /phpStudyTest/application/index/controller/Index.php on this server.
发生情况:将thinkPHP从官网上下了 http://thinkphp.cn 然后安装了phpstudy和PHPstorm,并将thinkPHP解压到www路径下 在用PHPstorm打开 thi ...
- jq 下拉框
<div class="alls"> <div class="item"> <div class="all"& ...
- pandas学习series和dataframe基础
PANDAS 的使用 一.什么是pandas? 1.python Data Analysis Library 或pandas 是基于numpy的一种工具,该工具是为了解决数据分析人物而创建的. 2.p ...
- 【Python学习之七】递归——汉诺塔问题的算法理解
汉诺塔问题 汉诺塔的移动可以用递归函数非常简单地实现.请编写move(n, a, b, c)函数,它接收参数n,表示3个柱子A.B.C中第1个柱子A的盘子数量,然后打印出把所有盘子从A借助B移动到C的 ...
- 01将图片嵌入到Markdown文档中
将图片内嵌入Markdown文档中 将图片嵌入Markdown文档中一直是一个比较麻烦的事情.通常的做法是将图片存入本地某个路径或者网络存储空间,使用URL链接的形式插入图片: ![image][ur ...
- 【Python高级工程师之路】入门+进阶+实战+爬虫+数据分析整套教程
点击了解更多Python课程>>> 全网最新最全python高级工程师全套视频教程学完月薪平均2万 什么是Python? Python是一门面向对象的编程语言,它相对于其他语言,更加 ...
- CentOS 6 搭建SVN支持httpd和svnserve独立服务器两种模式 以及邮件配置
Linux下SVN服务器同时支持Apache的http和svnserve独立服务器两种模式且使用相同的访问权限账号 服务器操作系统:CentOS 6.x 1.在服务器上安装配置SVN服务: 2.配置S ...
- laravel连接数据库提示mysql_connect() :Connection refused...
在.env配置文件中填写了正确的数据库连接配置的情况下连接还是出错了,明显提示的不是密码错误,那就看看端口吧, DB_HOST=127.0.0.1 DB_PORT= DB_DATABASE=test ...
- python3 兔子繁殖问题
题目 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 代码: month = int(input("繁殖 ...
- 网络流之Dinic算法
初学网络流.存一下Dinic板子. 复杂度O(n^2*m) UVA - 1515 Pool construction 把每个草地与 S 相连,花费为dig,每个洞与 T 相连,花费为 然后对于每个两个 ...