HDU5971

题意:

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

​ 翻译成汉语,就是给出n个人,m场比赛,已知x个人为好的运动员,y个人为差的运动员,每场比赛两个人必有一个好一个差,问好的和差的是否会前后冲突?如果不冲突是否每个运动员都可以归类?

思路:

​ 听队友说正解是二分图染色,自己不会图论,就用BFS做了一手,只用62ms成功A掉

扫描二维码关注公众号,回复: 3663686 查看本文章

​ 初始化book数组为0,把x个好运动员的book[i]初始化为1,y个差运动员的book[i]初始化为-1,并把这x+y个运动员加入到队列中。对队列中的每个运动员进行检查,和该运动员有关的(也就是与他有比赛的)运动员book[i]为0,那么就设置成和他之相反并把有关的运动员加入到队列中。如果和他book[i]相同,那么代表前后矛盾,直接标记为失败并跳出循环。

​ 这是对与X+Y以及与之有关的运动员进行判定,如果没有失败的话,则对所有参与比赛的人进行检索,若存在参加比赛的人book[i]为0,这时候要注意,这个运动员和之前的运动员不存在任何关联!!!所以可以把他设置为book[i]为1并加入到优先队列,再次进行BFS进行检索查看是否有矛盾

​ 若上面两个步骤都没有矛盾,则从头遍历所有运动员,是否有未归类的运动员,若没有则输出YES,否则都是NO。

代码:

#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <algorithm>
#include <vector>
#include <queue>
#define d int32_t
#define ll int64_t
#define mem(a) memset(a, 0, sizeof(a))
#define For(i, star, endd) for(d i = star; i <= endd; i++)
using namespace std;

d book[1005];
vector<d>zcy[1005];
queue<d>line;
d n, m, x, y;
d a[10005], b[10005];

//初始化
void init() {
    mem(book);
    d p;
    For(i, 1, n) {
        zcy[i].clear();
    }
    while (!line.empty()) {
        line.pop();
    }
    For(i, 1, m) {
        scanf("%d%d", &a[i], &b[i]);
        zcy[a[i]].push_back(b[i]);
        zcy[b[i]].push_back(a[i]);
    }
    For(i, 1, x) {
        scanf("%d", &p);
        book[p] = 1;
        line.push(p);
    }
    For(i, 1, y) {
        scanf("%d", &p);
        book[p] = -1;
        line.push(p);
    }
}

//BFS检索与已经入队的运动员有关的运动员是否前后冲突
d work() {
    d flag = 0;
    while (!line.empty()) {
        d t = line.front();
        line.pop();
        d pan = book[t];
        for (d i = 0; i < zcy[t].size(); i++){
            if(book[zcy[t][i]] == 0) {
                book[zcy[t][i]] = -pan;
                line.push(zcy[t][i]);
            } else if(book[zcy[t][i]] == pan){
                flag = 1;
                break;
            }
        }
        if(flag) break;
    }
    return flag;
}

d main () {
    while (scanf("%d%d%d%d", &n, &m, &x, &y) == 4) {
        init();
        d flag = work();
        if(flag) {
            printf("NO\n");
            continue;
        }
        For(i, 1, m) {
            if(!book[a[i]]) {
                book[a[i]] = 1;
                line.push(a[i]);
                flag = work();
                if(flag) break;
            }
        }
        if(flag) {
            printf("NO\n");
            continue;
        }
        //最后检索是否存在没有归类的运动员
        For(i, 1, n) {
            if(!book[i]) {
                flag = 1;
                break;
            }
        }
        if(flag) {
            printf("NO\n");
            continue;
        }
        printf("YES\n");
    }
    return 0;
}

转载请注明出处!!!

如果有写的不对或者不全面的地方 可通过主页的联系方式进行指正,谢谢

猜你喜欢

转载自blog.csdn.net/Ivan_zcy/article/details/83215271