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 ...
随机推荐
- 实现Linux下的U盘(USB Mass Storage)驱动
如何实现Linux下的U盘(USB Mass Storage)驱动 版本:v0.7 How to Write Linux USB MSC (Mass Storage Class) Driver Cri ...
- 忽然有一种感觉:云存储必须从系统级定制,所以必须对Linux相当熟悉。Windows下开发软件的模式已经过时了
看了诸多招聘帖子以后的感觉- 工作内容: .存储相关产品的设计.开发和维护. .Linux系统应用程序研发. .主流Linux内核文件系统研发. .自动化测试框架和工具的研发. 职位要求: .计算机相 ...
- PHP单例模式编写
今天来点基础的设计模式: 如何利用单例模式实现一个数据库中间层 class Db{ static private $_instance; //当前数据库连接实例 static public funct ...
- 【C/C++】Linux下system()函数引发的错误
http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食 恋恋美食 发布时间: 2012/04/21 11:3 ...
- Tomcat无法安装 Check your settings and permissions Ignore and continue anyway
刚刚“sj”,把装在C盘的tomcat的文件夹给删除了,刚删完就想到干嘛不卸载啊,哎惯性思维啊,转而一想,tomcat这么简单安装,不怕不怕,后来一装,妈啊,装不了,百度之后原来是服务没有删除,好吧, ...
- C# 用模板生成静态页
最近在研究静态页输出的问题,找了一些资料.做了一个简单的模板模式的静态输出 模板代码: <html xmlns="http://www.w3.org/1999/xhtml"& ...
- css系列教程--color direction line-height letter-spacing
css标签:colorcolor:用法color:指定文本的颜色color:red/#fff/unicode; direction:用法 direction:定义文本的方向.dirction:ltr/ ...
- AES 加密,C#后台,javascript前台,crypt-js
javascript前台代码 <script src="http://apps.bdimg.com/libs/crypto-js/3.1.2/components/core-min.j ...
- 06JS高级创建对象使用原型共享对象方法
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 想使用WM_CONCAT 函数进行多列转一行,但发现没有
查看数据库版本: SELECT * FROM v$version; 1 Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bi ...