zoukankan      html  css  js  c++  java
  • HDOJ 4751 Divide Groups

    染色判断二分图+补图

    比赛的时候题意居然是反的,看了半天样例都看不懂HDOJ 4751 Divide Groups - qhn999 - 码代码的猿猿 。。。。

    Divide Groups

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 456    Accepted Submission(s): 172


    Problem Description
    HDOJ 4751 Divide Groups - qhn999 - 码代码的猿猿

      This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is going to invite distinguished alumnus back to visit and take photos.
      After carefully planning, Tom200 announced his activity plan, one that contains two characters:
      1. Whether the effect of the event are good or bad has nothing to do with the number of people join in.
      2. The more people joining in one activity know each other, the more interesting the activity will be. Therefore, the best state is that, everyone knows each other.
      The event appeals to a great number of alumnus, and Tom200 finds that they may not know each other or may just unilaterally recognize others. To improve the activities effects, Tom200 has to divide all those who signed up into groups to take part in the activity at different time. As we know, one's energy is limited, and Tom200 can hold activity twice. Tom200 already knows the relationship of each two person, but he cannot divide them because the number is too large.
      Now Tom200 turns to you for help. Given the information, can you tell if it is possible to complete the dividing mission to make the two activity in best state.
     

    Input
      The input contains several test cases, terminated by EOF.
      Each case starts with a positive integer n (2<=n<=100), which means the number of people joining in the event.
      N lines follow. The i-th line contains some integers which are the id
    of students that the i-th student knows, terminated by 0. And the id starts from 1.
     

    Output
      If divided successfully, please output "YES" in a line, else output "NO".
     

    Sample Input
    3
    3 0
    1 0
    1 2 0
     

    Sample Output
    YES
     

    Source
     

    Recommend
    liuyiding
     



    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>

    using namespace std;

    int guanxi[111][111],color[111];
    typedef struct
    {
        int to,next;
    }Edge;

    Edge E[21000];
    int Adj[21000],Size=0;

    void Init()
    {
        Size=0;
        memset(Adj,-1,sizeof(Adj));
    }

    void Add_Edge(int u,int v)
    {
        E[Size].to=v;
        E[Size].next=Adj;
        Adj=Size++;
    }

    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            memset(guanxi,0,sizeof(guanxi));
            for(int i=1;i<=n;i++)
            {
                int x;
                while(scanf("%d",&x)&&x)
                {
                    guanxi[x]=1;
                }
            }
            Init();
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(i==j) continue;
                    if(!guanxi[j]||!guanxi[j])
                    {
                        Add_Edge(i,j);
             //           printf("%d---->%d ",i,j);
                    }
                }
            }
            memset(color,0,sizeof(color));
            bool flag=true;
    while(true)
    {
            if(flag==falsebreak;
            int pos=-1;
            for(int i=1;i<=n;i++)
            {
                if(Adj==-1continue;
                else if(color!=0continue;
                else
                {
                    pos=i;
                    break;
                }
            }
           // printf("pos:   %d ",pos);
            if(pos==-1break;
            queue<int> q;
            q.push(pos);color[pos]=1;
            while(!q.empty())
            {
                if(flag==falsebreak;
                int u=q.front(); q.pop();
                for(int i=Adj;~i;i=E.next)
                {
                    int v=E.to;
                   // printf("%d------>%d   %d   %d ",u,v,color,color[v]);
                    if(color[v]==0)
                    {
                        color[v]=-color;
                        q.push(v);
                    }
                    else if(color[v]==color)
                    {
                        flag=false;
                        break;
                    }
             //        printf("%d------>%d   %d   %d ",u,v,color,color[v]);
                }
            }
    }
            if(!flag)
                puts("NO");
            else
                puts("YES");
        }
        return 0;
    }
    * This source code was highlighted by YcdoiT. ( style: Codeblocks )
     
  • 相关阅读:
    个人作业week7——前端开发感想总结
    C#【结对编程作业】小学数学习题助手
    【个人作业3】必应词典案例分析
    【个人博客作业II】有关代码规范问题的讨论
    【个人博客作业II】代码复审结果
    【补充】第一次个人项目出现的bug
    《构建之法》阅读反馈
    【个人项目总结】C#四则运算表达式生成程序
    软件工程驻足篇章:第十七周和BugPhobia团队漫长的道别
    软件工程反思篇章:第七周和进阶团队项目感想反思
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350833.html
Copyright © 2011-2022 走看看