hdu 5971 Wrestling Match 二分图染色
题目链接
题意
\(n\)人进行\(m\)场比赛,给定\(m\)场比赛的双方编号;再给定已知的为\(good\ player\)的\(x\)个人的编号,已知的为\(bad\ player\)的\(y\)个人的编号。准则是每场比赛的两个选手必定一位来自\(good\ player\),另一位来自\(bad\ player\). 问是否可以据此将所有选手划分成两个阵营.
思路
// 题意和样例都很迷...
总的来说就是二分图染色,已经染好了若干个点,问能否顺利染成一个二分图。
但是不允许有孤立点。
具体操作上用\(dfs\)比较方便,因为图不连通,并查集不太好处理。
Code
#include <bits/stdc++.h>
#define maxn 2010
#define maxm 10010
bool flag;
int c[maxn], tot, ne[maxn], n, m, a, b, cnt;
struct Edge {
int to, ne;
Edge(int _to=0, int _ne=0): to(_to), ne(_ne) {}
}edge[maxm<<1];
void add(int u, int v) {
edge[tot] = Edge(v, ne[u]);
ne[u] = tot++;
}
using namespace std;
typedef long long LL;
void dfs(int u, int col) {
++cnt;
if (flag) return;
c[u] = col;
for (int i = ne[u]; ~i; i = edge[i].ne) {
int v = edge[i].to;
if (c[v]==-1) dfs(v, !col);
else if (c[v] == col) { flag = true; return; }
}
}
void work() {
int x, y;
tot = 0; memset(ne, -1, sizeof(ne)); memset(c, -1, sizeof c);
for (int i = 0; i < m; ++i) {
scanf("%d%d", &x, &y);
add(x, y), add(y, x);
}
for (int i = 0; i < a; ++i) scanf("%d", &x), c[x] = 0;
for (int i = 0; i < b; ++i) scanf("%d", &x), c[x] = 1;
flag = false;
for (int i = 1; i <= n; ++i) {
if (c[i] != -1) dfs(i, c[i]);
if (flag) { printf("NO\n"); return; }
}
for (int i = 1; i <= n; ++i) {
if (c[i] == -1) cnt = 0, dfs(i, 0);
if (flag || cnt == 1) { printf("NO\n"); return; }
}
printf("YES\n");
}
int main() {
while (scanf("%d%d%d%d", &n, &m, &a, &b) != EOF) work();
return 0;
}
hdu 5971 Wrestling Match 二分图染色的更多相关文章
- 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 ...
- HDU 5971"Wrestling Match"(二分图染色)
传送门 •题意 给出 n 个人,m 场比赛: 这 m 场比赛,每一场比赛中的对决的两人,一个属于 "good player" 另一个属于 "bad player" ...
- 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 ...
随机推荐
- 201621123080 《Java程序设计》第10周学习总结
201621123080 <Java程序设计>第10周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 ...
- pycharm安装 suds模块报错:AttributeError: module 'pip' has no attribute 'main'
需求:安装suds模块 遇到的问题: 一.报错信息:[file][Default Settint]---Project Interpreter 点击 搜索suds安装模块报错 解决:依据上图提示找到C ...
- 10GNU C语言函数调用
6. C 函数调用机制概述 在 Linux 内核程序 boot/head.s 执行完基本初始化操作之后,就会跳转区执行 init/main.c 程序.那么 head.s 程序时如何把执行控制转交给 ...
- linux文件属性软硬链接知识
链接的概念 在linux系统中,链接可分为两种:一种为硬链接,另一种为软链接或符号链接.在默认不带参数的情况下,执行ln命令创建的链接是硬链接. 如果使用ln -s创建链接则为软链接,前面文件类型为 ...
- 通过源码编译安装VIM
开发中使用的是Ubuntu 12.04 LTS,通过sudo apt-get install vim安装的版本较低,不支持YCM,所以,用源码编译并安装最新的Vim. 卸载旧版本的Vim: sudo ...
- python 类的封装/property类型/和对象的绑定与非绑定方法
目录 类的封装 类的property特性 类与对象的绑定方法与非绑定方法 类的封装 封装: 就是打包,封起来,装起来,把你丢进袋子里,然后用绳子把袋子绑紧,你还能拿到袋子里的那个人吗? 1.隐藏属性和 ...
- matplotlib学习记录 二
# 绘制10点到12点的每一分钟气温变化折线图 import random from matplotlib import pyplot as plt # 让matplotlib能够显示中文 plt.r ...
- Down the Pyramid
Do you like number pyramids? Given a number sequence that represents the base, you are usually suppo ...
- JAVA里的别名机制
别名现象主要出现在赋值的问题上: 对基本数据类型的赋值是很简单的.基本数据类型存储了实际的数值,而并非指向一个对象的引用,所以在为其赋值的时候,是直接将一个地方的内容复制到了另一个地方.例如,对基本数 ...
- luogu1972 [SDOI2009]HH的项链
莫队裸题还不带修改 #include <algorithm> #include <iostream> #include <cstdio> #include < ...