染色判断二分图+补图

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

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. ObjC 利用反射和KVC实现嵌套对象序列化成JSON数据

    原理: 0.创建一个新的可变字典:NSMutableDictionary 1.采用class_copyPropertyList函数遍历对象的属性 2.property_getName获取属性名,val ...

  2. 手机卫士开发记录之handler错误

  3. log4j属性详解

    Log4j有三个主要的组件:Loggers(记录器),Appenders  (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使用这三个组件可以轻 ...

  4. 加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: Section 5 Window to a Wider World

    Stat2.3x Inference(统计推断)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

  5. 【转载】MFC键盘响应

    转载自:传送门 一:首先介绍键盘消息 系统消息: ALT,F1,--F24等,是由系统内部处理的,程序本身就存在,比如F1是帮助键. WM_SYSKEYDOWN WM_SYSKEYUP WM_SYSC ...

  6. 真机调试之android手机+chrome

    真机调试之android手机+chrome 虽然chrome上的移动设备模拟器很强大,但是在真机运行的时候,总会遇到一些小问题,这时就需要使用真机调试了. 第一步:准备一台android手机,并在手机 ...

  7. ubuntu 远程开机

    启动者(电脑A)  ----------->  被远程开启的电脑(电脑B) 一.被远程开启的电脑(电脑B):1. 重新开机,并进到BIOS设定2. 把Wake On Land / Wake On ...

  8. BZOJ1568: [JSOI2008]Blue Mary开公司

    可以平衡树或线段树维护斜率来做. 还有一种线段树直接打标记的做法: 线段树每个节点存一条线段作为标记,打标记时如果已有标记,则把占优区间小的那个线段下放. #include<cstdio> ...

  9. C#----Get和Set在属性中的使用

    Get和Set在属性中的作用: 第一个作用:保证数据的安全性,对字段进行了有效的保护. 第二个作用:起到监视作用 private int width=0; public int Width { get ...

  10. TeXmacs - 所见即所得 - 专业排版软件

    所见即所得,支持中文,很好用, 容易奔溃,奔溃进入不了程序时,删除文件夹 C:\Users\Perelman\AppData\Roaming\TeXmacs