zoukankan      html  css  js  c++  java
  • HDU

    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个坏人,需要根据下面的m场比赛,确认是否可以将n个人划分为好人还是坏人。不一定要知道这个人到底是属于好人还是坏人,主要看能否划分为两个群体,就和二分图匹配一样。如果有一个人没参加比赛也不知道是不是好人,就是No,出现了矛盾的地方也是NO。
    思路:首先将已确定为好人还是坏人的人连接点找出,染上相反的颜色。然后其他联通块也遍历过去,不知道是谁就染1,如果出现两个连接点数字相同肯定是错的。

    代码:
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #pragma GCC optimize(2)
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<set>
    #include<cmath>
    #include<string>
    #include<map>
    #include<vector>
    #include<ctime>
    #include<stack>
    using namespace std;
    #define mm(a,b) memset(a,b,sizeof(a))
    typedef long long ll;
    const long long mod = 1e9+7;
    const int maxn = 1e4+500;;
    const int inf = 0x3f3f3f3f;
    const int MAX_V = 1005;
    vector<int> G[MAX_V];
    int V;
    int color[MAX_V];
     
    bool dfs(int v, int c)
    {
        color[v] = c;
        for (int i = 0; i < G[v].size(); ++i)
        {
            if (color[G[v][i]] == c)
            {
                return false;
            }
            if (color[G[v][i]] == 0 && !dfs(G[v][i], -c))
                return false;
        }
        return true;
    }
     
    void solve()
    {
        for (int i = 0; i < V; ++i)
        {
            if(color[i]==1)
            {
                if(!dfs(i,1))
                {
                    puts("NO");
                    return ;
                }
            }
            else if(color[i]==-1)
            {
                if(!dfs(i,-1))
                {
                    puts("NO");
                    return ;
                }
            }
        }
     
        for (int i = 0; i < V; ++i)
        {
            if(color[i]==0)
            {
                if(!dfs(i,1))
                {
                    puts("NO");
                    return ;
                }
            }
        }
        for(int i=0;i<V;i++)
        {
            if(color[i]==-5)
            {
     
                puts("NO");
                    return ;
            }
     
        }
        puts("YES");
    }
     
    int main()
    {
        int E;
        int x,y;
        while (~scanf("%d%d%d%d", &V, &E,&x,&y))
        {
            int a, b;
            for (int i = 0; i < V; ++i)
            {
                G[i].clear();
                color[i]=-5;
            }
            for (int i = 0; i < E; ++i)
            {
                scanf("%d%d", &a, &b);
                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++)
            {
                scanf("%d",&a);
                a--;
                color[a]=1;
            }
            for(int i=0;i<y;i++)
            {
                scanf("%d",&a);
                a--;
                color[a]=-1;
            }
            solve();
        }
        return 0;
    }
  • 相关阅读:
    Json schema前奏 关于JSON
    笔试题:能被1~10同时整除的最小整数是2520,问能被1~20同时整除的最小整数是多少?
    CentOS7 安装 Docker、最佳Docker学习文档
    2019年4399暑期实习算法题2,迷宫路径条数
    2019vivo秋招提前批笔试题第3题
    python内存机制与垃圾回收、调优手段
    N皇后问题的python实现
    一行代码判断一个数是否是2的整数次方
    在O(1)的时间内删除链表节点
    打印从1到n位数的最大值
  • 原文地址:https://www.cnblogs.com/Tangent-1231/p/12416393.html
Copyright © 2011-2022 走看看