Divide Groups 二分图的判定

Divide Groups

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

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

#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
const int Max=111;
int G[Max][Max];
int path[Max][Max],book[Max],color[Max],book1[Max];
int n;
bool dfs(int u,int co)
{
//  cout<<u<<" "<<color[u]<<" "<<co<<endl;
  if(color[u]!=-1&&color[u]!=co) return true;
  if(color[u]==co) return false;
  color[u]=!co;
  for(int i=1;i<=n;i++)
  {
      if(u==i) continue;
      if(path[u][i]||path[i][u])
      if(book[i]) if(!dfs(i,!co)) return false;
  }
  return true;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(book,0,sizeof(book));
        memset(book1,-1,sizeof(book1));
        memset(G,0,sizeof(G));
        memset(path,0,sizeof(path));
        memset(color,-1,sizeof(color));
        int tmp;
        for(int i=1;i<=n;i++)
        {
            while(scanf("%d",&tmp))
            {
                if(tmp==0) break;
                G[i][tmp]=1;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                 if(G[i][j]!=1||G[j][i]!=1)
                 {
                     if(i==j) continue;
                     book[i]=book[j]=1;
                     path[i][j]=1;
                 }
            }
        }
        int flag=0;
        for(int i=1;i<=n;i++)
        {
          if(book[i])
           if(!dfs(i,1)) {flag=1;break;}
          memset(color,-1,sizeof(color));
        }
        if(flag) printf("NO\n");
        else     printf("YES\n");
    }
    return 0;
}
时间: 2024-11-03 21:00:55

Divide Groups 二分图的判定的相关文章

hdu 4751 Divide Groups 二分图

题意给出所有人之间的关系,单向的,问能不能将这群人分成两组,使得每组内部的任意两人都互相认识. 先把单向边都换成无向边,即如果a,b互相认识那么不变,如果只是单向边的话那么则认为他们两个不认识,然后假设能分成满足题意的两个集合,那么新图的补图中这两个集合内部是没有边的,所以只要判断补图是不是二分图即可. #include <cstdio> #include <cstring> #include <queue> using namespace std; int G[210

HDU 4751 Divide Groups

Divide Groups Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 475164-bit integer IO format: %I64d      Java class name: Main   This year is the 60th anniversary of NJUST, and to make the celebration more colo

POJ 2289 Jamie&#39;s Contact Groups 二分图多重匹配

题意:n个人,m个组,给出每个人可以分到那些组中,把每个人都分进某组中,问最大组的人数最小为?n<=1e3,m<=500,二分图多重匹配 左边为人 右边为分组 可以多个人对一个组由于要求最大组的最小人数 二分答案x,右边点最多流量为x,判断匹配数是否n即可. #include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; t

hdu 4751 Divide Groups 2—sat问题 还是未理解

Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1443    Accepted Submission(s): 512 Problem Description This year is the 60th anniversary of NJUST, and to make the celebration mor

POJ2289 Jamie&#39;s Contact Groups —— 二分图多重匹配/最大流 + 二分

题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 8147   Accepted: 2736 Description Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very lon

HDU 4751 Divide Groups(二分图的判断)

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, on

HDU 4751 Divide Groups(判断是否为二分图)

#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <set> #define LL long long #define FOR(i, x, y) for(int i=x;i<=

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

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

#1121 : 二分图一?二分图判定 (HIHOCoder +二分图的判定)

#1121 : 二分图一?二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Nettle,从这个星期开始由我来完成我们的Weekly. 新年回家,又到了一年一度大龄剩男剩女的相亲时间.Nettle去姑姑家玩的时候看到了一张姑姑写的相亲情况表,上面都是姑姑介绍相亲的剩男剩女们.每行有2个名字,表示这两个人有一场相亲.由于姑姑年龄比较大了记性不是太好,加上相亲的人很多,所以姑姑一时也想不起来其中有些人的性别.因此她拜托我检查一