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. bzoj1509

    树的直径 我先开始以为是个图,想想并不知道什么求图的直径的方法,结果是棵树 那么直觉告诉我们是在直径上面,实际上就是直径+min(i->u,i->v),扫一遍就行了 #include< ...

  2. codeforces round #428 div2

    A:暴力模拟,能加就加,如果累计到了8就加上,每次累积 #include<bits/stdc++.h> using namespace std; int main() { ; scanf( ...

  3. jquery中$()的使用

    在jquery中最常使用的就是$这个符号了,在我没有系统的学习jquery之前,我用到的$都是用于对元素的选择,而这只是$的很简单的用法.在jquery$()函数一共有三种用法: $(selector ...

  4. JavaScript--History 对象

    history对象记录了用户曾经浏览过的页面(URL),并可以实现浏览器前进与后退相似导航的功能. 注意:从窗口被打开的那一刻开始记录,每个浏览器窗口.每个标签页乃至每个框架,都有自己的history ...

  5. SPFA+Dinic HDOJ 5294 Tricks Device

    题目传送门 /* 题意:一无向图,问至少要割掉几条边破坏最短路,问最多能割掉几条边还能保持最短路 SPFA+Dinic:SPFA求最短路时,用cnt[i]记录到i最少要几条边,第二个答案是m - cn ...

  6. HTML基础2——综合案例1——如何用iis配置网站

      1.打开iis 如果机子上面没有iis,可以先装一个,不同的系统可能安装步骤不一样,至于iis的安装方法,大家可以去百度找找.   2.准备网站源程序 既然要配置网站,肯定要先准备好网站源程序,网 ...

  7. C# 文件压缩方法

    using System; using System.IO; using System.IO.Packaging; namespace Utility { public class ZipHelper ...

  8. 使用SpringMvc的一个注意事项

    在Intelij Idea下,如果在新建项目时使用了自带的模板,那么自动生成的web.xml里的DispatcherServlet配置节点默认的servlet-mapping是这样的: 而习惯上,我们 ...

  9. Elasticsearch--扩展索引结构

    目录 索引树形数据 索引非扁平数据 索引关系型数据 使用嵌套对象 评分与嵌套查询 使用主从关系 索引树形数据 使用path_analyzer分析树形数据字段 索引非扁平数据 数据如下: { " ...

  10. android开发小内容

    EditText弹出输入数字:android:inputType="phone"