UVa 10004 - Bicoloring

来源:互联网 发布:mac的windows截屏 编辑:程序博客网 时间:2024/05/22 22:32

传送门UVa 10004 - Bicoloring

题意是给出几个代表edge的数字,求是否可以把相邻的涂成不同的颜色。

可以用BFS,也可以用DFS。

参考了CharkJ_Z的代码,多亏了他详细的注释。原来这里还要用到一点点点有向或者无向的知识。大家如果有疑问就去看他的吧,我就不注释了。

详情见代码


#include <cstdio>#include <cstring>using namespace std;  int n;int vis[220];int in[220][220];int color[220];bool flag;  void DFS(int node);  int main(){    //freopen("input.txt", "r", stdin);    int T, i, j, a, b;    while (scanf("%d", &n) && n)    {        flag = false;        memset(vis, 0, sizeof(vis));        memset(in, 0, sizeof(in));        memset(color, 0, sizeof(color));        scanf("%d", &T);        for (i = 0; i < T; i++)        {            scanf("%d%d", &a, &b);            in[a][b] = in[b][a] = 1;        }        DFS(0);        if (!flag)            printf("BICOLORABLE.\n");        else            printf("NOT BICOLORABLE.\n");    }    return 0;}  void DFS(int node){    if (flag)        return;    for (int i = 0; i < n; i++)        if (in[node][i] && !flag)            if (!vis[i])            {                vis[i] = 1;                color[i] = !color[node];                DFS(i);            }            else            {                if (color[i] == color[node])                {                    flag = true;                    return;                }            }}



0 0
原创粉丝点击