UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)
d.给定一个图,判断是不是二分图。
s.可以交叉染色,就是二分图;否则,不是。
另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了。
如果不能保证从一个点出发可以遍历整个图,那么编程要注意了,应该从每个点出发遍历一次。
s2.带权并查集来判断,略复杂。先略过。先上个博客:http://blog.csdn.net/zsc09_leaf/article/details/6727622
c.邻接矩阵,bfs
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std; #define MAXN 205 int map[MAXN][MAXN];
int color[MAXN];
int n; bool bfs(int start){//bfs交叉染色,两种颜色标记为 1 和 -1,未染色标记为 0
queue<int>q;
color[start]=;
q.push(start); while(!q.empty()){
int temp=q.front();
q.pop(); for(int i=;i<n;++i){
if(map[temp][i]){
if(color[i]==){//未染色
if(color[temp]==){
color[i]=-;
}
else{
color[i]=;
}
q.push(i);
}
else{//已染色
if(color[i]==color[temp]){//相邻的两点颜色相同
return false;//不能交叉染色
}
}
}
}
}
return true;
} int main(){ int L;
int u,v; while(~scanf("%d",&n)){
if(n==)break; memset(map,,sizeof(map));
memset(color,,sizeof(color)); scanf("%d",&L); for(int i=;i<L;++i){
scanf("%d%d",&u,&v); map[u][v]=;
map[v][u]=;
} if(bfs()){
printf("BICOLORABLE.\n");
}
else{
printf("NOT BICOLORABLE.\n");
}
} return ;
}
c2.邻接矩阵,dfs。这个图是强连通的,所以一次dfs可以遍历所有节点了。如果不是强连通的,则需要遍历多次。
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std; #define MAXN 205 int map[MAXN][MAXN];
int color[MAXN];
int n; bool dfs(int u){//dfs交叉染色,两种颜色标记为 1 和 -1,未染色标记为 0
for(int i=;i<n;++i){
if(map[u][i]){
if(color[i]==){//未染色
if(color[u]==){
color[i]=-;
}
else{
color[i]=;
} if(!dfs(i)){//不能交叉染色
return false;
}
}
else{//已染色
if(color[i]==color[u]){//不能交叉染色
return false;
}
}
}
}
return true;
} int main(){ int L;
int u,v; while(~scanf("%d",&n)){
if(n==)break; memset(map,,sizeof(map));
memset(color,,sizeof(color)); scanf("%d",&L); for(int i=;i<L;++i){
scanf("%d%d",&u,&v); map[u][v]=;
map[v][u]=;
} color[]=;
if(dfs()){//这个图是强连通的,所以一次dfs可以遍历所有节点了。如果不是强连通的,则需要遍历多次。
printf("BICOLORABLE.\n");
}
else{
printf("NOT BICOLORABLE.\n");
}
} return ;
}
c3.邻接表,bfs
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std; #define MAXN 205//点数
#define MAXM 10000//边数 int color[MAXN]; struct Edge{
int to,next;
}edge[MAXM]; int head[MAXN];
int tot; void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void init(){
tot=;
memset(head,-,sizeof(head));
} bool bfs(int start){//bfs交叉染色,两种颜色标记为 1 和 -1,未染色标记为 0
int v;
queue<int>q;
color[start]=;
q.push(start); while(!q.empty()){
int temp=q.front();
q.pop(); for(int i=head[temp];i!=-;i=edge[i].next){// v=edge[i].to; if(color[v]==){//未染色
if(color[temp]==){
color[v]=-;
}
else{
color[v]=;
}
q.push(v);
}
else{//已染色
if(color[v]==color[temp]){//相邻的两点颜色相同
return false;//不能交叉染色
}
} }
}
return true;
} int main(){ int n,L;
int u,v; while(~scanf("%d",&n)){
if(n==)break; memset(color,,sizeof(color));
init(); scanf("%d",&L); for(int i=;i<L;++i){
scanf("%d%d",&u,&v); addedge(u,v);
addedge(v,u);
} if(bfs()){
printf("BICOLORABLE.\n");
}
else{
printf("NOT BICOLORABLE.\n");
}
} return ;
}
c4.邻接表,dfs
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std; #define MAXN 205//点数
#define MAXM 10000//边数 int color[MAXN]; struct Edge{
int to,next;
}edge[MAXM]; int head[MAXN];
int tot; void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void init(){
tot=;
memset(head,-,sizeof(head));
} bool dfs(int u){//dfs交叉染色,两种颜色标记为 1 和 -1,未染色标记为 0 int v; for(int i=head[u];i!=-;i=edge[i].next){ v=edge[i].to; if(color[v]==){//未染色
if(color[u]==){
color[v]=-;
}
else{
color[v]=;
} if(!dfs(v)){//不能交叉染色
return false;
}
}
else{//已染色
if(color[v]==color[u]){//不能交叉染色
return false;
}
} }
return true;
} int main(){ int n,L;
int u,v; while(~scanf("%d",&n)){
if(n==)break; memset(color,,sizeof(color));
init(); scanf("%d",&L); for(int i=;i<L;++i){
scanf("%d%d",&u,&v); addedge(u,v);
addedge(v,u);
} color[]=;
if(dfs()){
printf("BICOLORABLE.\n");
}
else{
printf("NOT BICOLORABLE.\n");
}
} return ;
}
UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)的更多相关文章
- POJ 1703 Find them, Catch them【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】
The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the ...
- hdu 1829 &poj 2492 A Bug's Life(推断二分图、带权并查集)
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- CodeForces - 687D: Dividing Kingdom II (二分图&带权并查集)
Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great ...
- UVA 12232 - Exclusive-OR(带权并查集)
UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...
- BZOJ4025 二分图 分治 并查集 二分图 带权并查集按秩合并
原文链接http://www.cnblogs.com/zhouzhendong/p/8683831.html 题目传送门 - BZOJ4025 题意 有$n$个点,有$m$条边.有$T$个时间段.其中 ...
- BZOJ 2303 方格染色(带权并查集)
要使得每个2*2的矩形有奇数个红色,如果我们把红色记为1,蓝色记为0,那么我们得到了这2*2的矩形里的数字异或和为1. 对于每个方格则有a(i,j)^a(i-1,j)^a(i,j-1)^a(i-1,j ...
- hdu3038判断区间谎言(带权并查集)
题目传送门 题目描述:给你n,m,n代表从1到n这么大的数组,m组v,u,val,代表v到u这个区间的总和是val,然后让你判断m组关系中有几组是错误的. 思路:带权并查集,这道题其实算是让我知道什么 ...
- CodeForces - 688C:NP-Hard Problem (二分图&带权并查集)
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex c ...
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
随机推荐
- C#.net的常用函数列表
原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] 1、DateTime 数字型 System.DateTime currentTime=new System.Dat ...
- HDU 4803 Poor Warehouse Keeper (贪心+避开精度)
555555,能避开精度还是避开精度吧,,,,我们是弱菜.. Poor Warehouse Keeper Time Limit: 2000/1000 MS (Java/Others) Memor ...
- msp430项目编程53
msp430综合项目---扩展项目三53 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结
- Mac 快速修改 hosts 文件
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts
- Google解决跨域
1.添加 --disable-web-security --user-data-dir=D:\tmp 2.在D的根目录新建tmp文件夹
- Java中ArrayList类
ArratList 类:存放同一数据类型容器(只能为引用数据类型,因实际其内部存放的是地址) 1.导入其所在包 import java.util.ArratList 2.创建对象 ArrayList& ...
- mysql批量删除相同前缀的表格
原文:http://www.open-open.com/code/view/1446691883076 如果你网站后台没法运行mysql,就进phpmyadmin,然后运行一段代码.假如要删除织梦cm ...
- [转]Visual Studio 2012 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案
原文地址:http://www.cnblogs.com/gb2013/archive/2013/03/05/SecurityEnhancementsInTheCRT.html 在VS 2012 中编译 ...
- PAT 1094. The Largest Generation(BFS)
CODE: #include<cstdio> #include<cstring> #include<queue> using namespace std; bool ...
- 诊断并解决 ORA-4030 错误 (Doc ID 1548826.1)
适用于: Oracle Database - Enterprise Edition - 版本号 8.1.7.4 和更高版本号 本文档所含信息适用于全部平台 用途 怎样诊断 ORA-4030 错误 排错 ...