Wrestling Match (dfs乱搞染色)

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 1 to N), 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 numbers a, 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这两个阵营的人的编号。每个人必须属于一个阵营。问是否能分成这两个阵营。输出YES或NO。

思路:

直接利用dfs顺着m组关系,染色,有冲突则直接输出NO。

然后利用X,Y两个阵营的人员名单染色,分两种情况染色,X染0,Y染1,或者X染1,Y染0.

如果有冲突,则有冲突那种情况返回NO。

如果这两种情况有一种是YES的,就输出YES。否则NO。

代码:

#include
using namespace std;
#define ll long long
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const int maxn=1100;
const int maxm=110000;
const double pi=acos(-1);
int head[maxm];
int edgenum=0;
int n,m,X,Y;
struct node
{
    int to,next;
} edge[maxm];
void addedge(int a,int b)
{
    edge[edgenum].to=b;
    edge[edgenum].next=head[a];
    head[a]=edgenum++;
}
int work(int w,int a[],int b[],int vis[])
{
    //0
    for(int i=1;i<=X;i++)
    {
        int u=a[i];
        if(vis[u]==-1)
        {
            vis[u]=w;
        }
        else
        {
            if(vis[u]!=w)
            {
                return 0;
            }
        }
    }
    //1
    for(int i=1;i<=Y;i++)
    {
        int u=b[i];
        if(vis[u]==-1)
        {
            vis[u]=w^1;
        }
        else
        {
            if(vis[u]!=(w^1))
            {
                return 0;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==-1)return 0;
    }
    return 1;
}
int a[maxn],b[maxn];
int vis[maxn];
void debug()
{
    for(int i=1; i<=n; i++)
    {
        cout<

 

你可能感兴趣的:(搜索,二分图)