CSU-ACM2017暑假集训比赛7 - D - Bicoloring - UVA - 10004

D - Bicoloring

Problem description

一边深度优先搜索,一边染色,一边检查是否有相邻节点染上了相同颜色即可。也可以理解为染色法判断二分图是否成立。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 204;

int N, L, cnt;
vector<int> g[maxn];
struct node{
   int color;
   bool vis;
}point[maxn];

void init(){
   for(int i = 0; i <= N; i++)
       g[i].clear();
   memset(point, 0, sizeof(point));
}

bool dfs(int u){
   for(int i = 0; i < g[u].size(); i++){
       int to = g[u][i];
       if(!point[to].vis){
           point[to].vis = true;
           point[to].color = -point[u].color;
           return dfs(to);
       }
       else{
           if(point[to].color + point[u].color == 0)
               continue;
           else
               return false;
       }
   }
   return true;
}

int main(){
#ifdef TEST
freopen("test.txt", "r", stdin);
#endif // TEST

   while(scanf("%d", &N) && N){
       init();
       scanf("%d", &L);
       for(int i = 0; i < L; i++){
           int a, b;
           scanf("%d%d", &a, &b);
           g[a].push_back(b);
       }
       point[0].color = point[0].vis = 1;
       bool ok = dfs(0);
       if(ok)
           printf("BICOLORABLE.\n");
       else
           printf("NOT BICOLORABLE.\n");
   }

   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值