染色判断二分图+补图

比赛的时候题意居然是反的,看了半天样例都看不懂 。。。。

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

  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\n",i,j);
                }
            }
        }
        memset(color,0,sizeof(color));
        bool flag=true;
while(true)
{
        if(flag==false) break;
        int pos=-1;
        for(int i=1;i<=n;i++)
        {
            if(Adj==-1) continue;
            else if(color!=0) continue;
            else
            {
                pos=i;
                break;
            }
        }
       // printf("pos:   %d\n",pos);
        if(pos==-1) break;
        queue<int> q;
        q.push(pos);color[pos]=1;
        while(!q.empty())
        {
            if(flag==false) break;
            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 )
 

HDOJ 4751 Divide Groups的更多相关文章

  1. HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)

    Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. hdu 4751 Divide Groups(dfs染色 或 2-sat)

    Problem Description   This year is the 60th anniversary of NJUST, and to make the celebration more c ...

  3. HDU 4751 Divide Groups 2013 ACM/ICPC Asia Regional Nanjing Online

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题目大意:判断一堆人能否分成两组,组内人都互相认识. 解题思路:如果两个人不是相互认识,该两人之 ...

  4. HDU 4751 Divide Groups

    题目链接 比赛时候,建图建错了.大体算法想到了,不过很多细节都没想好. #include <cstdio> #include <cstring> #include <cm ...

  5. hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)

    SDUST的训练赛 当时死磕这个水题3个小时,也无心去搞其他的 按照题意,转换成无向图,预处理去掉单向的边,然后判断剩下的图能否构成两个无向完全图(ps一个完全图也行或是一个完全图+一个孤点) 代码是 ...

  6. HDU 4751 Divide Groups (2-SAT)

    题意 给定一个有向图,问是否能够分成两个有向完全图. 思路 裸的2-sat--我们设一个完全图为0,另一个完全图为1,对于一个点对(u, v),如果u.v不是双向连通则它们两个不能在一组,即u和v至少 ...

  7. Divide Groups 二分图的判定

    Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. hdu4751 Divide Groups

    This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is goi ...

  9. HDU-4751 Divide Groups 染色问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题意:有n个人,每个人都认识一些人,要求把他们分成两个集合,使得两个集合中的人都相符两两认识. ...

随机推荐

  1. 企业开发中选择logback而不是log4j的理由

    不知道看到这篇文章的Java工程师有没有考虑过这个问题:为什么在企业开发中会选择logback来记录日志,而不是log4j呢? 如果你以前没有考虑过这个问题,那么现在如果让你考虑一下,你可能觉的会是因 ...

  2. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  3. iOS - 基础面试知识

    1.arc(automatic reference counting) OC对象被创建时引用计数从默认值0加1,当它被释放时候引用计数减1,引用计数减0时autorelease方法,销毁OC对象. 自 ...

  4. C#开发和调用Web Service

    http://blog.csdn.net/h0322/article/details/4776819 1.1.Web Service基本概念 Web Service也叫XML Web Service ...

  5. BigDecimal数据加法返回值接收

    1.相加 两个BigDecimal变量a,b. 如果想进行相加,即a加b的话,返回值需要使用a进行接收,如下: a = a.add(b); BigDecimal为不可变类, 所以执行运算的结果需要再返 ...

  6. python处理中文(待补充)

    字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(en ...

  7. Git创建ssh-key

    打开git bash界面,输入: ssh-keygen -t rsa -C "yourname@email.com" 一路回车,后续保持默认值即可. 把C:\users\yourn ...

  8. HBase filter shell操作

    创建表 create 'test1', 'lf', 'sf' lf: column family of LONG values (binary value) -- sf: column family ...

  9. Java反射机制<2>

    反射机制还可以调用类中的指定方法或指定属性,并且可以通过反射完成对数组的操作. 通过反射调用类中的方法 import java.lang.reflect.Method; //============= ...

  10. JavaScript排序算法——归并排序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...