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年半年时间完成 ...
随机推荐
- [Redis] RDB & AOF
http://my.oschina.net/davehe/blog/174662 rdb - 存在dump.rdb 的二进制文件中 dump 整个db, 数据多的时候,不合适频繁保存,保存的时间间隔应 ...
- luigi学习4-构建工作流
luigi提供了两个基本单元来构造一个工作流,这两个基本单元分别是Task和Target.这两个单元都是抽象类,我们实现他们中的某些方法就可以了.除了这两个基本单元,还有一个重要的概念是Pramete ...
- php下intval()和(int)转换有哪些区别
想知道使用intval()和(int)转换有什么区别? 或者说两者有什么不同,包括功能.定义方面的.或者和使用频率.效率等. 复制代码代码如下: <?php echo "<br ...
- nginx php 安装
.选定源码目录选定目录 /data/klj/ cd /data/klj/ 2.安装PCRE库cd /data/klj/wget ftp://ftp.csx.cam.ac.uk/pub/software ...
- PhpStrom 配置Xdebug
1 到 http://xdebug.org/download.php下载xdebug.注意找到自己对应的php版本.或者可以通过 http://xdebug.org/wizard.php页面,将php ...
- 带有×的EditText
代码: EditTextWithDel.java(直接复制): package com.sunday.customs; import com.example.customs.R; import and ...
- ViewPager使用 -------滑动图片
package com.zzw.viewpage; import java.util.ArrayList; import java.util.List; import android.app.Acti ...
- java初探native
最近碰见一个java中一个native关键字,不知道是干什么的,如下: public native String FileName(String strURL); static{ ...
- ContentProvider与ContentResolver使用【转】
这篇文章被转载而转载者未注明原文出处,在此未加上原文地址链接,本人向原作者致以歉意. 下面是文章内容: 使用ContentProvider共享数据: 当应用继承ContentProvider类,并重写 ...
- NuGet 的使用
install-package entityframework//Enable-Migrations -ContextTypeName College.Models.CollegeEntities ...