UVa:193 Graph Coloring

每个点有黑白两种颜色可涂,另外注意涂黑的一个前提是周围没有黑色,这样dfs即可。

因为邻接表一个地方写错了调了好久。。

没有超时我感到非常的不解。

 

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#define MAXN 105
#define MOD 1000000007
#define INF 2139062143
#define ll long long
using namespace std;
vector<int> gl[MAXN];
int n,ans;
bool col[MAXN],res[MAXN];
void dfs(int black,int k)
{
    if(k>n)
    {
        if(black>ans)
        {
            ans=black;
            for(int i=1; i<=n; ++i)
                res[i]=col[i];
        }
    }
    else
    {
        bool ok=true;
        for(int i=0; i<gl[k].size(); ++i)
            if(col[gl[k][i]])
            {
                ok=false;
                break;
            }
        if(ok)
        {
            col[k]=true;
            dfs(black+1,k+1);
        }
        col[k]=false;
        dfs(black,k+1);
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int k;
        for(int i=0; i<=n; ++i) gl[i].clear();
        scanf("%d%d",&n,&k);
        int a,b;
        for(int i=0; i<k; ++i)
        {
            scanf("%d%d",&a,&b);
            gl[a].push_back(b);
            gl[b].push_back(a);
        }
        memset(res,0,sizeof(res));
        memset(col,0,sizeof(col));
        ans=0;
        dfs(0,1);
        bool out=false;
        printf("%d\n",ans);
        for(int i=1; i<=n; ++i)
            if(res[i])
            {
                if(!out)
                {
                    out=true;
                    printf("%d",i);
                }
                else printf(" %d",i);
            }
        printf("\n");
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值