HDU - 5971 Wrestling Match (搜索+染色)

探讨了在已知部分摔跤手身份(好手与坏手)的情况下,如何通过比赛记录判断所有摔跤手的身份归属,避免身份冲突并确保每个人都能被正确分类。

摘要生成于 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 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
**问题链接:**http://acm.hdu.edu.cn/showproblem.php?pid=5971
问题简述: 有n个摔跤手,m场比赛,以及知道有x个好手,y个坏手,判断能否将所有摔跤手分成好坏手(判断后有还没有判断出来的算是NO,有既是好手又是坏手的也算NO)
**问题分析:**先将已知的摔跤手染色,再用循环搜索染色。
代码及注释一下(感觉像是错的,不知道错在哪,却能AC,求指教)
AC通过的C++语言程序如下:

#include <iostream>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <climits>
#include <iomanip>
#include <queue>
#include<vector>
using namespace std;

const int N=10005;
int n,m,x,y;//人数/比赛场数/已知的好/坏人数
int p1[N],p2[N];//存储每一组比赛的人
int peo[N];//染色数组

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m>>x>>y)
    {
        if(n==0&&m==0&&x==0&&y==0) break;
        memset(peo,0,sizeof(peo));
        int flag=0;
        for(int i=1;i<=m;i++)
            cin>>p1[i]>>p2[i];//要输入比赛的人员
        while(x--)
        {
            int a;
            cin>>a;
            peo[a]=1;
        }
        while(y--)//先将已知好坏的人进行染色
        {
            int a;
            cin>>a;
            peo[a]=-1;
        }
        for(int i=1;i<=n;i++)//比赛判断
        {
            if(peo[p1[i]]==0&&peo[p2[i]]==0)
            {//如果没有染过色,将第一组染成1,第二组染成2
                peo[p1[i]]=1;
                peo[p2[i]]=-1;
            }
            else if((peo[p1[i]]==1&&peo[p2[i]]==1)||(peo[p1[i]]==-1&&peo[p2[i]]==-1))
            {//如果出现矛盾
                flag=1;
            }
            else if((peo[p1[i]]==1&&peo[p2[i]]==0)||(peo[p1[i]]==-1&&peo[p2[i]]==0))//一好或者一坏跟一个没有确定,染色
                peo[p2[i]]=-1*peo[p1[i]];
            else if((peo[p2[i]]==1&&peo[p1[i]]==0)||(peo[p2[i]]==-1&&peo[p1[i]]==0))
                peo[p1[i]]=-1*peo[p2[i]];
        }
        for(int i=1;i<=n;i++)//这里判断比赛完后还有没有没有分到好/坏组里的人
            if(peo[i]==0)
            {
                flag=1;
            }
        if(flag==1) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值