Bicoloring UVA - 10004 二分图判断
\(\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 二分图判断的更多相关文章
- UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)
d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍 ...
- UVA 10004 Bicoloring
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&pa ...
- UVa 10004:Bicoloring
这道题要我们判断所给图是否可以用两种颜色进行染色,即"二染色“.已知所给图一定是强连通图. 分析之: 若图中无回路,则该图是一棵树,一定可以二染色. 若图中有回路,但回路有偶数个节点,仍然可 ...
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- UVA 10004 Bicoloring(DFS染色)
题意: 给N个点构成的无环无向图,并且保证所有点对都是连通的. 给每个点染色,要么染成黑要么染成白.问是否存在染色方案使得所有有边相连的点对颜色一定不一样. 是输出 BICOLORABLE 否则输出 ...
- hdu 4751 2013南京赛区网络赛 二分图判断 **
和以前做过的一个二分图颇为相似,以前的是互相不认识的放在一组,这个是互相认识的,本质上是相同的 是 hdu 2444 #include<cstdio> #include<iostre ...
- hdu 2444 二分图判断与最大匹配
题意:有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出No ...
- hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS Me ...
- UVA1627-Team them up!(二分图判断+动态规划)
Problem UVA1627-Team them up! Total Submissions:1228 Solved:139 Time Limit: 3000 mSec Problem Descr ...
随机推荐
- Scala的Json序列化
import java.util.TimeZone import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMappe ...
- appium_python 实现手势密码
直接上代码吧: from appium.webdriver.common.touch_action import TouchAction from driver import AppiumTest # ...
- C语言生成程序问题
问题: 我用VS2013写好C语言程序调试运行后就在debug文件夹下生成了EXE文件,可以在本机运行.但是这个EXE文件在别的没装过VS2013的电脑上就不能直接运行,说丢失MSVCR120D.dl ...
- NoSQL概述
- Tomcat服务器简介
- JAVA基础知识总结6(面向对象特征之一:多态)
多 态:函数本身就具备多态性,某一种事物有不同的具体的体现. 体现:父类引用或者接口的引用指向了自己的子类对象. Animal a = new Cat(); 多态的好处:提高了程序的扩展性. 多态的弊 ...
- android中finish和system.exit方法退出的区别
finish只是将此activity推向后台,并没有释放资源. 而system.exit则是杀死进程,会释放资源
- ROS Learning-026 (提高篇-004 A Mobile Base-02) 控制移动平台 --- “分封制”
ROS 提高篇 之 A Mobile Base-02 - 控制移动平台 - "分封制" 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ub ...
- ROS Learning-013 beginner_Tutorials (编程) 编写ROS服务版的Hello World程序(Python版)
ROS Indigo beginner_Tutorials-12 编写ROS服务版的Hello World程序(Python版) 我使用的虚拟机软件:VMware Workstation 11 使用的 ...
- java中字符串的存储
在java中,不同的字符串赋值方法,其所在的地址可能不同也就导致,两个字符串的值看似相等可是在s1==s2操作时,其结果返回的却是false 例: String s1 = "Programm ...