HDU 5971 Wrestling Match(染色+暴力)

题目分析

主要的策略就是如果有已经确定的点,那么很明显沿着这条边直接跑就可以了,判断其相连的所有点时good player 还是 bad player,如果所有已经明确了的所有已经明确了的点都没有相邻边,那么直接找一个没有被标记过的点跑一下就可以了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1005;

struct Edge{
    int to, next;
}e[maxn*20];

int head[maxn], color[maxn], tot;

void addedge(int from, int to){
    e[tot].to = to;
    e[tot].next = head[from];
    head[from] = tot++;
}

bool dfs(int u, int fa){
    for(int i = head[u]; i != -1; i = e[i].next){
        int v = e[i].to;
        if(v == fa) continue;
        if(color[v] == color[u]) return false;
        if(!color[v]){
            color[v] = 3 - color[u];
            if(!dfs(v, u)) return false;
        }
    }
    return true;
}

void init(){
    tot = 0;
    memset(head, -1, sizeof(head));
    memset(color, 0, sizeof(color));
}

int main(){
    int n, m, x, y;
    while(scanf("%d%d%d%d", &n, &m, &x, &y) != EOF){
        init();
        int from, to;
        for(int i = 0; i < m; i++){
            scanf("%d%d", &from, &to);
            addedge(from, to);
            addedge(to, from);
        }
        int xx;
        int flag = true, cnt = 1;
        for(int i = 0; i < x; i++){
            scanf("%d", &xx);
            if(!color[xx]){
                color[xx] = 1;
                if(head[xx] != -1) cnt = 0;
                flag = flag&&dfs(xx, -1);
            }
            else if(color[xx] == 2) flag = false;
        }

        for(int i = 0; i < y; i++){
            scanf("%d", &xx);
            if(!color[xx]){
                color[xx] = 2;
                if(head[xx] != -1) cnt = 0;
                flag = flag&&dfs(xx, -1);
            }
            else if(color[xx] == 1) flag = false;
        }
        if(!flag){
            printf("NO\n");
            continue;
        }
        else{
            if(cnt){
                for(int i = 1; i <= n; i++)
                    if(!color[i]){
                        color[i] = 1;
                        flag = flag&&dfs(i, -1);
                        break;
                    }

                for(int i = 1; i <= n; i++)
                    if(!color[i]){ flag = false; break; }
            }
            else{
                for(int i = 1; i <= n; i++)
                    if(!color[i]){ flag = false; break;}
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值