\(\color{#0066ff}{题目描述}\)

多组数据,n=0结束,每次一个n,m,之后是边,问你是不是二分图

\(\color{#0066ff}{输入样例}\)

3
3
0 1
1 2
2 0
3
2
0 1
1 2
9
8
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0

\(\color{#0066ff}{输出样例}\)

NOT BICOLORABLE.
BICOLORABLE.
BICOLORABLE.

\(\color{#0066ff}{题解}\)

二分图染色法

看能否用两种颜色染色,使相邻两点颜色不同

#include<cstdio>
#include<queue>
#include<vector>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#define _ 0
#define LL long long
#define Space putchar(' ')
#define Enter putchar('\n')
#define fuu(x,y,z) for(int x=(y),x##end=z;x<=x##end;x++)
#define fu(x,y,z) for(int x=(y),x##end=z;x<x##end;x++)
#define fdd(x,y,z) for(int x=(y),x##end=z;x>=x##end;x--)
#define fd(x,y,z) for(int x=(y),x##end=z;x>x##end;x--)
#define mem(x,y) memset(x,y,sizeof(x))
#ifndef olinr
inline char getc()
{
static char buf[100001],*p1=buf,*p2=buf;
return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100001,stdin),p1==p2)? EOF:*p1++;
}
#else
#define getc() getchar()
#endif
template<typename T>inline void in(T &x)
{
int f=1; char ch; x=0;
while(!isdigit(ch=getc()))(ch=='-')&&(f=-f);
while(isdigit(ch)) x=x*10+(ch^48),ch=getc();
x*=f;
}
int T;
struct node
{
int to;
node *nxt;
};
typedef node* nod;
nod s[105050];
nod head[555];
int col[555];
int n,m,top;
inline void add(int from,int to)
{
static node st[105050],*tail=st;
nod t=top? s[top--]:tail++;
t->to=to;
t->nxt=head[from];
head[from]=t;
}
inline bool dfs(int x,int c)
{
col[x]=c;
for(nod i=head[x];i;i=i->nxt)
{
if(~col[i->to])
{
if(col[i->to]!=(c^1)) return false;
}
else return dfs(i->to,c^1);
}
return true;
}
int main()
{
while(1)
{
in(n);
if(!n) break;
in(m);
fu(i,0,n) head[i]=NULL,col[i]=-1;
int x,y;
fuu(i,1,m) in(x),in(y),add(x,y),add(y,x);
printf(dfs(1,0)? "BICOLORABLE.":"NOT BICOLORABLE."),Enter;
fu(i,0,n) for(nod j=head[i];j;j=j->nxt) s[++top]=j;
}
return ~~(0^_^0);
}

Bicoloring UVA - 10004 二分图判断的更多相关文章

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

    d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍 ...

  2. UVA 10004 Bicoloring

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&pa ...

  3. UVa 10004:Bicoloring

    这道题要我们判断所给图是否可以用两种颜色进行染色,即"二染色“.已知所给图一定是强连通图. 分析之: 若图中无回路,则该图是一棵树,一定可以二染色. 若图中有回路,但回路有偶数个节点,仍然可 ...

  4. uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)

    Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...

  5. UVA 10004 Bicoloring(DFS染色)

    题意: 给N个点构成的无环无向图,并且保证所有点对都是连通的. 给每个点染色,要么染成黑要么染成白.问是否存在染色方案使得所有有边相连的点对颜色一定不一样. 是输出 BICOLORABLE 否则输出 ...

  6. hdu 4751 2013南京赛区网络赛 二分图判断 **

    和以前做过的一个二分图颇为相似,以前的是互相不认识的放在一组,这个是互相认识的,本质上是相同的 是 hdu 2444 #include<cstdio> #include<iostre ...

  7. hdu 2444 二分图判断与最大匹配

    题意:有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出No ...

  8. hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  9. UVA1627-Team them up!(二分图判断+动态规划)

    Problem UVA1627-Team them up! Total Submissions:1228  Solved:139 Time Limit: 3000 mSec Problem Descr ...

随机推荐

  1. sql server将字符串转换为 uniqueidentifier 时失败

    sql server查询中出现 将字符串转换为 uniqueidentifier 时失败异常 原因为id设置为uniqueidentifier 字段,在where查询时需要做转换cast(id as ...

  2. Oracle OCM提纲

    ocm提纲 数据库创建详解 ◆ 通过手动方式创建数据库 环境变量的设置 密码文件的创建过程以及使用情景 Oracle数据库中参数文件的演进过程 参数文件的对比 参数的修改方式介绍 数据库启动过程时的内 ...

  3. CRC 简介

    CRC wiki,历史发展,各个版本的用途 等 https://en.wikipedia.org/wiki/Cyclic_redundancy_check (apple)crc32.c /* * Th ...

  4. WINDOWS 7下的记事本程序目录

    这是win7的目录    系统所在分区:\Windows\system32\notepad.exe

  5. javascipt——对象的概念——数组

    一.Array 特点: 数组的长度是可变的: 数组的索引可以是数字.字符串: 数组的内容可以是任意内容: 可以通过索引获取之前不存在的一个位置,其值为undefined: 1.构造函数: new Ar ...

  6. FFmpeg for Android compiled with x264, libass, fontconfig, freetype and fribidi

    android下打算使用ffmpeg的 drawtext ,不过需要 --enable-libfreetype  但是freetype是个第三方库,所以需要先编译freetype,然后再编译ffmpe ...

  7. [Python Study Notes]物体运动检测

    基于opencv的cv2模块实现 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  8. ArcGIS Field Type /esriFieldTypeDate(转)

    ArcGIS Field Type   The following table outlines the equivalent field data types in ArcCatalog, ArcO ...

  9. powerdesigner自动将name填充到注释的脚本

    我在建模的时候,希望在生成脚本的时候有注释,所以才会看到Comment列,实际上,只要你的表中的Name列不为空,运行下面的VBScript,PD会帮你自动填充注释的Comment列值. '把pd中那 ...

  10. Opengl使用模型视图变换移动光源

    光源绕一个物体旋转,按下鼠标左键时,光源位置旋转. #include <GL/glut.h> static int spin = 0;static GLdouble x_1 = 0.0;s ...