\(\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. idea debug的时候 启动起来超级慢

  2. [置顶] 从零制作文件系统到JZ2440,使其支持telnet , ftp 和tftp

    转自:http://mp.weixin.qq.com/s?__biz=MzAxNTAyOTczMw==&mid=2649328515&idx=1&sn=5849fba4b44e ...

  3. .Net Memory Profiler入门

    简介:http://www.cnblogs.com/wmlunge/archive/2013/01/08/2850809.html 实践: http://www.cnblogs.com/eaglet/ ...

  4. Python代码规范总结

    1.缩进问题: Tip:用4个空格来缩进代码 不要用Tab键或者是Tab和空格混用, vim用户可以将tab键设置为4个空格的长度.要么选择垂直对齐换行的元素, 或者是使用4空格悬挂式缩进(第一行没有 ...

  5. The connection to adb is down and a sever error has occured的解决

    1. 打开任务管理器,关掉豌豆夹等手机助手 2. 打开命令行,切换到adb所在目录,如:C:\Users\Jubincn\Downloads\adt-bundle-windows-x86_64-201 ...

  6. CSS——display

    display的主要属性: 1.none: 不显示: 不保留其在页面中的位置,即相当于html文档中没有这个元素: visible:hidden虽然也不显示:但依然保存着他的位置和大小. 2.bloc ...

  7. Oracle——基础知识(一)

       一.Oracle中的数据类型 1.字符串类型.如:char.nchar.varchar2.nvarchar2.2.数值类型.如:int.number(p,s).integer.smallint. ...

  8. [Python Study Notes]七彩散点图绘制

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  9. MyBatis02 MyBatis基础知识之Mapper映射器

    1 Mapper映射器是什么 是符合映射文件要求的接口 接口要求 a. 方法名要与sql的id一致. b. 方法的参数类型要与parameterType一致. c. 方法的返回类型要与resultTy ...

  10. c# 一维数组,二维数组,多维数组。

    数组就是给一个变量定义多个字符,可以是string也可以是int.或者说是一组变量. 可以更加方便的操作大量数据. 数组的定义1.数组里面的内容必须是同一类型2.数据必须有长度限制 一维数组 *一.数 ...