Uva 10004(二分图的判定)
来源:互联网 发布:iphone桌面软件管理 编辑:程序博客网 时间:2024/06/05 14:55
这题其实很简单的说,第一次用邻接表来存图= =
首先图的储存结构是结构体+head数组。。。其实head数组保存的
struct node{ int v; int next;}V[200*200];
假设现在有u节点,v表示的是和他邻接的点,next保存的是v对应的编号,head数组保存的是链表的头节点编号。
比如有
1 2
1 3
1 4的点边关系那么V[head[1]].v=4;V[head[2]].v=1,V[head[3]].v=1;
现在讲讲如何判断图是不是二分图,首先对任意没染色的点进行染色,之后判断与其相邻的点若是没染色则染上与其相邻的顶点不同的颜色,若是染过色且与相邻的顶点颜色相同则不是二分图==
判断的过程用dfs来实现
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;struct node{ int v; int next;}V[200*200];int tol; int head[200];int col[200];void init(){ int tol=0; memset(head,-1,sizeof(head));}void add(int u,int v){ V[tol].v=v; V[tol].next=head[u]; head[u]=tol++;}bool bldfs(int u,int co){ int i,v; col[u]=co; for(i=head[u];i!=-1;i=V[i].next) { v=V[i].v; if(col[v]==co) return false; if(col[v]==-1&&!bldfs(v,co^1)) return false; } return true;}int main(){ int n,m; while(scanf("%d",&n)!=EOF&&n) { init(); int u,v; scanf("%d",&m); for(int i=0;i<m;i++) { scanf("%d %d",&u,&v); add(u,v); add(v,u); } memset(col,-1,sizeof(col)); int flag=0; for(int i=0;i<n;i++) { if(col[i]==-1) if(!bldfs(i,0)) { flag=1; break; } } if(flag==1) printf("NOT BICOLORABLE.\n"); else printf("BICOLORABLE.\n"); } return 0;}
0 0
- Uva 10004(二分图的判定)
- UVa 10004 Bicoloring(二分图判定+DFS)
- uva 11396 二分图判定
- UVa - 11396 Claw Decomposition 二分图的判定
- 二分图的判定
- 二分图的判定
- 二分图的判定
- 二分图的判定
- 二分图的判定
- uva 11396 - Claw Decomposition(二分图判定)
- UVA 11396 - Claw Decomposition(二分图判定)
- uva 11396 Claw Decomposition(二分图判定 BFS)
- uva 11396 - Claw Decomposition(二分图判定)
- uva 11396 Claw Decomposition 二分图判定
- uva 11396Claw Decomposotion(二分图判定)
- UVA 11396:Claw Decomposition(二分图判定)
- UVA 1627 【二分图判定+背包问题】
- #1121 : 二分图一•二分图判定 (HIHOCoder +二分图的判定)
- B. Bear and Three Musketeers
- Uva657 - The die is cast
- Uva532(三维搜索)
- Uva 705 - Slash Maze
- Uva 10557 XYZZY
- Uva 10004(二分图的判定)
- 右值与左值解析
- Uva(10129)+Uva(10054)
- 二叉排序树的建立
- zoj(1221
- Uva(10305)
- Uva(10034)
- HDU(1087)继续畅通工程
- 10369 - Arctic Network