HDU 5971"Wrestling Match"(二分图染色)
•题意
给出 n 个人,m 场比赛;
这 m 场比赛,每一场比赛中的对决的两人,一个属于 "good player" 另一个属于 "bad player";
给出你 x 个已经确定的"good player" 和 y 个已经确定的 "bad player"。
问是否可以将这 n 个人划分成两类,其中一类属于 "good player",另一类属于 "bad player";
即不存在某人即属于 "good player" 又属于 "bad player";
如果能,输出 "YES",反之,输出 "NO";
•题解
对于每一场比赛的两人 $u,v$,连一条双向边 $u\rightarrow v\ ,\ v\rightarrow u$;
然后 DFS 染色。
先从已经确定的 $x+y$ 个人开始,染色与其相关的人,矛盾就输出 "NO";
然后对于不确定的人,枚举染色, 矛盾就输出 "NO";
如果不存在矛盾,输出 "YES";
•Code
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+;
const int M=1e4+; int n,m,x,y;
int num;
int head[N];
struct Edge
{
int to;
int next;
}G[M<<];
void addEdge(int u,int v)
{
G[num]={v,head[u]};
head[u]=num++;
}
int col[N];///0:Good , 1:bad
int g[N];
int b[N];
bool ok; void DFS(int u,int flag)
{
col[u]=flag;
for(int i=head[u];~i && !ok;i=G[i].next)
{
int v=G[i].to; if(col[v] == -)
DFS(v,flag^); if(col[v] == col[u])///u,v对立,如果出现col[u]=col[v],矛盾
ok=true;
}
}
char *Solve()
{
for(int i=;i <= x;++i)
{
int u=g[i];
ok=false;
if(col[u] == -)
DFS(u,);
if(ok || col[u] == )
return "NO";
}
for(int i=;i <= y;++i)
{
int u=b[i];
if(col[u] == -)
DFS(u,); if(ok || col[u] == )
return "NO";
}
for(int i=;i <= n;++i)
{
ok=false;
if(col[i] == - && head[i] != -)
DFS(i,); if(ok)
return "NO";
}
return "YES";
}
void Init()
{
num=;
for(int i=;i <= n;++i)
{
col[i]=-;
head[i]=-;
}
}
int main()
{
// freopen("C:\\Users\\hyacinthLJP\\Desktop\\C++WorkSpace\\in&&out\\contest","r",stdin);
while(~scanf("%d%d%d%d",&n,&m,&x,&y))
{
Init();
for(int i=;i <= m;++i)
{
int u,v;
scanf("%d%d",&u,&v);
addEdge(u,v);
addEdge(v,u);
}
for(int i=;i <= x;++i)
scanf("%d",g+i);
for(int i=;i <= y;++i)
scanf("%d",b+i);
puts(Solve());
}
return ;
}
HDU 5971"Wrestling Match"(二分图染色)的更多相关文章
- hdu 5971 Wrestling Match 二分图染色
题目链接 题意 \(n\)人进行\(m\)场比赛,给定\(m\)场比赛的双方编号:再给定已知的为\(good\ player\)的\(x\)个人的编号,已知的为\(bad\ player\)的\(y\ ...
- HDU 5971 Wrestling Match (二分图)
题意:给定n个人的两两比赛,每个人要么是good 要么是bad,现在问你能不能唯一确定并且是合理的. 析:其实就是一个二分图染色,如果产生矛盾了就是不能,否则就是可以的. 代码如下: #pragma ...
- hdu 5971 Wrestling Match
题目链接: hdu 5971 Wrestling Match 题意:N个选手,M场比赛,已知x个好人,y个坏人,问能否将选手划分成好人和坏人两个阵营,保证每场比赛必有一个好人和一个坏人参加. 题解:d ...
- hdu 5971 Wrestling Match 判断能否构成二分图
http://acm.hdu.edu.cn/showproblem.php?pid=5971 Wrestling Match Time Limit: 2000/1000 MS (Java/Others ...
- A - Wrestling Match HDU - 5971
Nowadays, at least one wrestling match is held every year in our country. There are a lot of people ...
- HDU 3081 Marriage Match II (二分图,并查集)
HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...
- HDU 5971 二分图判定
Wrestling Match Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU2444 :The Accomodation of Students(二分图染色+二分图匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
随机推荐
- php安装oci8和pdo_oci扩展实现连接oracle数据库
PHP一般跟MySQL数据库搭配使用,但最近遇到一个需求需要实现PHP连接Oracle,了解到PHP可以通过pdo_oci和oci8扩展来连接Oracle,这里将安装的过程记录下来. 安装环境:PHP ...
- 第二周<导学/分类>
分类学习 分类算法各有不同 knn naivebyes regression dnn sklearn.linear_modlel 线性函数 sklearn.preprocessing 非线性函数 分类 ...
- JSP Web第六章整理复习 JavaBean技术
P183 什么是JavaBean,JavaBean有哪些特点? javabean是一种特殊的java类 特点:属性private,方法public P184 JavaBean封装数据,例6-1,6-2 ...
- 深入浅出Cocoa之类与对象【转】
最近打算写一些ObjC中比较底层的东西,尤其是 runtime 相关的.苹果已经将 ObjC runtime 代码开源了,我们可以从:http://opensource.apple.com/sourc ...
- li设置多选和取消选择的样式、输入数据类型判断
li设置多选和取消选择的样式: $('li').click(function(){ if($(this).hasClass('active')) {$(this).removeClass('activ ...
- fedora 安装ftp
fedora默认不安装ftp服务(包括client程序/service程序),需要进行手动安装: yum install ftp(安装client) yum install vsftpd(安装serv ...
- python 模块的执行环境
- Chef 安装
http://www.tuicool.com/articles/RnAVn2 三个角色: chef server, chef workstation, chef nodes(chef clients) ...
- xib搭建scrollView无法滑动的问题
最近给xib中的scrollView添加contentView的时候,view的约束总是参照莫名其妙的东西,不是frameLayout就是safeArea 因为之前都是默认以superView为参照系 ...
- ubuntu 使用glfw.h 出现函数无法调用
最近在学习在Ubuntu下使用qt进行opengl开发,使用到了glfw这个库.我安装官网的编译和安装方法进行了配置安装,在usr/local/include的下产生了glfw.h文件. 于是我在我的 ...