hdu 4751(dfs染色)
题目链接: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染色)的更多相关文章
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- 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 5313 Bipartite Graph(dfs染色 或者 并查集)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)
SDUST的训练赛 当时死磕这个水题3个小时,也无心去搞其他的 按照题意,转换成无向图,预处理去掉单向的边,然后判断剩下的图能否构成两个无向完全图(ps一个完全图也行或是一个完全图+一个孤点) 代码是 ...
- HDU 5143 DFS
分别给出1,2,3,4 a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...
- Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
- cf804C(dfs染色)
题目链接: http://codeforces.com/problemset/problem/804/C 题意: 有一颗含有 n 个顶点的树, 第 i 个顶点上有 k 个冰激凌, 每个冰激凌的种类为 ...
- 紫书 习题8-9 UVa 1613 (dfs染色+图的性质)
这道题一开始我没想什么直接开始染, 但是是for循环一个节点一个节点染, 然后就WA 后了看了https://www.cnblogs.com/jerryRey/p/4702323.html 发现原来还 ...
- 【POJ - 2386】Lake Counting (dfs+染色)
-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...
随机推荐
- java常见异常类图(分类了Error/RuntimeExecption、check Exception)
版权:欧初权 http://www.cnblogs.com/langtianya/p/4435537.html
- Linux 解压命令tar的理解
今天回顾了下tar 这个打包工具的一些常用参数 选项与参数: -c :创建打包文件,可搭配 -v 来察看过程中被打包的档名(filename) -t :察看打包文件的内容含有哪些档名,重点在察看『档名 ...
- shell截取字符串
image_tag="pangu-20151021102145\"" 1.用#号截取,符号-右面所有字符串 TMP=${image_tag#*-} echo $TMP 得 ...
- JSONModel - 字符串换转实体类
JSONModel https://github.com/icanzilb/JSONModel/ 一. 获取属性的元数据 const char *attrs = property_getAttrib ...
- [BZOJ1659][Usaco2006 Mar]Lights Out 关灯
[BZOJ1659][Usaco2006 Mar]Lights Out 关灯 试题描述 奶牛们喜欢在黑暗中睡觉.每天晚上,他们的牲口棚有L(3<=L<=50)盏灯,他们想让亮着的灯尽可能的 ...
- DCMTK3.6.0(MD支持库)安装说明
一.运行环境:WIN7 32bit + VisualStudio2008 + dcmtk3.6.0 + Cmake2.8.8 或者 WIN7 64bit 二.准备工作: 1)MD/MT的知识储备: / ...
- C/C++ 文件操作
C/C++ 文件操作大概有以下几种 1.C的文件操作: 2.C++的文件操作: 3.WINAPI的文件操作: 4.BCB库的文件操作: 5.特殊文件的操作. 当然了,水题时最常用的当然还是: freo ...
- 基于nginx和uWSGI在Ubuntu上部署Django
转自: http://www.jianshu.com/p/e6ff4a28ab5a
- 《ASP.NET1200例》ListView控件之修改,删除与添加
aspx <body> <form id="form1" runat="server"> <div> <asp:Lis ...
- A + B Problem
Write a function that add two numbers A and B. You should not use + or any arithmetic operators. 分析: ...