Uva 10004 - Bicoloring
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.在1976年,四色定理在计算机的辅助下被证明了,这个定理的内容指的是地图中每个相邻的区域用不同的颜色填充,那么按这种方式填充一张地图能被四种颜色涂满
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.图是无向图,也就是说,如果 a 连向 b, 那么 b 也连向 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 ( ).输入有多组测试数据,每组测试数据开始第一行是一个数 n,表示有节点的数目,第二行是一个数 l,接下来是 l 行数据,每行包含两个数字说明在这两个数之间有边连接
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.
Sample Input
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
Sample Output
NOT BICOLORABLE. BICOLORABLE.
Miguel Revilla
2000-08-21
#include<stdio.h> #include<string.h> #define MAXN 210 int maze[MAXN][MAXN]; int flag[MAXN], visit[MAXN]; int Traverse(int current, int x, int n) { int i, j; visit[current] = 1; for(i=0; i<n; ++i) { if(maze[current][i] == 1) { if(flag[i] == 0) { flag[i] = x; return Traverse(i, -x, n); } else if(flag[i] == x) { if(!visit[i]) return Traverse(i, -x, n); } else return 0; } } return 1; } int main() { int n, m, l, i, j, x, y; while(scanf("%d", &n) != EOF && n) { memset(flag, 0, sizeof(flag)); memset(visit, 0, sizeof(visit)); memset(maze, 0, sizeof(maze)); scanf("%d", &l); for(i=0; i<l; ++i) { scanf("%d%d", &x, &y); maze[x][y] = maze[y][x] = 1; } x = 1; flag[0] = x; if(Traverse(0, -x, n)) printf("BICOLORABLE.\n"); else printf("NOT BICOLORABLE.\n"); } return 0; }
解题思路:简单题
随便给一个点定一个值(flag数组)然后判断跟其相连的点是否已有值,如果有的话看两者是否相同,如果相同就说明不行,直接返回,否则就给他赋值或继续,注意访问的点就不用再访问了

更多内容请关注个人微信公众号 物役记 (微信号:materialchains)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?