染色判断二分图+补图

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

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. js保留位和取整

    //hold是保留位,例,元,角,分 //integerType是在保留位的基础上,如果后面有值,向上向下取整 calAmount:function(hold,integerType,amount){ ...

  2. POJ 3617 Best Cow Line (贪心)

    Best Cow Line   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted: 4 ...

  3. mysql错误

    安装mysql之后提示(ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password:错误 具体就是: 安 ...

  4. PHP设计模式-策略模式 转

    策略模式(Strategy Pattern) 策略模式是对象的行为模式,用意是对一组算法的封装.动态的选择需要的算法并使用. 策略模式指的是程序中涉及决策控制的一种模式.策略模式功能非常强大,因为这个 ...

  5. 用arp-scan扫描局域网IP地址

    1,在安装之前需要安装yum install -y libpcap libpcap-devel如果没有安装yum工具需要用rpm安装如下软件包[root@oradba arp-scan-1.8]# y ...

  6. Android学习笔记——button_activity

    工程的功能是实现在一个acticity上点击按钮,切换到另外一个activity 以下代码为MainActivity.java中的代码 package com.example.button_activ ...

  7. 关于Spring常用的注解

    参考文献:http://www.cnblogs.com/xdp-gacl/p/3495887.html 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationC ...

  8. 15款优秀移动APP产品原型设计工具

    一新来小盆友问:“移动产品原型设计都用啥工具?” 答:“@#¥……&%*” 又问:“能详细说下各个工具吗?我比较一下” “……” 好吧,谁让我那么的爱分享而你又是小美女呢 ———————正文开 ...

  9. OpenGL瓶颈

    在优化Erya3D引擎的过程中,遇到的瓶颈: 1. 字符串操作,避免逐个字符的比较,使用哈希码比较2. 贴图操作:切换绑定贴图.更改贴图参数3. 切换绑定GLSL程序4. Draw Call:http ...

  10. ERROR 1130 (HY000) Host ‘hostname’ is not allowed to connect to this MySQL server

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'; FLUSH PRIVILEGES;