Wrestling Match (dfs乱搞染色)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a1046765624/article/details/82932740

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<bits/stdc++.h>
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<<i<<" "<<vis[i]<<endl;
    }
}
int dfs(int u,int fa,int w)
{
    if(head[u]!=-1)
    {
        if(vis[u]==-1)
        {
            vis[u]=w;
        }
        else
        {
            if(vis[u]!=w)
            {
                return -1;
            }
            else return 1;
        }
    }
    int flag=1;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].to;
        int f1=dfs(v,u,w^1);
        if(f1==-1)flag=-1;
    }
    return flag;
}
void init()
{
    memset(head,-1,sizeof(head));
    memset(vis,-1,sizeof(vis));
    edgenum=0;
}
int main()
{
    while(~scanf("%d%d%d%d",&n,&m,&X,&Y))
    {
        int u,v;
        init();
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        for(int i=1; i<=X; i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1; i<=Y; i++)
        {
            scanf("%d",&b[i]);
        }
        int flag_1=0;
        for(int i=1; i<=n; i++)
        {
            int p=0;
            if(vis[i]==-1)p=dfs(i,-1,0);
            if(p==-1)
            {
                flag_1=1;
                break;
            }
        }
        //debug();
        if(flag_1)
        {
            printf("NO\n");
            continue;
        }
        int flag1=work(0,a,b,vis);
        int flag2=work(1,a,b,vis);
        if(flag1||flag2)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a1046765624/article/details/82932740