/*************************************************************************
> File Name: j.cpp
> Author: HJZ
> Mail: 2570230521@qq.com
> Created Time: 2014年08月28日 星期四 12时26分13秒
************************************************************************/ //提議:能否將一個給定的有向圖,變成兩個完全圖(任意兩點都直接相連,雙向邊) #include <queue>
#include <string>
#include <cstdio>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 105
using namespace std; int a[N], b[N];
//a, b集合初始化爲空!
int g[N][N];
int n; bool flag; bool dfs(int u, int la, int lb){
if(u>n && la+lb==n) return true;//如果a ,b集合中的元素數目恰好是n,則說明可以形成兩個完全圖!
bool flagx=true;
for(int i=; i<la && flagx; ++i)
if(!(g[a[i]][u] && g[u][a[i]]))
flagx=false;
if(flagx) a[la]=u;//如果u節點可以放入a集合中
if(flagx && dfs(u+, la+, lb)) return true;
bool flagy=true;
for(int i=; i<lb && flagy; ++i)
if(!(g[b[i]][u] && g[u][b[i]]))
flagy=false;
if(flagy) b[lb]=u;//如果u節點可以放入b集合中
if(flagy && dfs(u+, la, lb+)) return true;
return false;
} int main(){
while(scanf("%d", &n)!=EOF){
memset(g, , sizeof(g));
for(int i=; i<=n; ++i){
int v;
vis[i]=;
while(scanf("%d", &v) && v)
g[i][v]=;
}
flag=dfs(, , );
if(flag)
printf("YES\n");
else printf("NO\n");
}
return ;
}
 /*************************************************************************
> File Name: j.cpp
> Author: HJZ
> Mail: 2570230521@qq.com
> Created Time: 2014年08月28日 星期四 12时26分13秒
************************************************************************/
//思路:bfs,和判断二分图差不多,将图分成两个集合,如果a和b都有g[a][b]&&g[b][a]说明
//a和b一定在同一个集合中,如果有a,b不在一个集合中,a,c不在同一个集合中,b,c也不在同一个
//集合中,出现矛盾!也就是这个图不能分成两个完全图!
#include <queue>
#include <string>
#include <cstdio>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 105
using namespace std;
queue<int>q;
int g[N][N];
int coll[N];
int n; bool bfs(int u){
while(!q.empty()) q.pop();
q.push(u);
while(!q.empty()){
int x=q.front();
q.pop();
for(int y=; y<=n; ++y){
if(x==y || g[x][y]&&g[y][x]) continue;
if(coll[y]==-){
coll[y]=coll[x]^;
q.push(y);
}
else if(coll[y]==coll[x])
return true;
}
}
return false;
} int main(){
while(scanf("%d", &n)!=EOF){
memset(g, , sizeof(g));
for(int i=; i<=n; ++i){
int v;
while(scanf("%d", &v) && v)
g[i][v]=;
coll[i]=-;
}
int i;
for(i=; i<=n; ++i){
if(coll[i]==-){
coll[i]=;//默认是在集合0中
if(bfs(i)) break;
}
}
if(i<=n) printf("NO\n");
else printf("YES\n");
}
return ;
}

hdu4751Divide Groups(dfs枚举完全图集合或者bfs染色)的更多相关文章

  1. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  2. POJ1288 Sly Number(高斯消元 dfs枚举)

    由于解集只为{0, 1, 2}故消元后需dfs枚举求解 #include<cstdio> #include<iostream> #include<cstdlib> ...

  3. POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)

    题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd   lcm/gcd=a/gcd*b/gcd 可知a/gc ...

  4. POJ 1270 Following Orders (拓扑排序,dfs枚举)

    题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y.    要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...

  5. HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)

    想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...

  6. POJ 1753 Flip Game (DFS + 枚举)

    题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...

  7. Day054--MySQL, 创建用户和授权, 数据类型, 枚举和集合, 约束,唯一, 主键,外键

    创建用户和授权 1.创建用户: # 指定ip:192.118.1.1的mjj用户登录 create user 'mjj'@'192.118.1.1' identified by '123'; # 指定 ...

  8. 数据结构作业——图的存储及遍历(邻接矩阵、邻接表+DFS递归、非递归+BFS)

    邻接矩阵存图 /* * @Author: WZY * @School: HPU * @Date: 2018-11-02 18:35:27 * @Last Modified by: WZY * @Las ...

  9. HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

随机推荐

  1. 简单BigDecimal运算精度

    项目中遇到了数值运算,如网上所写的,一般有这几个方法: /** * 提供精确的加法运算. * @param v1 被加数 * @param v2 加数 * @return 两个参数的和 */ publ ...

  2. overview

    [1] Don’t panic! All will become clear in time; [2] You don’t have to know every detail of C++ to wr ...

  3. C#学习感悟

    上周虽然没上课,课上的内容是部分同学展示大作业成果,但是对于我来说,看了一些同学辛勤劳动的成果,听了他们对C#学习的一些感悟,我受益匪浅. 在这里我想谈谈我的收获.老师给的模板是todolist,但是 ...

  4. rdc21n(研发与设计综合讨论)博客开通了!

    rdc21n是“Research and Design Comprehensive discussioN”,其中21表示Comprehensive discussioN中间的21个字母(不包含空格), ...

  5. CardView

    CardView继承至FrameLayout类,可以在一个卡片布局中一致性的显示内容,卡片可以包含圆角和阴影.CardView是一个Layout,可以布局其他View. CardView常用属性: c ...

  6. NSString格式校验

    在项目开发过程中,NSString类型的变量是经常用到的,而且我们常常会对其格式进行对应的各种校验,你比如,在登录注册的时候,需要验证用户名的长度,用户名的字符组成等等,其实现在也有很多第三方提供的N ...

  7. Python path

    1.os.path 1.1.os.path.dirname(__file__) 1.1若文件__file__以绝对路径被运行的,则输出绝对路径 1.2若文件以相对路径被运行的,则输出为空 1.2.os ...

  8. Icident event 分析

    现象 备库中断,显示如下错误 Connect_Retry: 60 Master_Log_File: mysql-bin.000185 Read_Master_Log_Pos: 308647804 Re ...

  9. springrain 1.1 发布,spring 的极简封装

    经过2个月的测试修改,springrain1.1已经稳定,今日发布. 主要改动如下: 1.添加批量更新和保存的方法 2.添加maven分支 3.添加博客管理的demo 4.增加redis做为缓存实现 ...

  10. 使用SQL生成DateTime.Ticks

    在项目中我需要使用到一个随机数(Random Number),该随机数将作为 Hashtable 中的 Key 用于唯一索引数据,所以需要保持单机唯一性. 同时该随机数还需要具备可排序性以便对数据进行 ...