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脚本练习
例1:写一个脚本,利用循环和continue关键字,计算100以内能被3整除的数之和 运行结果: 例2: 写一个脚本,利用循环和continue关键字,计算100以内能被3整除的数之和 运行结果: 例 ...
- 【LeetCode练习题】Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- CentOS bridge br0 kvm libvirt-xml
1,kvm bridge br0配置文件内容实例: ifcfg-em1配置文件内容Example: DEVICE=em1 Bridge=br0 TYPE=Ethernet onboot=yes NM_ ...
- Android学习之SharedPreferences类
SharedPreferences类 android.content.SharedPreferences 类概括: 访问和修改由函数getSharedPreferences(String,int)返回 ...
- hdu 5500 Reorder the Books(规律)
题意: 有一个1→n的排列形成的数列,我们要用最少的操作次数把这个数列从小到大排序,每次操作都是把一个数放到整个数列的最前面. 思路: 首先最大的数n是不用操作的(其他数操作好了,n ...
- 找出诡异的Bug:数据怎么存不进去
带着学生做课程设计.程序一大,课程中做过了小项目,练过了分解动作,一到合起来了,难免还是要乱了分寸.事实上,实战的功夫,就是这样出来的.(课程设计指导视频链接(第36课时,3.18 银行系统开发).课 ...
- JqueryUI-1
本文在于巩固基础 学习网址:http://jqueryui.com/ 基本概念:jQuery UI[是以 jQuery 为基础的开源 JavaScript 网页用户界面代码库.包含底层用户交互.动画. ...
- 页面样式base.css
下面是我用过多次的base.css.欢迎各种建议吐槽.大家共同进步. ;;} table{;} fieldset,img {;} address,caption, cite,code,dfn,em,s ...
- js的事件属性方法一览表
event对象常用属性和方法 event 对象用来表示当前事件,事件有很多状态,例如,鼠标单击时的位置,按下键盘时的按键,发生事件的HTML元素,是否执行默认动作,是否冒泡等,这些都是作为event对 ...
- UVA1600 Patrol Robot
题意: 求机器人走最短路线,而且可以穿越障碍.N代表有N行,M代表最多能一次跨过多少个障碍. 分析: bfs()搜索,把访问状态数组改成了3维的,加了个维是当前能跨过的障碍数. 代码: #includ ...