HDU Problem - 5971 Wrestling Match(染色)

题目链接

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

AC

  • 染色
    1. 首先将已经确定的选手染色
    2. 如果还有选手存在比赛,而且没有染色,那么他们肯定是成对存在,对这些选手染色(染成good和bad都可以)
    3. 最后判断,如果还存在没有染色的选手就不能组成合适的对阵
#include <iostream>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <algorithm>
#define ll long long
#define ull unsigned long long
#define N 1005
using namespace std;

int n, m;
vector<int> g[N];
int color[N];
// 染色判断 
bool dfs(int x, int y) {
    color[x] = y;
    for (int i = 0; i < g[x].size(); ++i) {

        if (color[g[x][i]] == (y ^ 1)) 
            continue;

        else if (color[g[x][i]] == y) 
            return false;


        else if (!dfs(g[x][i], y ^ 1)) 
            return false;

    }
    return true;
}

bool solve() {
    // 将已知颜色染色 
    for (int i = 1; i <= n; ++i) {
        if (color[i] == 1 && !dfs(i, 1))
            return false;
        else if (color[i] == 0 && !dfs(i , 0))
            return false;
    }
    // 如果还有没有没有染色,但是存在比赛的选手,将其染色 
    for (int i = 1; i <= n; ++i) {
        if (color[i] == 2 && !dfs(i, 1))
            return false;
    }
    // 如果还有不确定的选手 
    for (int i = 1; i <= n; ++i) {
        if (color[i] == -1)
            return false;
    }
    return true;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif  
    int x, y;
    while (scanf("%d%d%d%d", &n, &m, &x, &y) != EOF) {
        memset(color, -1, sizeof(color));
        // color -1 表示没有染色
        // color 1 表示选手为bad 
        // color 0 表示选手为good 
        // color 2 表示存在对决但是没有被染色 
        for (int i = 0; i < m; ++i) {
            int u, v;
            scanf("%d%d", &u, &v);
            color[u] = color[v] = 2; 
            g[u].push_back(v);
            g[v].push_back(u);
        }
        for (int i = 1; i <= x; ++i) {
            int tar;
            scanf("%d", &tar);
            color[tar] = 1;
        }
        for (int i = 1; i <= y; ++i) {
            int tar;
            scanf("%d", &tar);
            color[tar] = 0;
        }
        if (solve())    printf("YES\n");
        else    printf("NO\n");
        for (int i = 1; i <= n; ++i)
            g[i].clear();
    }

    return 0;
}


猜你喜欢

转载自blog.csdn.net/henuyh/article/details/82109870