UVa 10004 - Bicoloring

article/2025/11/5 3:29:41

FILE10004 - Bicoloring32340
42.67%
8939
86.93%
题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105


题目类型:搜索


题目:

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.

题目翻译:

1976年“四色定理”在计算机的帮助下被证明。 这个定理宣告任何一个地图都可以只用四种颜色来填充, 并且没有相邻区域的颜色是相同的。

现在让你解决一个更加简单的问题。 你必须决定给定的任意相连的图能不能够用两种颜色填充。 就是说,如果给其中一个分配一种颜色, 要让所有直接相连的两个节点不能是相同的颜色。 为了让问题更简单,你可以假设:

1. 没有节点是连接向它自己的。

2. 是无向图。  即如果a连接b, 那么b也是连接a的

3. 图是强连接的。就是说至少有一条路径可走向所有节点。


样例输入:

3
3
0 1
1 2
2 0
9
8
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0


样例输出:

NOT BICOLORABLE.
BICOLORABLE.



分析与总结:

方法一:广搜BFS

由题目可知,对于每个结点,所有和它相接的点必须和这个点颜色不一样。那么,很自然可以用广搜来做: 选取其中一点,给这个点赋值为一种颜色,可以用数字0来代替,然后进行广搜,那么所有和他相邻的点就可以赋值为另一种颜色,可以用1来代替。如此搜下去, 如果遇到一个点是已经赋值过了的,那就进行判断,他已经有的值是不是和这次要给它的值相同的,如果是相同的,就继续。如果不同的话,那么直接判断为不可以。


BFS代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 210
using namespace std;
int n, m, a, b, G[MAXN][MAXN], lastPos;
int vis[210],edge[250][2];
bool flag;int que[100000];
void bfs(int pos){int front=0, rear=1;que[0] = pos;while(front < rear){int m = que[front++];for(int i=0; i<n; ++i){if(G[m][i]){ if(!vis[i]){que[rear++] = i;vis[i] = vis[m]+1;}else if(vis[i]==vis[m]){flag = true;return;}}}}
}
int main(){
#ifdef LOCALfreopen("input.txt","r",stdin);
#endifwhile(~scanf("%d",&n) && n){memset(G, 0,sizeof(G));scanf("%d",&m);for(int i=0; i<m; ++i){scanf("%d %d",&a,&b);G[a][b] = 1;G[b][a] = 1;}memset(vis, 0, sizeof(vis));vis[0] = 1;flag = false;bfs(0);if(flag) printf("NOT BICOLORABLE.\n");else printf("BICOLORABLE.\n");}return 0;
} 




方法二: 深搜DFS

同样,这题也可以用深搜来做。 深搜的基本思想是,沿着一个方向不断搜下去,没走一步都进行染色,当前这一点的色和上一点的色相反。如果搜到了一个染过的(即有回环),那么也进行判断,已经有的色是不是和这次给它的颜色是否一致的。不一致的话,就判断为不可以。


DFS代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 210
using namespace std;
int n, m, a, b, G[MAXN][MAXN], lastPos;
int vis[210];
bool flag;void dfs(int pos){if(flag) return;for(int i=0; i<n; ++i){if(G[pos][i]){if(vis[i]==-1){vis[i] = !vis[pos];dfs(i);vis[i] = -1;}else if(vis[i] != !vis[pos]){flag = true;return;}}}
}int main(){
#ifdef LOCALfreopen("input.txt","r",stdin);
#endifwhile(~scanf("%d",&n) && n){memset(G, 0,sizeof(G));scanf("%d",&m);for(int i=0; i<m; ++i){scanf("%d %d",&a,&b);G[a][b] = 1;G[b][a] = 1;}flag = false;memset(vis, -1, sizeof(vis));vis[0] = 0;dfs(0);if(flag) printf("NOT BICOLORABLE.\n");else printf("BICOLORABLE.\n");}return 0;
} 



——      生命的意义,在于赋予它意义。 

                   原创  http://blog.csdn.net/shuangde800  , By   D_Double











http://chatgpt.dhexx.cn/article/RN3uP6s3.shtml

相关文章

SemanticException [Error 10004]: line 14:13 Invalid table alias or column reference ‘a‘: .....

原查询如下&#xff1a; with table1 as (SELECT a.mid, a.summary_time as summary_time FROM hst_dwd.dwd_member_consume_info a, hst_dwd.dwd_business_info b WHERE a.business_id b.business_id AND a.market_id 218 AND b.type_id 4 ) select…

SemanticException [Error 10004]: Line 1:30 Invalid table alias or column reference ‘customers_state‘

项目场景&#xff1a; hive sql 语句 执行报错 问题描述&#xff1a; SemanticException [Error 10004]: Line 1:30 Invalid table alias or column reference ‘customers_state’: (possible column names are: customer_id, customer_fname, customer_lname, customer_em…

hive sql报错:SQL 错误 [10004] [42000]: Error while compiling statement: FAILED: SemanticException [Error

SQL 错误 [10004] [42000]: Error while compiling statement: FAILED: SemanticException [Error 10004]: Line 64:0 Invalid table alias or column reference ‘T4’: (possible column names are: order_id, order_status, update_time, charge_id, charge_status, station…

【JavaScript】清空数组的三种方式

方式1&#xff0c;splice var ary [1,2,3,4]; ary.splice(0,ary.length); console.log(ary); // 输出 []&#xff0c;空数组&#xff0c;即被清空了方式2&#xff0c;length赋值为0 这种方式很有意思&#xff0c;其它语言如Java&#xff0c;其数组的length是只读的&#xff…

js-清空array数组

三种实现方式 1.splice&#xff1a;删除元素并添加新元素&#xff0c;直接对数组进行修改&#xff0c;返回含有被删除元素的数组。 arrayObject.splice(index,howmany,element1,…,elementX) index&#xff1a;必选&#xff0c;规定从何处添加/删除元素。 howmany&#xff1a;…

Vue中实现清空数组和清空el-table

场景 要实现的效果是 那么就要用到怎样将这个el-table清空&#xff0c;即在vue中怎样将数组清空。 注&#xff1a; 博客&#xff1a;https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书、教程推送与免费下载。 实现 首先将这个el-tab…

JavaScript清空数组的三种方法

1、用“length”清除 用length方法可以很轻松地清空数组&#xff0c;代码示例&#xff1a; var arr [1,2,3]; console.log(arr); arr.length 0; console.log(arr); 结果如下&#xff1a; 2、用“splice”清除 splice() 方法向/从数组中添加/删除项目&#xff0c;然后返回…

JavaScript中清空数组最有效的三种方法

文章目录 1、用“length”清除2、用“splice”清除3、用“[]”清除 1、用“length”清除 用length方法可以很轻松地清空数组&#xff0c;代码示例&#xff1a; var arr [1,2,3]; 1 console.log(arr); arr.length 0; console.log(arr); 123结果如下&#xff1a; 2、用“spli…

MVEL快速入门—MVEL属性和文字讲解(二)

相关文章 &#x1f525; MVEL快速入门—MVEL基础语法讲解&#xff08;一&#xff09; 以下为今日内容&#xff1a; 属性信息 MVEL的属性信息保持了其他bean一般通常使用的形式&#xff0c;略有区别的是&#xff0c;MVEL为访问属性、静态信息、Map等提供了统一的访问形式 Bea…

MVEL快速入门—MVEL基础语法讲解(一)

概述 MVEL是从英文翻译而来的&#xff0c;MVFLEX表达式语言是Java平台的动态/静态混合类型的运行时可嵌入表达式语言。该项目最初是作为应用程序框架的实用语言开始的&#xff0c;现在已完全独立开发。MVEL通常用于通过XML文件或注释等配置将基本逻辑公开给最终用户和程序员。它…

MVEL快速入门—MVEL流程控制和高级功能(三)

之前文章 MVEL快速入门—MVEL基础语法讲解&#xff08;一&#xff09; MVEL快速入门—MVEL属性和文字讲解&#xff08;二&#xff09; 流程控制 实际上MVEL的表达形式不仅仅局限于简单的表达式&#xff0c;他还支持流程控制。使我们能够执行高级的脚本。 if - then - else M…

int 类型和double类型数值转换

类型自动转换规则&#xff1a; 参与运算&#xff08;算数运算和赋值运算&#xff09;操作数和结果类型必须一致&#xff0c; 不一致时启动隐式转换&#xff1a; 两种类型兼容&#xff1a;int 和double兼容&#xff08;都是数字类型&#xff09; 目标类型大于原类型 Int 类型取值…

double转int精度丢失问题

先来看一下精度丢失的现象&#xff1a; #include <iostream> #include <cmath> using namespace std;int main() {double a 74.46;int b a * 100;cout << "a: " << a << " b: " << b <<endl;return 0; }结果…

double转换成int java,Java将double转换为int

本文概述 我们可以使用类型转换在Java中将double转换为int。要将double数据类型转换为int, 我们需要执行类型转换。 Java中的类型转换通过类型转换运算符(数据类型)执行。 在这里, 我们将学习如何将double基本类型转换为int以及将Double对象转换为int。 Java double to int示例…

int 转 double 的一个坑

int 转 double 的一个坑 首先&#xff0c;看这样这个语句 int max (int) Math.pow(2, 31);输出的应该是 2 32 2^{32} 232 的值&#xff0c;我们看运行结果 我们再看看科学计算器算出来的 这个时候问题就出来了&#xff0c;我们对比发现&#xff0c;结果相差1。 这里就是一个…

int转double会存在丢失?

问题是这样的&#xff0c;当我用一个int类型的值去整除100的时候&#xff0c;结果用double类型接收&#xff0c;直接变成0.0了&#x1f623; int a97;double ba/100;System.out.println("result:"b);输出结果如下&#xff1a; 正确写法&#xff1a; //1 double ba/…

C#基础③——类型转换(int转double、double转int、Convert)

类型转换是什么&#xff1f; 不同数据类型间的转换&#xff0c;如&#xff1a;将int类型转换为string类型 为什么需要类型转换&#xff1f; 从控制台接收到的用户输入的内容都是string类型&#xff0c;如果要进行计算&#xff0c;就需要将接收到的内容转换成数值类型 什么是…

在HTML中画一条横线

怎么画一条横线&#xff1f;我想到的有三种方法&#xff0c;但是各自样式不一&#xff0c;所以大家按需求来哦。 1.<hr />标签&#xff0c;对的&#xff0c;这个标签就代表一条横线&#xff0c;样式大概是这样的&#xff0c;如图&#xff08;a与b之间哦&#xff09;&…

如何去掉HTML中文字下面显示的横线

之前的状态&#xff1a; 之后的状态&#xff1a; 其实只需要在相对应的css样式中添加&#xff1a; text-decoration: none;