Bicoloring - UVa 10004 dfs
2014-10-25 19:31
375 查看
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 thesame 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.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.
题意:给你一些相邻的点,问是否存在只用两个颜色的方式使得相邻的点都有不同的颜色。
思路:第一个点随便涂,然后与其相邻的都变成另一种颜色,如果出现已经涂上颜色,并且不符合要求的情况,那就是Not Bicoloring。
AC代码如下:
#include<cstdio> #include<cstring> #include<vector> using namespace std; int n,m,col[210]; vector<int> vc[210]; bool flag; void dfs(int u) { int i,j,k,v,a,b,len=vc[u].size(); if(col[u]==1) { a=1;b=2; } else { a=2;b=1; } for(i=0;i<len;i++) { v=vc[u][i]; if(col[v]==0) { col[v]=b; dfs(v); } else if(col[v]!=b) flag=false; } } int main() { int i,j,k,u,v; while(~scanf("%d",&n) && n>0) { scanf("%d",&m); for(i=1;i<=n;i++) vc[i].clear(); for(i=1;i<=m;i++) { scanf("%d%d",&u,&v); u++;v++; vc[u].push_back(v); vc[v].push_back(u); } memset(col,0,sizeof(col)); col[1]=1; flag=true; dfs(1); if(flag) printf("BICOLORABLE.\n"); else printf("NOT BICOLORABLE.\n"); } }
相关文章推荐
- UVA 10004 Bicoloring(DFS染色)
- UVA - 10004 Bicoloring (dfs和bfs两种解法)
- UVa 10004 Bicoloring (DFS&二分图)
- UVA 10004 Bicoloring【DFS简单二部图判定】
- uva10004 Bicoloring 黑白染色问题,DFS
- UVA 10004 - Bicoloring(DFS)
- uva 10004 Bicoloring(DFS)
- uva10004 Bicoloring 黑白染色问题,DFS
- UVa10004 Bicoloring (dfs水题)
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
- UVa 10004 Bicoloring(二分图判定+DFS)
- uva 10004 - Bicoloring
- uva 10004 - Bicoloring
- UVA 10004 Bicoloring
- UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)
- UVA10004- Bicoloring(二染色)
- uva 10004 - Bicoloring
- UVa 10004 - Bicoloring
- UVA 10004 Bicoloring
- UVa 10004 - Bicoloring