UVa 10004 二染色

题意:给定一个无向图,是强连通的,而且无自回路。对顶点进行染色,相邻的顶点需要用不同的颜色,但总共只有两种颜色,是否可行。

思路:二部图的判定。其实通过题意思考,也可以发现,如果没有回路是可以的,如果有回路,而回路的顶点个数是偶数个也是可以的,是奇数个则不行。而这正是二部图的充要条件:无向图的所有回路长度都为偶数。  但这里如何判断回路长度不好实现。  这里的思路是,对图进行遍历,dfs或bfs都可以,在遍历的过程中进行染色,当发现冲突时即不可行。

Code:

#include<stdio.h>
#include<string.h>

bool dfs(int k,int n);

int graph[210][210];
int vis[210];
int color[210];

int main()
{
  //freopen("10004.in","r",stdin);
  //freopen("10004.out","w",stdout);
  int n,nl;
  while(scanf("%d%d",&n,&nl)==2 && n)
  {
    memset(graph,0,sizeof(graph));
    memset(vis,0,sizeof(vis));
    int i,j;
    for(int k=0;k<nl;++k)
    {
      scanf("%d%d",&i,&j);
      graph[i][j]=graph[j][i]=1;
    }
    vis[0]=1;
    color[0]=1;
    if(dfs(0,n)) printf("BICOLORABLE.\n");
    else printf("NOT BICOLORABLE.\n");
  }
  return 0;
}

bool dfs(int k,int n)
{
  for(int i=0;i<n;++i)
  {
    if(graph[k][i])
    {
      if(!vis[i])//结点k和结点i有条边,且结点i未被访问
      {
        vis[i]=1;
        color[i]=!color[k];//取相反颜色
        if(dfs(i,n)==0) return false;
      }
      else if(color[i]==color[k])//注意这个判断也应该是在 判断是否邻接的if语句里
      {
        return false;
      }
    }
  }
  return true;
}
时间: 2024-07-30 00:57:35

UVa 10004 二染色的相关文章

UVA 10004 Bicoloring

题目如下: Bicoloring  In 1976 the ``Four Color Map Theorem" was proven with the assistance of acomputer. This theorem states that every map can be colored using only fourcolors, in such a way that no region is colored using the same color as aneighbor re

uva 10004 Bicoloring(DFS)

uva 10004 Bicoloring In 1976 the ``Four Color Map Theorem" was proven with the assistance of acomputer. This theorem states that every map can be colored using only fourcolors, in such a way that no region is colored using the same color as aneighbor

NOIP2010关押罪犯 二分+二染色

这个题一上来 没有思路,后来想没有思路就二分吧 那么我们来二分 首先,大于当前的mid值的关系,不能出现在一个集合里 (即关系形成的图是一个二分图,判定二分图可以二染色) 如果不能形成二分图,那么说明有些关系要在一个集合里,那就向上二分 否则向下二分 #include<cstdio> #include<cstring> #include<queue> #include<set> #include<cstdlib> #include<algo

hdu 5285 wyh2000 and pupil(二染色)

第一次用vector解得题,值得纪念,这道题是二染色问题,我用bfs解得,就是染色,判断,计数问题,其 实挺简单的,就是得判一下特殊情况,当n<2的时候就不能有解,因为题目要求每个组至少有一个人,当没有不认识的 人的时候就是一个组是n-1,另一个组人数为1 上代码: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<vector

UVA - 10004 - Bicoloring (简单图论-着色判断)

UVA - 10004 Bicoloring Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Bicoloring  In 1976 the ``Four Color Map Theorem" was proven with the assistance of a computer. This theorem states that e

uva 10004(图)

题解:本来想着用dfs,后来写着写着就成普通的循环了,将起始点0先涂色,然后把和他相邻的其他点涂成另一种颜色,再从下一个点搜索,如果有连线但已经被涂色且和自己颜色一样就可以判断结果是错. #include <stdio.h> #include <string.h> const int N = 200 + 5; int n, l, G[N][N], vis[N], flag; void init() { memset(G, 0, sizeof(G)); memset(vis, 0,

uva 10004 Bicoloring(二染色)

这道题我始终还是看了题解,不过有进步的是我看了他的想法自己实现的,恩恩,就是要这样 ,一定得先有个正确的 想法,这样才能做对题,敲代码之前想法就错了那么一定做不对题目的,我之前想的是只要存在环就不会实现去全部染 色,其实是可以的,当这个环是奇数的时候就可以,偶数的时候不可以.所以我用的dfs每次遍历的时候遇到没有染色 的就染色,遇到染过色的就判断一下是否是一样的颜色. 贴代码: #include<stdio.h> #include<stdlib.h> #include<str

UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)

d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍历整个图,那么编程要注意了,应该从每个点出发遍历一次. s2.带权并查集来判断,略复杂.先略过.先上个博客:http://blog.csdn.net/zsc09_leaf/article/details/6727622 c.邻接矩阵,bfs #include<iostream> #include&

UVA 10004 Bicoloring(DFS染色)

题意: 给N个点构成的无环无向图,并且保证所有点对都是连通的. 给每个点染色,要么染成黑要么染成白.问是否存在染色方案使得所有有边相连的点对颜色一定不一样. 是输出 BICOLORABLE 否则输出 NOT BICOLORABLE 思路: 从某点开始,直接进行染色,如果矛盾,返回false. 代码: int n,l; vector<int> graph[205]; int color[205]; bool dfs(int u,int fa){ if(color[fa]==0) color[u]