【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表示 ...
随机推荐
- Dojo操作dom元素的样式
1.使用dom-style的set方法,可以直接设置dom元素的样式属性,这和使用dom元素的style属性效果一样. 2.使用dom-class的replace方法可以替换某个dom元素的样式,ad ...
- c++ 创建路径方法
linux.unix平台 #include "stdio.h" #include "stdlib.h" #include <sys/types.h> ...
- Unity基础-发布
PC BuildSetting File->BuildSetting Switch Platform Development Build是在调试模式下使用版本 Script Debugging调 ...
- VUE2.0声明周期钩子:不同阶段不同钩子的开启
- NXP低功耗蓝牙集成芯片QN9080C的时钟配置
/*************************************************************************************************** ...
- lwqq
源码安装lwqq 加密保存密码 发送截图
- kettle-学习参考
一 关于Kettle Kettle是一款国外开源的ETL工具,纯java编写,数据抽取高效稳定的数据迁移工具.Kettle中有两种脚本文件,transformation和job,transf ...
- mac 安装composer的方法
打开命令后 cd /usr/local/bin 然后执行 curl -sS https://getcomposer.org/installer | php 接下来 sudo mv composer.p ...
- Hadoop4.2HDFS测试报告之一
1.1 测试方案 1.1.1 测试目标 为了验证本地文件系统和HDFS存储能力对比,将1G文件组存储进各个文件系统,记录存储任务消耗的时间. l 测试HDFS的高可用性和高稳定性 l 测试 ...
- HDU4010 Query on The Trees (LCT动态树)
Query on The Trees Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Othe ...