UVA 10004 Bicoloring

Source:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=945

Description

原题:
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. 是无向图。
3. 图是强连通的。就是说至少有一条路径可走向所有节点。

Sample Input

The input consists of several test cases. Each test case starts with a line containing the number n (1

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

Sample Output

You have to decide whether the input graph can be bicolored or not, and print it as shown below.

NOT BICOLORABLE.
BICOLORABLE.
BICOLORABLE.

Caution:

题目大意其实就是说能不能只用两种颜色染遍图中的所有顶点。

我们从任意一个顶点出发,用dfs染过,分别用1和-1代表不同的颜色,不出现冲突即可。

一直WA,错在忘记每次循环应该把全局数组memset,导致同一问题两次输入的结果是不一样的……

然而我发现在下面这个random case上,debug里的结果应该是可染的,然而所有ac的代码包括我的都在这个案例上不可染,我很怀疑是不是他case output的答案给错了

15
58
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 9
0 10
0 14
1 2
1 4
1 6
1 8
1 9
1 10
1 11
1 12
1 14
2 3
2 4
2 5
2 8
2 9
2 10
2 12
3 5
3 7
3 8
3 11
3 12
4 8
4 9
4 10
4 13
5 6
5 7
5 11
5 13
6 7
6 9
6 10
6 11
6 12
6 13
6 14
7 8
7 11
7 13
7 14
8 10
8 13
8 14
9 13
10 11
12 13
12 14
13 14

示例代码:

#include
#include
#include
#include
#include 

using namespace std;

int m, n, u, v;
vector<int> g[10001];
int color[10001];

void init()
{
    memset(g, 0, sizeof(g));
    memset(color, 0, sizeof(color));
    for (int i = 0; i < n; ++i)
    {
        cin >> u >> v;
        g[u].push_back(v);
    }
}

bool dfs(int s,int c)
{
    color[s] = c;
    for (int i = 0; i < g[s].size(); ++i)
    {
        if (color[g[s][i]] == c)
            return false;
        if (color[g[s][i]] == 0 && !dfs(g[s][i], -c))
            return false;
    }
    return true;
}

void work()
{
    for (int i = 0; i < m; ++i)
    {
        if (color[i] == 0)
            if (!dfs(i, 1))
            {
                cout << "NOT BICOLORABLE." << endl;
                return;
            }
    }
    cout << "BICOLORABLE." << endl;
}

int main()
{
    while (cin >> m && m)
    {
        cin >> n;
        init();
        work();
    }
    return 0;
}               

你可能感兴趣的:(算法,编程语言,OJ)