UVA - 10004
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
Bicoloring |
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:
An input with n = 0 will mark the end of the input and is not to be processed.
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.
Source
思路:DFS,每次使一个点着色(这里抽象颜色为1或2),每次要着色这个点之前加个判断,看是否现在满不满足与之相邻的点是否是同一种颜色
AC代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #define LL long long #define INF 0x7fffffff using namespace std; const int maxn = 205; int color[maxn]; int map[maxn][maxn]; int n, l; int judge(int x) { int f1 = 0, f2 = 0; for(int i = 0; i < n; i++) { if(map[x][i]) { if(color[i] == 1) f1 = 1; else if(color[i] == 2) f2 = 1; } } if(f1 && f2) return 1; return 0; } int dfs(int x) { for(int i = 0; i < n; i++) { if(map[x][i] && !color[i]) { if(judge(i)) return 0; if(color[x] == 1) color[i] = 2; else if(color[x] == 2) color[i] = 1; dfs(i); } } return 1; } int main() { while(scanf("%d", &n), n) { scanf("%d", &l); memset(map, 0, sizeof(map)); for(int i = 0; i < l; i++) { int u, v; scanf("%d %d", &u, &v); map[u][v] = 1; map[v][u] = 1; } /*for(int i = 0; i< n; i++, printf("\n")) { for(int j = 0; j < n; j++) { printf("%d ", map[i][j]); } }*/ memset(color, 0, sizeof(color)); color[0] = 1; if(dfs(0)) { //for(int i = 0; i < n; i++) printf("%d ", color[i]); printf("BICOLORABLE.\n"); } else { //for(int i = 0; i < n; i++) printf("%d ", color[i]); printf("NOT BICOLORABLE.\n"); } } return 0; }
UVA - 10004 - Bicoloring (简单图论-着色判断)
原文地址:http://blog.csdn.net/u014355480/article/details/45116017