http://acm.hdu.edu.cn/showproblem.php?pid=5971

Wrestling Match

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 25    Accepted Submission(s): 15

Problem Description
Nowadays, at least one wrestling match is held every year in our country. There are a lot of people in the game is "good player”, the rest is "bad player”. Now, Xiao Ming is referee of the wrestling match and he has a list of the matches in his hand. At the same time, he knows some people are good players,some are bad players. He believes that every game is a battle between the good and the bad player. Now he wants to know whether all the people can be divided into "good player" and "bad player".
 
Input
Input contains multiple sets of data.For each set of data,there are four numbers in the first line:N (1 ≤ N≤ 1000)、M(1 ≤M ≤ 10000)、X,Y(X+Y≤N ),in order to show the number of players(numbered 1toN ),the number of matches,the number of known "good players" and the number of known "bad players".In the next M lines,Each line has two numbersa, b(a≠b) ,said there is a game between a and b .The next line has X different numbers.Each number is known as a "good player" number.The last line contains Y different numbers.Each number represents a known "bad player" number.Data guarantees there will not be a player number is a good player and also a bad player.
 
Output
If all the people can be divided into "good players" and "bad players”, output "YES", otherwise output "NO".
 
Sample Input
5 4 0 0
1 3
1 4
3 5
4 5
5 4 1 0
1 3
1 4
3 5
4 5
2
 
Sample Output
NO
YES
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5981 5980 5979 5978 5977 
 

给定一个图,有可能是分散的图,其中有一些点是固定是颜色的,现在要求判断其能否成为二分图。

假如是分成了若干个联通快(块内的点个数 >= 2),对于每个联通快,如果有一些点是确定了的,那么就应该选那个点进行开始染色,途中如果遇到一些点已经确定颜色的了,但是和现在的想填的颜色不同,那么就应该输出NO,否则,进行染色即可。

对于点数为1的联通快,如果它没有被确定颜色的话,那么就直接输出NO了。

然后边数要开两倍,不然直接给wa,这里坑了我。一直做不出。

5 3 0 0
1 2
1 3
4 5
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
int n, m, x, y;
const int maxn = + ;
struct node {
int u, v, w;
int tonext;
} e[ * + ];
int first[maxn];
bool vis[maxn];
int black = ;
int white = ;
int arr[maxn];
int ca[maxn];
bool in[maxn];
bool flag;
int num;
void add(int u, int v, int w) {
++num;
e[num].u = u;
e[num].v = v;
e[num].w = w;
e[num].tonext = first[u];
first[u] = num;
}
void dfs(int cur, int col) {
for (int i = first[cur]; i && flag; i = e[i].tonext) {
int v = e[i].v;
if (vis[v]) {
if (arr[v] == col) {
flag = false;
return;
}
}
if (vis[v]) continue;
vis[v] = true;
if (arr[v] == -) {
arr[v] = !col;
dfs(v, !col);
} else {
if (arr[v] == col) {
flag = false;
return;
} else {
dfs(v, !col);
}
}
}
}
void work() {
num = ;
memset(arr, -, sizeof arr);
memset(ca, -, sizeof ca);
memset(in, , sizeof in);
memset(first, , sizeof first);
flag = true;
for (int i = ; i <= m; ++i) {
int u, v;
cin >> u >> v;
add(u, v, );
add(v, u, );
in[v] = in[u] = ;
}
for (int i = ; i <= x; ++i) {
int val;
cin >> val;
ca[val] = black;
arr[val] = black;
}
for (int i = ; i <= y; ++i) {
int val;
cin >> val;
ca[val] = white;
arr[val] = white;
}
for (int i = ; i <= n; ++i) {
if (in[i] == && ca[i] == -) {
cout << "NO" << endl;
return;
}
}
memset(vis, , sizeof vis);
for (int i = ; i <= n; ++i) {
if (vis[i]) continue;
if (ca[i] == -) continue;
vis[i] = ;
arr[i] = ca[i];
dfs(i, ca[i]);
}
for (int i = ; i <= n; ++i) {
if (vis[i]) continue;
arr[i] = black;
dfs(i, black);
}
if (flag == false) {
cout << "NO" << endl;
return;
}
cout << "YES" << endl;
}
int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
IOS;
while (cin >> n >> m >> x >> y) {
work();
}
return ;
}

hdu 5971 Wrestling Match 判断能否构成二分图的更多相关文章

  1. hdu 5971 Wrestling Match

    题目链接: hdu 5971 Wrestling Match 题意:N个选手,M场比赛,已知x个好人,y个坏人,问能否将选手划分成好人和坏人两个阵营,保证每场比赛必有一个好人和一个坏人参加. 题解:d ...

  2. HDU 5971 Wrestling Match (二分图)

    题意:给定n个人的两两比赛,每个人要么是good 要么是bad,现在问你能不能唯一确定并且是合理的. 析:其实就是一个二分图染色,如果产生矛盾了就是不能,否则就是可以的. 代码如下: #pragma ...

  3. hdu 5971 Wrestling Match 二分图染色

    题目链接 题意 \(n\)人进行\(m\)场比赛,给定\(m\)场比赛的双方编号:再给定已知的为\(good\ player\)的\(x\)个人的编号,已知的为\(bad\ player\)的\(y\ ...

  4. HDU 5971"Wrestling Match"(二分图染色)

    传送门 •题意 给出 n 个人,m 场比赛: 这 m 场比赛,每一场比赛中的对决的两人,一个属于 "good player" 另一个属于 "bad player" ...

  5. A - Wrestling Match HDU - 5971

    Nowadays, at least one wrestling match is held every year in our country. There are a lot of people ...

  6. Wrestling Match---hdu5971(2016CCPC大连 染色法判断是否是二分图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5971 题意:有n个人,编号为1-n, 已知X个人是good,Y个人是bad,m场比赛,每场比赛都有一个 ...

  7. hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  8. HDU 3081 Marriage Match II (二分图,并查集)

    HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...

  9. hdu 2444(染色法判断二分图+最大匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

随机推荐

  1. java基础汇总

    1.关于Http和Hibernatet里面Session的区别HttpSession      HttpSession:是一个抽象接口,J2EE的Web程序在运行的时候,会给每一个新的访问者建立一个H ...

  2. Android记录程序崩溃Log写入文件

    将导致程序崩溃的堆栈调用Log写入文件,便于收集bug.在调试安卓程序,由于某些原因调试时手机不能连接PC端,无法通过IDE查看程序崩溃的Log,希望log能够写入文件中,对于已经发布的App可以通过 ...

  3. LaTeX常用的符号

    推荐新手使用的网站:http://latex.codecogs.com/eqneditor/editor.php \(\sum _{d|n}{u(d)F(\frac{n}{d})}\) \sum _{ ...

  4. ubuntu中使用gensim+word2vec[备忘]

    python版本: 2.7.12 0. 安装python和pip 1. 用pip依次安装: numpy, cython,scipy,pattern,word2vec 五个工具包 2. 用pip安装ge ...

  5. 【C】貌似不友好的scanf()

    scanf语句执行过程: (1)逆序取参数的偏移地址并分别入栈. (2)根据控制字符串的格式说明符从缓冲区取数据给各变量赋值. ①若格式说明符是数值类数据:如果从缓冲区中拿出的第一个字符可以合法表示该 ...

  6. 让Spinner中的文字居中

    如果套用simple_spinner_item或是simple_spinner_dropdown_item,然后直接在Spinner中用 android:gravity="center&qu ...

  7. OC-内存管理的所有权链问题

    背景: 最近维护之前的项目,没有注意具体的对象之间的关系,导致了一个bug. 让我了解到对象的所有权链问题. 需要内存管理的知识: 众所周知,oc是使用引用计数来管理内存的(当一个对象被持有,他的re ...

  8. 前端调用后端接口返回200(成功状态码),后端有返回,但是控制台Network Response为空,没展示任何信息

    解决方法: 1.在js里面debugger,可以看到后台是否有返回数据. 2.直接console.log(),直接把返回值打印出来,查看返回的数据格式,方便前端进行数据的处理. PS:因为后端返回的数 ...

  9. StarUML中时序图

    StarUML中时序图 在看时序图的例子的时候,发现有些的时序图上有小人的图标,可是一些UML工具却没有找到小人的图标,这让我很闹心,一直没解决,今天终于将该问题给解决了.解决这个问题来自于网上的一个 ...

  10. Linux 静态库 & 动态库

    转自:http://blog.chinaunix.net/uid-26833883-id-3219335.html 一.什么是库   本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执 ...