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=;
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]!=-&&color[u]!=co) return true;
if(color[u]==co) return false;
color[u]=!co;
for(int i=;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,,sizeof(book));
memset(book1,-,sizeof(book1));
memset(G,,sizeof(G));
memset(path,,sizeof(path));
memset(color,-,sizeof(color));
int tmp;
for(int i=;i<=n;i++)
{
while(scanf("%d",&tmp))
{
if(tmp==) break;
G[i][tmp]=;
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(G[i][j]!=||G[j][i]!=)
{
if(i==j) continue;
book[i]=book[j]=;
path[i][j]=;
}
}
}
int flag=;
for(int i=;i<=n;i++)
{
if(book[i])
if(!dfs(i,)) {flag=;break;}
memset(color,-,sizeof(color));
}
if(flag) printf("NO\n");
else printf("YES\n");
}
return ;
}
 

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. HDOJ 4751 Divide Groups

    染色判断二分图+补图 比赛的时候题意居然是反的,看了半天样例都看不懂 .... Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  3. 二分图的判定hihocoder1121 and hdu3478

    这两个题目都是二分图的判定,用dfs染色比较容易写. 算法流程: 选取一个没有染色的点,然后将这个点染色,那么跟他相连的所有点一定是不同颜色的,所以,如果存在已经染过颜色的,如果和这个颜色相同的话,就 ...

  4. hdu_2444The Accomodation of Students(二分图的判定和计算)

    hdu_2444The Accomodation of Students(二分图的判定和计算) 标签:二分图匹配 题目链接 题意: 问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两 ...

  5. 【HDU4751】Divide Groups

    题目大意:给定 N 个点和一些有向边,求是否能够将这个有向图的点分成两个集合,使得同一个集合内的任意两个点都有双向边联通. 题解:反向思考,对于没有双向边的两个点一定不能在同一个集合中.因此,构建一个 ...

  6. POJ:2492-Bug's Life(二分图的判定)

    Bug's Life Time Limit: 10000MS Memory Limit: 65536K Description Background Professor Hopper is resea ...

  7. 双栈排序(洛谷P1155)二分图的判定+思维贪心

    题目:戳这里 题目大意: 给你一个数列,问能否通过两个栈的push与pop把它输出成一个升序序列(每个数只能入队并出队一次) 不能的话输出0,能的话输出操作方法 主要思路: 1.判断是否可以成功输出升 ...

  8. Divide Groups(分组)(二分图染色)

    题目链接 题目大意是说输入数字n 然后告诉你第i个人都认识谁? 让你把这些人分成两堆,使这每个堆里的人都互相认识. 做法:把不是互相认识的人建立一条边,则构建二分图,两堆的人肯定都互相认识,也就是说, ...

  9. hdu4751 Divide Groups

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

随机推荐

  1. [odb-users] Create schema error (unknown database schema '')

    Boris Kolpackov boris at codesynthesis.comFri May 31 11:13:02 EDT 2013 Previous message: [odb-users] ...

  2. E20171230-hm

    refine   vt. 提炼; 改善; 使高雅; revert  vi. 恢复; 重提; 回到…上; <律>归还;             vt. 使恢复原状;            n ...

  3. SQL 经典语句大全

    原地址:http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html 一.基础 1.说明:创建数据库 CREATE DATABASE ...

  4. STL内存分配方式

    关于STL用的很多比如map, vector, set, queue, stack等等.很少关注它的内存分配情况,但是经常遇到比如使用map,不停的在map中插入了一些很小的对象,然后释放了一些,然后 ...

  5. Android集成微信分享功能应用签名生成方法及分享不生效的问题

    通过友盟sdk集成微博.微信.qq等分享功能时,微博和qq很顺利,但在做微信集成时一直不成功.主要问题还是之前在微信开放平台申请创建移动应用时,对应用签名没有填写对,走了很多弯路现总结出来,加深记忆避 ...

  6. DatePickerDialog日期对话框以及回调函数的用法

    DatePickerDialog类的实例化需要用到回调接口,如下定义: android.app.DatePickerDialog.DatePickerDialog(Context context, O ...

  7. 北大ACM(POJ1018-Communication System)

    Question:http://poj.org/problem?id=1018 问题点:枚举. Memory: 564K Time: 329MS Language: C++ Result: Accep ...

  8. 3星|《哈佛商业评论》201708:IT项目风险之大远超你想象

    老牌管理学杂志.本期干货偏少,我评3星. 以下是本期一些信息的摘抄: 1:当我们调查被关闭餐馆周边的犯罪规律时,我们发现了与关闭药房同样的现象:被关闭餐馆周围财产犯罪和车内财物偷盗犯罪行为立即出现了上 ...

  9. Win7 下 PB (PowerBuilder) Insert Control 崩溃的解决办法

    环境: WIN7 x86  PB8.0, x64系统目录不同,不过也可以试试 Insert -> OLE... -> Insert Control  - 崩溃 如果网上提供的办法解决不了你 ...

  10. 再谈布局之 UIStackView

    UIStackView 是 iOS9 新增的一个布局技术.熟练掌握相当节省布局时间. UIStackView 是 UIView 的子类,是用来约束子控件的一个控件.但他的作用仅限于此,他不能被渲染(即 ...