zoukankan      html  css  js  c++  java
  • 「ACM Dalian Onsite 2016」A.Wrestling Match(判断二分图)

    描述

    传送门:我是传送门

    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 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.

    输出

    If all the people can be divided into “good players” and “bad players”, output “YES”, otherwise output “NO”.

    样例

    输入

    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

    输出

    NO
    YES

    思路

    可以抽象为两个对立的阵营(good and bed)

    抽象为二分图,用染色法判断,如果不是二分图则一定有矛盾的地方,输出‘NO’

    再判断一下已确定的X与Y个队伍

    具体见代码

    代码

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int mod = 1e9+7;
    const int N = 1e3+100;
    int n,m,x,y,u,v;
    int color[N];
    int e[N][N];
    int a[N],b[N];
    bool vis[N];
    map<int,int> m1,m2;
    bool check(int s,int n)
    {
        queue<int> q;
        q.push(s);
        color[s] = 1;
        while(!q.empty())
        {
            int from = q.front();
            q.pop();
            for(int i = 1;i <= n;i++)
            {
                if(e[from][i] && color[i] == -1)
                {
                    q.push(i);
                    color[i] = !color[from];
                }
                if(e[from][i] && color[from] == color[i])
                    return false;
            }
        }
        return true;
    }
    
    int main()
    {
        while(scanf("%d %d %d %d",&n,&m,&x,&y) != EOF)
        {
            bool bo = 1;
            memset(e,0,sizeof e);
            memset(color,-1,sizeof color);
            memset(vis,0,sizeof vis);
            m1.clear();
            m2.clear();
            int st = 0x3f3f3f3f;
            for(int i = 1;i <= m;i++)
            {
                scanf("%d %d",&u,&v);
                vis[u] = 1; vis[v] = 1;
                if(u > v)   swap(u,v);
                if(u < st)  st = u;
                e[u][v] = 1;
            }
            for(int i = 1;i <= x;i++){   scanf("%d",&a[i]); m1[a[i]] = 1;}
            for(int i = 1;i <= y;i++)
            {
                scanf("%d",&b[i]);
                if(m1[b[i]])
                {
                    printf("NO
    ");
                    bo = 0;
                }
                else
                    m2[b[i]] = 1;
            }
            for(int i = 1;bo && i <= n;i++)
            {
                if(!vis[i])
                {
                    if(m1[i] == 0 && m2[i] == 0)
                    {
                        printf("NO
    ");
                        bo = 0;
                        // return 0;
                    }
                }
            }
            if(bo && check(st,n))
            {
                if(x)
                {
                    int flag = color[a[1]];
                    for(int i = 2;bo && i <= x;i++)
                    {
                        if(color[a[i]] != flag)
                        {
                            printf("NO
    ");
                            bo = 0;
                            // return 0;
                        }
                    }
                    for(int i = 1;bo && i <= y;i++)
                    {
                        if(color[b[i]] == flag)
                        {
                            printf("NO
    ");
                            bo = 0;
                            // return 0;
                        }
                    }
                }
                else
                {
                    if(y)
                    {
                        int flag = color[b[1]];
                        for(int i = 2;bo && i <= y;i++)
                        {
                            if(color[b[i]] != flag)
                            {
                                printf("NO
    ");
                                bo = 0;
                                // return 0;
                            }
                        }
                    }
                }
                if(bo)
                    printf("YES
    ");
            }
            else
            {
                if(bo)
                    printf("NO
    ");
            }
        }
        return 0;
    }
    
  • 相关阅读:
    驱动-问题解决
    Broadcast-广播的接收
    menu-代码添加以及add方法参数意义
    ubuntu-12.04工作区内容变换所属工作区
    $.ajax(),传参要用data
    阻止浏览器默认的事件冒泡行为
    form提交跳转问题
    根据已知值,选中selec中的选项
    option click事件
    按回车键确定
  • 原文地址:https://www.cnblogs.com/duny31030/p/14305182.html
Copyright © 2011-2022 走看看