题目链接: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 ;
}

最新文章

  1. MFC Picture控件加载图片
  2. 设计模式之美:Null Object(空对象)
  3. [转]Neural Networks, Manifolds, and Topology
  4. (十一) 一起学 Unix 环境高级编程 (APUE) 之 高级 IO
  5. Java的多线程机制系列:(四)不得不提的volatile及指令重排序(happen-before)
  6. dos 下 查看和设置classpath的命令
  7. 下载、运行docker
  8. PLSQL_性能优化工具系列16_Best Practices: Proactively Avoiding Database
  9. Project Euler 91:Right triangles with integer coordinates 格点直角三角形
  10. 1、探究java方法参数传递——引用传递?值传递!
  11. 第二个C语言代码
  12. HTML5画布
  13. mysl lock table read
  14. THINKPHP中几个缓存的问题
  15. python_怎么格式化字符串?
  16. Hyper-V安装虚拟机
  17. [JUC-4]ThreadPoolExecutor源码分析
  18. lambda表达式底层处理机制
  19. RabbitMQ安装教程
  20. Latex使用:在latex中添加算法模块

热门文章

  1. Java API —— Map接口
  2. bootstrap 3.x笔记
  3. class_create()
  4. [58 Argo]58同城开源web框架Argo搭建实践
  5. Jeally Bean中MonekyRunner 帮助文件
  6. Redis必要的一些配置
  7. ListView 点击某一项换背景图片
  8. Java知识点:条件编译
  9. Sublime-text markdown with Vim mode and auto preview
  10. GBDT(Gradient Boosting Decision Tree)算法&amp;协同过滤算法