您的位置:首页 > 其它

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

2017-08-23 19:40 489 查看

D - Bicoloring



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

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva DFS 二分图