uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description
In the ``Four Color Map Theorem" was proven with the assistance of a computer. This theorem states that every map can be colored using only four colors, in such a way that no region is colored using the same color as a neighbor region.
Here you are asked to solve a simpler similar problem. You have to decide whether a given arbitrary connected graph can be bicolored. That is, if one can assign colors (from a palette of two) to the nodes in such a way that no two adjacent nodes have the same color. To simplify the problem you can assume: no node will have an edge to itself.
the graph is nondirected. That is, if a node a is said to be connected to a node b, then you must assume that b is connected to a.
the graph will be strongly connected. That is, there will be at least one path from any node to any other node.
Input
The input consists of several test cases. Each test case starts with a line containing the number n ( < n < ) of different nodes. The second line contains the number of edges l. After this, l lines will follow, each containing two numbers that specify an edge between the two nodes that they represent. A node in the graph will be labeled using a number a ( $ \le a < n$).
An input with n = will mark the end of the input and is not to be processed.
Output
You have to decide whether the input graph can be bicolored or not, and print it as shown below.
Sample Input
Sample Output
NOT BICOLORABLE.
BICOLORABLE.
dfs二分染色,和hdu 4751代码差不多
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 206
#define inf 1e12
int n,m;
vector<int>g[N];
int color[N]; bool dfs(int u,int c){
color[u]=c;
for(int i=;i<g[u].size();i++){
int num=g[u][i];
if(color[num]!=-){
if(color[num]==c){
return false;
}
continue;
}
if(!dfs(num,!c)) return false;
}
return true;
}
int main()
{
while(scanf("%d",&n)== && n!=){
for(int i=;i<N;i++){
g[i].clear();
}
scanf("%d",&m);
for(int i=;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
} memset(color,-,sizeof(color));
int flag=;
for(int i=;i<n;i++){
if(color[i]==- && !dfs(i,)){
flag=;
break;
}
}
if(flag){
printf("BICOLORABLE.\n");
}
else{
printf("NOT BICOLORABLE.\n");
}
}
return ;
}
uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)的更多相关文章
- UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)
d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍 ...
- UVA 10004 Bicoloring
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&pa ...
- UVA 10004 Bicoloring(DFS染色)
题意: 给N个点构成的无环无向图,并且保证所有点对都是连通的. 给每个点染色,要么染成黑要么染成白.问是否存在染色方案使得所有有边相连的点对颜色一定不一样. 是输出 BICOLORABLE 否则输出 ...
- HDU 1241 (DFS搜索+染色)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...
- (简单) POJ 2492 A Bug's Life,二分染色。
Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...
- Java实现 LeetCode 655 输出二叉树(DFS+二分)
655. 输出二叉树 在一个 m*n 的二维字符串数组中输出二叉树,并遵守以下规则: 行数 m 应当等于给定二叉树的高度. 列数 n 应当总是奇数. 根节点的值(以字符串格式给出)应当放在可放置的第一 ...
- hdu 4751(dfs染色)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...
- hdu 5188 dfs+二分
get了很多新技能 当时想到了用dfs,但是排序用的是限制时间排序,一直没搞出来. 正解: 二分用时,dfs判断,为了顺利进行做题,需要按照做题开始时间排序 还可以用dp 题意: 作为史上最强的刷子之 ...
- hdu 4751 Divide Groups(dfs染色 或 2-sat)
Problem Description This year is the 60th anniversary of NJUST, and to make the celebration more c ...
随机推荐
- QT5中如何使用QFtp类(这个类虽然没有被收录,但一直在更新)
由于QT5对QML的支持有很大的改进,所以打算将原来基于QT4的程序移植到QT5上,在移植用QFtp类写的程序时傻眼了! Qt5 移除了 QFtp API,原因是其实现质量.QNetworkAcces ...
- C语言的本质(19)——预处理之一:宏定义
我们在写代码时已多次使用过以"#"号开头的预处理命令.如包含命令#include,宏定义命令#define等.在源程序中这些命令都放在函数之外,而且一般都放在源文件的前面,它们称为 ...
- VC++6.0下通过opencv读入图像并反色
第一个opencv测试程序: 不多说,直接上代码,代码注释很详尽: ////////////////////////////////////////////////////////////////// ...
- 杭电oj 1009 FatMouse' Trade
Tips:本题采用贪心算法,类似于背包问题,关键在于读入数据之后,将数据按 J[i]/F[i] 从大到小排列即可. /**本程序主要采用贪心算法思想,类似于背包问题*/ #include<std ...
- #include <queue>
双端队列deque比向量vector更有优势 双端队列(deque) 连续存储的指向不同元素的指针所组成的数组<deque> 队列(queue) 先进先出的值的排列 <queue&g ...
- STL入门
STL入门 STL的组成 六大组件 容器container 算法algorithm 迭代器iterator 仿函数function object 适配器adaptors 空间配制器allocator ...
- sql server中的 stuff 函数
STUFF eg: SELECT stuff(12345,3,1,'-') result: 12-45 以下文段来自:http://www.cnblogs.com/lingxyd/archive ...
- 求职(2015南京站获得百度、美的集团、趋势科技、华为offer)
版权所有所有:没有马缰绳chhuach(CSDN博客源).转载请注明出处. 禁止www.haogongju.net转载. 特此声明 一.开篇: 9月底,找工作接近尾声,笔者主要经历了2015年南京站百 ...
- JAVA 和 C# 调用外部.exe文件,传值并等等exe完成,获取返回值
JAVA- String ykexe = getProperty("ykexe") + " " + tableout; //getproperty(" ...
- JavaScript检测原始值、引用值、属性
上周写过一篇读书笔记<编写可维护的JavaScript>之编程实践,其中 第8章 避免『空比较』是博主在工作中遇坑较多的雷区,所以特此把该章节重新整理分享,希望大家不再坑队友(>﹏& ...