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<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
const int maxn = ;
int n,col[maxn];
bool kw[maxn][maxn];
vector<int> g[maxn];
bool dfs(int x,int c){
col[x] = c;
int to;
for(int i = ;i < g[x].size();i++){
to = g[x][i];
if(col[to] == c) return false;
if(!col[to] && !dfs(to,-c)) return false;
}
return true;
}
void get_ans(){
for(int i = ;i <= n;i++){
if(!col[i]){
if(!dfs(i,)){
printf("NO\n");
return;
}
}
}
printf("YES\n");
}
int main(){
int to;
while(scanf("%d",&n) == ){
for(int i = ;i <= n;i++) g[i].clear();
memset(col,,sizeof(col));
memset(kw,,sizeof(kw));
for(int i = ;i <= n;i++){
while(scanf("%d",&to) == &&to){
kw[i][to] = true;
}
}
for(int i = ;i <= n;i++){
for(int j = i + ;j <= n;j++){
if(!kw[i][j] || !kw[j][i]){
g[i].push_back(j);
g[j].push_back(i);
}
}
}
get_ans();
}
return ;
}

hdu4751 Divide Groups的更多相关文章

  1. HDU-4751 Divide Groups 染色问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题意:有n个人,每个人都认识一些人,要求把他们分成两个集合,使得两个集合中的人都相符两两认识. ...

  2. HDOJ 4751 Divide Groups

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

  3. HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)

    Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. Divide Groups 二分图的判定

    Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. 【HDU4751】Divide Groups

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

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

    Problem Description   This year is the 60th anniversary of NJUST, and to make the celebration more c ...

  7. HDU 4751 Divide Groups

    题目链接 比赛时候,建图建错了.大体算法想到了,不过很多细节都没想好. #include <cstdio> #include <cstring> #include <cm ...

  8. HDU 4751 Divide Groups 2013 ACM/ICPC Asia Regional Nanjing Online

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题目大意:判断一堆人能否分成两组,组内人都互相认识. 解题思路:如果两个人不是相互认识,该两人之 ...

  9. Divide Groups(分组)

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

随机推荐

  1. Git 服务器搭建

    接下来我们将以 Centos 为例搭建 Git 服务器. 1.安装Git $ yum install curl-devel expat-devel gettext-devel openssl-deve ...

  2. 12.04 redmine

    sudo apt-get redmine-mysql mysql-server-5.5 sudo apt-get redmine-mysql redmine(中间会死一段时间,没关系,等会会过去) 安 ...

  3. 使用Guava来计算笛卡尔积

    以前做项目的时候计算笛卡尔积的时候,总是使用各种for循环来嵌套,最后往往在Sonar代码检查的时候总是会报警说for循环嵌套过深. 今天才知道Guava原来已经为我们提供了优雅的计算笛卡尔积的方法. ...

  4. centos7安装eclipse

    centos7安装eclipse Eclipse是一个集成开发环境(IDE),包含一个基工作区和定制环境的可扩展插件系统.大部分使用 Java 编写,Eclipse 可以用来开发应用程序.通过各种插件 ...

  5. mysql简单操作(实时更新)

    从表中删除某条记录: delete from table_name where xx=xxxx; 创建数据库(注意不同系统对大小写的敏感性): create database xxx; 查看数据库列表 ...

  6. 收集的一些jQuery (我平常用的少的,但确实挺有效果的)

    禁用Jquery(动画)效果 jQuery.fx.off = true; 使用自己的 Bullets(这个有一丁点儿的小技巧) //这里是js代码 也就是给每个ul添加一个类名 然后给ul的子li前面 ...

  7. TCP服务器连接相关知识点

    # netstat -an|awk '/tcp/ {print $6}'|sort|uniq -c 68 CLOSE_WAIT2 CLOSING136 ESTABLISHED38 FIN_WAIT11 ...

  8. javascript中对象字面量的理解

    javascript中对象字面量与数组字面量 第一部分 我们知道JavaScript中的数据类型有基本数据类型和引用类型,其中Object类型就是非常常用的类型.那么如果创建一个Object类型的实例 ...

  9. JavaWeb---总结(十三)使用Session防止表单重复提交

    在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...

  10. cookie和浏览器

    XHR API允许应用添加自定义的HTTP首部(通过setRequestHeader()方法),同时也有一些首部都是应用代码不能设定的. Accept-Charset.Accept-Encoding. ...