HDU-4751 Divide Groups 染色问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751
题意:有n个人,每个人都认识一些人,要求把他们分成两个集合,使得两个集合中的人都相符两两认识。
如果两个人单向认识或者相互不认识,那么必定在不同的集合,因此建立边,染色就可以了。。
//STATUS:C++_AC_31MS_388KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e60;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int n;
int g[N][N],color[N];
vector<int> G[N]; bool bipartite(int u)
{
for(int i=;i<G[u].size();i++){
int v=G[u][i];
if(color[v]==color[u])return false;
if(!color[v]){
color[v]=-color[u];
if(!bipartite(v))return false;
}
}
return true;
} int main(){
// freopen("in.txt","r",stdin);
int i,j,a,b;
while(~scanf("%d",&n))
{
mem(g,);
for(i=;i<=n;i++)G[i].clear();
for(i=;i<=n;i++){
while(scanf("%d",&a) && a)g[i][a]=;
}
for(i=;i<=n;i++){
for(j=i+;j<=n;j++){
if( (!g[i][j]&&g[j][i]) || (g[i][j]&&!g[j][i]) || (!g[i][j]&&!g[j][i])) {
G[i].push_back(j);
G[j].push_back(i);
}
}
} mem(color,);
int ok=;
for(i=;i<=n;i++){
if(!color[i]){
color[i]=;
if(!bipartite(i)){
ok=;
break;
}
}
} printf("%s\n",ok?"YES":"NO");
}
return ;
}
HDU-4751 Divide Groups 染色问题的更多相关文章
- HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)
Divide Groups Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- hdu 4751 Divide Groups(dfs染色 或 2-sat)
Problem Description This year is the 60th anniversary of NJUST, and to make the celebration more c ...
- HDU 4751 Divide Groups 2013 ACM/ICPC Asia Regional Nanjing Online
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题目大意:判断一堆人能否分成两组,组内人都互相认识. 解题思路:如果两个人不是相互认识,该两人之 ...
- hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)
SDUST的训练赛 当时死磕这个水题3个小时,也无心去搞其他的 按照题意,转换成无向图,预处理去掉单向的边,然后判断剩下的图能否构成两个无向完全图(ps一个完全图也行或是一个完全图+一个孤点) 代码是 ...
- HDU 4751 Divide Groups
题目链接 比赛时候,建图建错了.大体算法想到了,不过很多细节都没想好. #include <cstdio> #include <cstring> #include <cm ...
- HDU 4751 Divide Groups (2-SAT)
题意 给定一个有向图,问是否能够分成两个有向完全图. 思路 裸的2-sat--我们设一个完全图为0,另一个完全图为1,对于一个点对(u, v),如果u.v不是双向连通则它们两个不能在一组,即u和v至少 ...
- HDOJ 4751 Divide Groups
染色判断二分图+补图 比赛的时候题意居然是反的,看了半天样例都看不懂 .... Divide Groups Time Limit: 2000/1000 MS (Java/Others) Memo ...
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- Hdu 4751(2-SAT)
题目链接 Divide Groups Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- mysql: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x90</...'
插入数据出现问题,因为包含了特殊字符. 现象: 插入的数据中如果含有某些特殊字符,会导致插入数据失败,例如字符串”测试**插入数据...“,在console中insert是正常的,但是使用java代码 ...
- BAT面经
http://bbs.csdn.net/topics/390734210?page=4 注意评论以及文章原地址
- php命令行用法简介
Php是一个非常流行的web服务端脚本语言.其实,php不仅仅可以在web服务器中充当重要角色.在命令行一样可以执行. 本文中,笔者为各位介绍下php在命令行中的使用方法. 1. 查看php的版本. ...
- 在Windows 上的 Python
在 Windows 上, 安装 Python 有两种选择. ActiveState 制作了一个 Windows 上的 Python 安装程序称为 ActivePython, 它包含了一个完整的 Pyt ...
- Storm集群的搭建
storm的环境和hadoop的环境没有任何关系 1.安装Zookeeper集群 2.解压storm 3.修改文件conf/storm.yaml 3.1.配置zookeeper服务器 storm.zo ...
- Word 中没有Endnote工具栏的解决方法
环境:Windows XP + Word 2003 + EndNote 6 以下各方法可以依次试一下,需要重启Word后才能看到是否可行.1 视图 -- 工具栏 -- EndNote,是否打勾.2 w ...
- java6 新特新
JAVA6新特性介绍 1. 使用JAXB来实现对象与XML之间的映射 JAXB是Java Architecture for XML Binding的缩写,可以将一个Java对象转变成为XML格式, ...
- uva12716GCD XOR
筛法,打表. 通过打表可知,但gcd(a,b)==a xor b时,a xor b = a-b. 就是求满足 c = a-b且c = a xor b 的c的个数. #include<cstdio ...
- noip2003提高组题解
这一年的前三题虽然难度不高,但是第二题极为繁琐,想在考场上用较短的时间拿到第二题的分数难上加难.所以必须要调整策略,争取拿其他三题的分数.第四题是比较普通的搜索题,分数比较好拿,但是很容易想成树形DP ...
- Linux 7 中Yum 配置 说明
在之前的Blog中写了Linux Yum 的配置方法,参考: Linux 平台下 YUM 源配置 手册 http://www.cndba.cn/account/article/details/154 ...