UVa 10004 - Bicoloring

19 篇文章 0 订阅

传送门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;
                }
            }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值