UVA 10004 Bicoloring
Problem:In 1976 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 ( 1 < n< 200) 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 (
).
An input with n = 0 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.
解法:判断是否可以二分染色。直接dfs即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#define inf 0x7fffffff
#define exp 1e-10
#define PI 3.141592654
using namespace std;
const int maxn=;
int color[maxn];
vector<int> G[maxn];
int bi(int u)
{
int k=G[u].size();
for (int i= ;i<k ;i++)
{
int v=G[u][i];
if (!color[v])
{
color[v]=-color[u];
if (!bi(v)) return false;
}
if (color[v]==color[u]) return false;
}
return true;
}
int main()
{
int n,l;
while (scanf("%d",&n)!=EOF)
{
if (!n) break;
scanf("%d",&l);
for (int i= ;i<=n ;i++) G[i].clear();
memset(color,,sizeof(color));
int a,b;
for (int i= ;i<l ;i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
color[]=;
int flag=bi();
if (flag) cout<<"BICOLORABLE."<<endl;
else cout<<"NOT BICOLORABLE."<<endl;
}
return ;
}
UVA 10004 Bicoloring的更多相关文章
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)
d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍 ...
- UVA 10004 Bicoloring(DFS染色)
题意: 给N个点构成的无环无向图,并且保证所有点对都是连通的. 给每个点染色,要么染成黑要么染成白.问是否存在染色方案使得所有有边相连的点对颜色一定不一样. 是输出 BICOLORABLE 否则输出 ...
- UVa 10004:Bicoloring
这道题要我们判断所给图是否可以用两种颜色进行染色,即"二染色“.已知所给图一定是强连通图. 分析之: 若图中无回路,则该图是一棵树,一定可以二染色. 若图中有回路,但回路有偶数个节点,仍然可 ...
- Bicoloring UVA - 10004 二分图判断
\(\color{#0066ff}{题目描述}\) 多组数据,n=0结束,每次一个n,m,之后是边,问你是不是二分图 \(\color{#0066ff}{输入样例}\) 3 3 0 1 1 2 2 0 ...
- uva 交叉染色法10004
鉴于网上讲交叉染色的资料比较少,于是我把我自己的心得与方法贴出来,方便与大家共同进步. 二分图: 百度百科传送门 wiki百科传送门 判断一个图是否为二分图可以用交叉染色的方法来判断,可以用BFS,也 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
随机推荐
- MongoDb gridfs-ngnix文件存储方案 - 图片
http://www.cnblogs.com/wintersun/p/4622205.html 在各类系统应用服务端开发中,我们经常会遇到文件存储的问题. 常见的磁盘文件系统,DBMS传统文件流存储. ...
- decode行转列,case when,
1.行转列 转之前:
- 大体了解Lua的语法
Lua 的语法比较简单,学习起来也比较省力,但功能却并不弱. 在Lua中,一切都是变量,除了关键字.请记住这句话. I. 首先是注释 写一个程序,总是少不了注释的. 在Lua中,你可以使用单行注释和多 ...
- mongodb 3.2存储目录结构说明
[root@hadoop1 mongodb]# tree ./data ./data |-- WiredTiger | |-- WiredTiger.lock | |-- WiredTiger.tur ...
- 13.python中的字典
字典其实和之前的元祖和列表功能相似,都是用来储存一系列对象的.也就是一种可变容器,或者是我所比喻的革新派的菜单. 但也不是完全相同,我在之前曾经将字典称为特殊的'序列',是字典拥有序列的部分特性,但是 ...
- xm 命令详解
xm 命令详解 xm addlabel label dom configfile [policy] xm addlabel label res resource [policy] 增加了名称为labe ...
- Python sequence (序列)
序列简介 sequence 是一组有序元素的组合 序列可以是多个元素,也可以一个元素都没有 序列有2种:tuple(定值表).List(表) D:\python\Python_Day>pytho ...
- Android UmengShareSDK第三方登录
Android UmengShareSDK 第三方登录- 今天就不废话了,集成平台第三方登录.市面上集成平台有shareSDK 和 Ument两种,shareSDK的ipa和服务好些,如果自己研究会很 ...
- [译]rabbitmq 2.5 Where’s my message? Durability and you
我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. There’s a dirty secret about creating queues and exchanges in ...
- [转]Gridview中实现RadioButton单选效果
HTML <asp:TemplateField ItemStyle-Width="22px"> <ItemTemplate> <asp:RadioBu ...