Wrestling Match
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1034 Accepted Submission(s): 391
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
NOYES题意: 给的数据是否能分成 good player 或者 bad player 两个队伍, 利用dfs染色来区分是否是二分图。
建图,首先把已知的 good or bad players 开始dfs,再对未知的进行dfs 染色 + 判断。 如果还有剩余没有搜索到的点则 false
#include <stdio.h> #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string.h> using namespace std; vector<int> map[1005]; int v; int color[1005]; bool dfs(int p, int c) { color[p] = c; for (int i = 0 ; i < map[p].size(); ++i) { if (color[map[p][i]] == c) { return false; } if (color[map[p][i]] == 0 && !dfs(map[p][i], -c)) //color == 0 判断 该点是否已被染色 已被染色就不dfs了 return false; } return true; } void solve() { for (int i = 1; i <= v; ++i) { if (color[i] == 1) { if (!dfs(i, 1)) { cout << "NO" << endl; return; } } } for (int i = 1; i <= v; ++i) { if (color[i] == -1) { if (!dfs(i, -1)) { cout << "NO" << endl; return; } } } for (int i = 1; i <= v; ++i) { if (color[i] == 0) { if (!dfs(i, 1)) { cout << "NO" << endl; return; } } } for (int i = 1; i <= v; ++i) { if (color[i] == -5) { cout << "NO" << endl; return; } } cout << "YES" << endl; } int main() { int n, a, b; int x, y; while (cin >> v >> n >> a >> b) { for (int i = 1; i <= v; ++i) { map[i].clear(); color[i] = -5; } for (int i = 0; i < n; ++i) { cin >> x >> y; color[x] = 0; color[y] = 0; map[x].push_back(y); map[y].push_back(x); } for (int i = 0; i < a; ++i) { cin >> x; color[x] = 1; } for (int i = 0; i < b; ++i) { cin >> x; color[x] = -1; } solve(); } }