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 ...
随机推荐
- Codeforces Round #263 (Div. 2) A. Appleman and Easy Task【地图型搜索/判断一个点四周‘o’的个数的奇偶】
A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input sta ...
- sqlserver 一些基本的语法
1. STR()函数 这是将数字转换为字符串的快捷函数,这个函数有3个参数:数值,总长度,和小数位 如: select str(123.4,8,4) = 123.4000 如长度不够,则左 ...
- npm上面实用的第三方工具包
npm上面实用的第三方工具包 live-server 作用:为页面提供实时刷新重载的功能,并且能提供一个http服务器 官方地址:https://www.npmjs.com/package/live- ...
- 覆盖equals时请遵守通用约定
Object类中非final修饰的方法有equals().hashCode().toString().finalize().clone()1.equals()方法不需要被覆盖的情况:1)实例化的对象只 ...
- HTML小技巧:按钮中的文字换行 .
一般按钮的文字都是一行的.但是有的时候画面需要按钮中的文字换行. 刚开始有个开发人员说没法实现.\r\n 都用过了没有效果.其实google这个老师是非常强大的. 直接换行的方法:<input ...
- C++ 浮点数 为 0 的判断
- jquery 回车提交事件
$("body").keydown(function(){ if(event.keyCode == "13"){ //13是回车键的位置 } })
- python KMP算法介绍
- JavaScript--关于变量提升思考
下面例子仅仅是思考变量提升使用: 在实际开发中并不推荐使用相同名字的变量和函数! // 如果变量和函数同名的话,函数优先提升 console.log(a); function a() { consol ...
- 人生苦短,LET'S GO! GO语言目录
1.Golang开山篇,GO就是NB! 1-1.go开发工具安装 2.go-人生第一个go程序和基本语法 3.go-流程控制 4.go-函数 5.go-流程控制 6.go-复合类型 7.go-面向对象 ...