HDOJ 4751 Divide Groups

来源:互联网 发布:js 给隐藏的input赋值 编辑:程序博客网 时间:2024/05/14 16:23
染色判断二分图+补图

比赛的时候题意居然是反的,看了半天样例都看不懂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
2013 ACM/ICPC Asia Regional Nanjing Online
 

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\n",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\n",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\n  %d   %d\n",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\n  %d   %d\n",u,v,color,color[v]);
            }
        }
}
        if(!flag)
            puts("NO");
        else
            puts("YES");
    }
    return 0;
}
* This source code was highlighted by YcdoiT. ( style: Codeblocks )
 
0 0