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
 
怒贴两种方法的代码,以表示我的愤怒,这么简单的题目都想的那么复杂
 
第一种是dfs染色法
 
 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 106
#define inf 1e12
int n;
vector<int>v[N];
int color[N];
int mp[N][N];
bool dfs(int u,int c){
color[u]=c;
for(int i=;i<v[u].size();i++){
int num=v[u][i];
if(color[num]!=-){
if(color[num]==c){
return false;
}
continue;
}
if(!dfs(num,!c)) return false;
}
return true;
}
int main()
{
while(scanf("%d",&n)==){
for(int i=;i<N;i++){
v[i].clear();
}
memset(mp,,sizeof(mp));
for(int i=;i<=n;i++){
int x;
scanf("%d",&x);
while(x!=){
//v[i].push_back(x);
//v[x].push_back(i);
mp[i][x]=;
scanf("%d",&x);
}
} for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(mp[i][j]== || mp[j][i]==){
v[i].push_back(j);
v[j].push_back(i);
}
}
} memset(color,-,sizeof(color));
int flag=;
for(int i=;i<=n;i++){
if(color[i]==- && !dfs(i,)){
flag=;
break;
}
}
if(flag){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return ;
}

第二种是2-sat,其实本质上和上一种是一样的

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 106
#define inf 1e12
int n,m; int mp[N][N];
int tot;
int head[N];
int vis[N];
int tt;
int scc;
stack<int>s;
int dfn[N],low[N];
int col[N];
struct Node
{
int from;
int to;
int next;
}edge[N*N];
void init()
{
tot=;
scc=;
tt=;
memset(head,-,sizeof(head));
memset(dfn,-,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(col,,sizeof(col));
}
void add(int s,int u)//邻接矩阵函数
{
edge[tot].from=s;
edge[tot].to=u;
edge[tot].next=head[s];
head[s]=tot++;
}
void tarjan(int u)//tarjan算法找出图中的所有强连通分支
{
dfn[u] = low[u]= ++tt;
vis[u]=;
s.push(u);
int cnt=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(dfn[v]==-)
{
// sum++;
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]==)
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int x;
scc++;
do{
x=s.top();
s.pop();
col[x]=scc;
vis[x]=;
}while(x!=u);
}
}
bool two_sat(){ for(int i=;i<*n;i++){
if(dfn[i]==-){
tarjan(i);
}
}
for(int i=;i<n;i++){
if(col[*i]==col[*i+]){
return false;
}
}
return true;
}
int main()
{
while(scanf("%d",&n)==){
init();
memset(mp,,sizeof(mp)); while(!s.empty()){
s.pop();
}
int a,b,c;
int x;
for(int i=;i<n;i++){
scanf("%d",&x);
while(x!=){
x--;
mp[i][x]=;
scanf("%d",&x);
}
}
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(i==j) continue;
if(mp[i][j]==){
add(*i,*j+);
add(*j,*i+);
add(*i+,*j);
add(*j+,*i);
}
}
}
if(two_sat()){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return ;
}

hdu 4751 Divide Groups(dfs染色 或 2-sat)的更多相关文章

  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 bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)

    SDUST的训练赛 当时死磕这个水题3个小时,也无心去搞其他的 按照题意,转换成无向图,预处理去掉单向的边,然后判断剩下的图能否构成两个无向完全图(ps一个完全图也行或是一个完全图+一个孤点) 代码是 ...

  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 (2-SAT)

    题意 给定一个有向图,问是否能够分成两个有向完全图. 思路 裸的2-sat--我们设一个完全图为0,另一个完全图为1,对于一个点对(u, v),如果u.v不是双向连通则它们两个不能在一组,即u和v至少 ...

  6. HDOJ 4751 Divide Groups

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

  7. hdu 4751(dfs染色)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...

  8. uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)

    Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...

  9. hdu 5313 Bipartite Graph(dfs染色 或者 并查集)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

随机推荐

  1. PHP冒泡排序,选择排序,插入排序

    1  冒泡排序是两个元素相互比较,找到最小值,然后冒泡到最后,代码如下:

  2. vs2010 正式版官方下载地址

    北京时间2010年4月12日12:00,微软Visual Studio 2010 正式版提供官方下载,眼下有三个版本号,Professional/Premium/Ultimate. 注意原页面的链接R ...

  3. [Hapi.js] Logging with good and good-console

    hapi doesn't ship with logging support baked in. Luckily, hapi's rich plugin ecosystem includes ever ...

  4. Swift——(一)为Swift内置类型加入属性

    在看苹果官方的Swift Language的时候,遇到实验:Write an extension for the Double type that add an absoluteValue prope ...

  5. BFC,IFC,GFC,FFC的定义及功能

    What's FC?一定不是KFC,FC的全称是:Formatting Contexts,是W3C CSS2.1规范中的一个概念.它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定 ...

  6. tableView特色用法

    // //  ViewController.m //  UITableView // //  Created by yhj on 15/12/15. //  Copyright © 2015年 QQ: ...

  7. 解决IE11只能用管理员身份运行的问题

    解决IE11只能用管理员身份运行的问题 IE11 打不开,必须要用管理员身份运行才可以打开,而且重置浏览器这个方法也不奏效. 今天本人也遇到了,上网查找发现是注册表权限的问题,原因尚不明确,安装了或被 ...

  8. OpenSSl编译

    1.下载openssl代码,下载地址:http://www.openssl.org/source/ ,如果使用winrar解压失败的话(提示不能创建符号链接),可以关闭UAC.2.下载安装Active ...

  9. cmd下操作mysql

      将mysql 安装目录下 的bin 添加到 windows 环境变量        步骤:        我的电脑 ->高级->环境变量->path->选择一个用户-> ...

  10. HibernateTemplate用法

    private HibernateTemplate hibernateTemplate; 使用HbernateTemplate HibernateTemplate提供持久层访问模板化,使用Hibern ...