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,这里坑了我。一直做不出。

  1. 5 3 0 0
  2. 1 2
  3. 1 3
  4. 4 5
  5.  
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #define IOS ios::sync_with_stdio(false)
  7. using namespace std;
  8. #define inf (0x3f3f3f3f)
  9. typedef long long int LL;
  10.  
  11. #include <iostream>
  12. #include <sstream>
  13. #include <vector>
  14. #include <set>
  15. #include <map>
  16. #include <queue>
  17. #include <string>
  18. int n, m, x, y;
  19. const int maxn = + ;
  20. struct node {
  21. int u, v, w;
  22. int tonext;
  23. } e[ * + ];
  24. int first[maxn];
  25. bool vis[maxn];
  26. int black = ;
  27. int white = ;
  28. int arr[maxn];
  29. int ca[maxn];
  30. bool in[maxn];
  31. bool flag;
  32. int num;
  33. void add(int u, int v, int w) {
  34. ++num;
  35. e[num].u = u;
  36. e[num].v = v;
  37. e[num].w = w;
  38. e[num].tonext = first[u];
  39. first[u] = num;
  40. }
  41. void dfs(int cur, int col) {
  42. for (int i = first[cur]; i && flag; i = e[i].tonext) {
  43. int v = e[i].v;
  44. if (vis[v]) {
  45. if (arr[v] == col) {
  46. flag = false;
  47. return;
  48. }
  49. }
  50. if (vis[v]) continue;
  51. vis[v] = true;
  52. if (arr[v] == -) {
  53. arr[v] = !col;
  54. dfs(v, !col);
  55. } else {
  56. if (arr[v] == col) {
  57. flag = false;
  58. return;
  59. } else {
  60. dfs(v, !col);
  61. }
  62. }
  63. }
  64. }
  65. void work() {
  66. num = ;
  67. memset(arr, -, sizeof arr);
  68. memset(ca, -, sizeof ca);
  69. memset(in, , sizeof in);
  70. memset(first, , sizeof first);
  71. flag = true;
  72. for (int i = ; i <= m; ++i) {
  73. int u, v;
  74. cin >> u >> v;
  75. add(u, v, );
  76. add(v, u, );
  77. in[v] = in[u] = ;
  78. }
  79. for (int i = ; i <= x; ++i) {
  80. int val;
  81. cin >> val;
  82. ca[val] = black;
  83. arr[val] = black;
  84. }
  85. for (int i = ; i <= y; ++i) {
  86. int val;
  87. cin >> val;
  88. ca[val] = white;
  89. arr[val] = white;
  90. }
  91. for (int i = ; i <= n; ++i) {
  92. if (in[i] == && ca[i] == -) {
  93. cout << "NO" << endl;
  94. return;
  95. }
  96. }
  97. memset(vis, , sizeof vis);
  98. for (int i = ; i <= n; ++i) {
  99. if (vis[i]) continue;
  100. if (ca[i] == -) continue;
  101. vis[i] = ;
  102. arr[i] = ca[i];
  103. dfs(i, ca[i]);
  104. }
  105. for (int i = ; i <= n; ++i) {
  106. if (vis[i]) continue;
  107. arr[i] = black;
  108. dfs(i, black);
  109. }
  110. if (flag == false) {
  111. cout << "NO" << endl;
  112. return;
  113. }
  114. cout << "YES" << endl;
  115. }
  116. int main() {
  117. #ifdef local
  118. freopen("data.txt","r",stdin);
  119. #endif
  120. IOS;
  121. while (cin >> n >> m >> x >> y) {
  122. work();
  123. }
  124. return ;
  125. }

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. PYTHON 爬虫笔记二:Urllib库基本使用

    知识点一:urllib的详解及基本使用方法 一.基本介绍 urllib是python的一个获取url(Uniform Resource Locators,统一资源定址器)了,我们可以利用它来抓取远程的 ...

  2. c/c++通用内存泄漏检测框架GMFD(General Memory Fault Detection Framework)

    http://qa.baidu.com/blog/?p=171 1 背景: x86平台有完善的用户态检测内存工具比如valgrind等,可以监控程序运行中详细的内存信息,从而精确定位内存问题.然而随着 ...

  3. codeforces B. Eugeny and Play List 解题报告

    题目链接:http://codeforces.com/problemset/problem/302/B 题目意思:给出两个整数n和m,接下来n行给出n首歌分别的奏唱时间和听的次数,紧跟着给出m个时刻, ...

  4. 使用Dubbo实现RPC调用

    启动Dubbo服务有2个方式,1是通过xml配置,2是通过注解来实现,这点和Spring相似. 采用XML配置如下: <?xml version="1.0" encoding ...

  5. 数学题--On Sum of Fractions

    题目链接 题目意思: 定义v(n)是不超过n的最大素数, u(n)是大于n的最小素数. 以分数形式"p/q"输出 sigma(i = 2 to n) (1 / (v(i)*u(i) ...

  6. Material Design 之 定义状态栏(Status Bar)的颜色

    Hey,好久不见.今天遇到一个问题,想要把Status Bar 和 Tool Bar的颜色弄成一样的,或者是类似的,例如Material Design: 图中Status Bar颜色比Tool Bar ...

  7. vuex 命名空间

    默认情况下,模块内部的action mutation getter是注册在全局命名空间的,如果希望你的模块具有更高的封装度和复用性,你可以通过添加namespaced:true的方式使其成为带命名空间 ...

  8. NHibernate错误:Could not compile the mapping document的解决

    用动软代码生成器的NHibernate生成模板,生成一个“XML映射模板”,老是提示Could not compile the mapping document的问题. 各种的找,就是没找到问题. 后 ...

  9. bzoj4521

    数位dp 复习数位dp 数位dp一般用记忆化搜索来解决 观察需要满足的条件,然后计入状态 状态还要记录是否达到上线,以及前导零 比如说这道题 dfs(bit,a4,a8,cnt,last,limit) ...

  10. 使用json-lib的JSONObject.toBean( )时碰到的日期属性转换的问题

    今天碰到这样一个问题:当前台以JSON格式向后台传递数据的时候,对于数据中的日期属性,无法正常转换为相应的Date属性.JSON数据是这样的:{"birthday":"1 ...