【HDU】5971-Wrestling Match 二分染色

http://acm.hdu.edu.cn/showproblem.php?pid=5971

n个人m场比赛x个已确定的好人y个已确定的坏人。
每场比赛必有一好一坏。
是否能够将所有人划分成好人或者坏人。

dfs 染色。先从 已确定的人 开始 染色。出现矛盾则NO。
然后对于不确定的人,枚举染色。出现矛盾则NO。
若最后还存在不确定的,那么就是 NO
否则就是 YES

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int maxv=1005;

vector <int> G[maxv];
int V,E;
int x,y;
int color[maxv];

bool dfs(int u,int c){
    color[u]=c;
    for (int i=0;i<G[u].size();i++){
        if (color[G[u][i]]==c){
            return false;
        }
        if (color[G[u][i]]==0&&!dfs(G[u][i],-c)){
            return false;
        }
    }
    return true;
}

void Solve(){
    for (int i=1;i<=V;i++){
        if (color[i]==1){
            if (!dfs(i,1)){
                cout << "NO" << endl;
                return;
            }
        }
        else
        if (color[i]==-1){
            if (!dfs(i,-1)){
                cout << "NO" << endl;
                return;
            }
        }
    }

    for (int i=1;i<=V;i++){
        if (color[i]==0){
            if (!dfs(i,1)){
                cout << "NO" << endl;
                return;
            }
        }
    }
    for (int i=1;i<=V;i++){
        if (color[i]==-5){
            cout << "NO" << endl;
            return;
        }
    }

    cout << "YES" << endl;
}
int main(){
    while (cin >> V >> E >> x >> y){
        for (int i=1;i<=V;i++){
            G[i].clear();
            color[i]=-5;
        }
        int a,b;
        for (int i=0;i<E;i++){
            cin >> a >> b;
            color[a]=0;
            color[b]=0;
            G[a].push_back(b);
            G[b].push_back(a);
        }
        for (int i=0;i<x;i++){
            cin >> a;
            color[a]=1;
        }
        for (int i=0;i<y;i++){
            cin >> b;
            color[b]=-1;
        }
        Solve();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值