Wrestling Match (dfs乱搞染色)

该博客探讨了一个摔跤比赛的阵营划分问题,给定人数、比赛关系、已知的好选手和坏选手数量,判断是否能将所有人分为好选手和坏选手两组。通过DFS深度优先搜索进行染色,根据染色过程中是否存在冲突来判断是否可以成功划分。如果两种可能的染色方案(好选手染0,坏选手染1或反之)中有一种可行,则输出'YES',否则输出'NO'。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值