题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751

思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不在同一个图中的,于是我们可以用dfs染色的方法来判断是否存矛盾。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std; int map[][];
int color[];
int n;
vector<vector<int> >g; bool dfs(int u,int father,int state)
{
color[u]=state;
for(int i=;i<g[u].size();i++){
int v=g[u][i];
if(v==father)continue;
if(color[v]&&color[u]==color[v]){
return false;
}else if(!color[v]&&!dfs(v,u,-state)){
return false;
}
}
return true;
} int main()
{
int x;
while(~scanf("%d",&n)){
memset(map,,sizeof(map));
memset(color,,sizeof(color));
for(int i=;i<=n;i++){
while(true){
scanf("%d",&x);
if(x==)break;
map[i][x]=;
}
}
g.clear();
g.resize(n+);
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(map[i][j]&&map[j][i])continue;
g[i].push_back(j);
g[j].push_back(i);
}
}
int flag=;
for(int i=;i<=n;i++){
if(g[i].size()>&&!color[i]){
if(!dfs(i,i,)){
flag=;
break;
}
}
}
flag?puts("YES"):puts("NO");
}
return ;
}

hdu 4751(dfs染色)的更多相关文章

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

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

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

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

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

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

  4. hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)

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

  5. HDU 5143 DFS

    分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...

  6. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

  7. cf804C(dfs染色)

    题目链接: http://codeforces.com/problemset/problem/804/C 题意: 有一颗含有 n 个顶点的树, 第 i 个顶点上有 k 个冰激凌, 每个冰激凌的种类为 ...

  8. 紫书 习题8-9 UVa 1613 (dfs染色+图的性质)

    这道题一开始我没想什么直接开始染, 但是是for循环一个节点一个节点染, 然后就WA 后了看了https://www.cnblogs.com/jerryRey/p/4702323.html 发现原来还 ...

  9. 【POJ - 2386】Lake Counting (dfs+染色)

    -->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...

随机推荐

  1. Cannot find class [org.apache.commons.dbcp.BasicDataSource]

    错误:Cannot find class [org.apache.commons.dbcp.BasicDataSource] 原因:缺少commons-dbcp.jar

  2. 根据ID获取CEdit的句柄实例

    MyApp.h和MyApp.cpp #ifndef MY_APP_H_ #define MY_APP_H_ #include <afxwin.h> class CMyApp:public ...

  3. [转载]如何在Ubuntu上安装LAMP服务器系统

    [2013年7月25日 51CTO外电头条]为何应该在Ubuntu上安装LAMP服务器?从事Web开发工作时,我更偏爱在不受干扰的情况下,在我那台计算机上的开发环境下进行开发.我宁愿所犯的错误大部分是 ...

  4. [转] Android开发者必备的42个链接

    下面收集了42个帮助大家学习Android的内容链接,部分内容是面向初学者的,帮助大家从头开始学习Android开发,其他则面向较高级的开发者.希望推荐的这些内容对你有帮助. 官方网站 1.谷歌And ...

  5. windows路径操作API函数

    备用,方便查找: PathRemoveArgs     去除路径的参数 PathRemoveBackslash   去除路径最后的反斜杠"\" PathAddBackslash 在 ...

  6. [Effective JavaScript 笔记]第52条:数组字面量优于数组构造函数

    js的优雅很大程序要归功于程序中常见的构造块(Object,Function及Array)的简明的字面量语法.字面量是一种表示数组的优雅方法. var a=[1,2,3,5,7,8]; 也可以使用构造 ...

  7. Stanford机器学习---第六讲. 怎样选择机器学习方法、系统

    原文:http://blog.csdn.net/abcjennifer/article/details/7797502 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...

  8. linux dd命令实用详解

    linux dd命令刻录启动U盘详解 dd命令做usb启动盘十分方便,只须:sudo dd if=xxx.iso of=/dev/sdb bs=1M 用以上命令前必须卸载u盘,sdb是你的u盘,bs= ...

  9. Thinkphp中eq,neq,gt,lt等表达式缩写

    eq 等于  equalneq 不等于gt 大于   greater thanegt 大于等于lt 小于     less thanelt 小于等于like LIKEbetween BETWEENno ...

  10. neutron 中 flat vlan gre vxlan的区别

    In a flat network, everyone shares the same network segment. For example, say 2 tenants are sharing ...