二分图判定 nyoj1015(模板)
来源:互联网 发布:dwg转excel软件 编辑:程序博客网 时间:2024/06/07 17:13
题目:点击打开链接nyoj1015
分析;题意很清楚,就是让判断一个图是不是二分图,思路当然就是染色法,首先给一个顶点然色,然后与它相邻的顶点全部染相反的颜色,如果过程中发现要染的点已经染色了,而且是和现在点相同的颜色的话,那么就说明不是一个二分图。
其实就是广搜模板
#include <cstdio>#include <iostream>#include <queue>#include <cstring>#include <algorithm>#include <stack>#include <vector>#include <utility>#include <cmath>using namespace std;const int N = 220;vector <int> v[N];int col[N];bool BFS(int st){ col[st]=1; queue<int> q; q.push(st); while(!q.empty()) { int k=q.front(); q.pop(); //printf("%d %d\n",k,col[k]); for(int i=0;i<v[k].size();i++) { int point=v[k][i]; int cc=col[k]==1?2:1; //printf("--%d %d %d\n",point,col[point],cc); if(col[point]==0){ col[point]=cc; q.push(point); } else if(col[point]!=cc){ return false; } } } return true;}int main(){ int n,m; while(~scanf("%d",&n) && n) { scanf("%d",&m); for(int i=0;i<m;i++) { int x,y; scanf("%d%d",&x,&y); v[x].push_back(y); v[y].push_back(x); } memset(col,0,sizeof(col)); if(BFS(0)) printf("BICOLORABLE.\n"); else printf("NOT BICOLORABLE.\n"); for(int i=0;i<=n;i++) //切记 v[i].clear(); } return 0;}
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <vector>#include <queue>#define CLR(arr,val) memset(arr,val,sizeof(arr))using namespace std;const int N=205;int color[N],n,m;bool vis[N];vector<int> map[N];bool bfs(int s){ queue<int> q; q.push(s);color[s]=1; while(!q.empty()){ int now=q.front(); q.pop(); if(!vis[now]){ int len=map[now].size(); for(int i=0;i<len;i++){ int des=map[now][i]; q.push(des); if(color[des]==-1) color[des]=color[now]==0?1:0; else { if(color[des]==color[now]) return false; else continue; } } vis[now]=true; } } return true;}int main(){ while(~scanf("%d%d",&n,&m)){ CLR(color,-1);CLR(vis,0); CLR(map,0); for(int i=1;i<=m;i++){ int a,b; scanf("%d%d",&a,&b); map[a].push_back(b); map[b].push_back(a); } bool flag=bfs(0); if(!flag){ printf("NOT BICOLORABLE.\n\n"); } else printf("BICOLORABLE.\n"); } return 0;}
0 0
- 二分图判定 nyoj1015(模板)
- 【BFS、二分图判断】二部图NYOJ1015
- NYOJ1015---二部图(判断是否是二分图:染色法)
- 二分图的判定模板(dfs,bfs)
- NYOJ1015 二部图(染色法判断二分图)
- poj3207(二分图判定)
- #1121 : 二分图一•二分图判定 (HIHOCoder +二分图的判定)
- p1403 二分+二分图判定
- 二分图判定(染色问题)
- Uva 10004(二分图的判定)
- Uva11396 爪分解(二分图判定)
- Claw Decomposition(二分图判定)
- 二分图的判定(C语言)
- 二分图判定 (挑战程序设计竞赛)
- 二分图判定
- 二分图判定
- 二分图的判定
- 二分图判定
- java.io.filenotfoundexception: the system cannot find the file specified
- C++ do while 0 的用法和意义
- pssh,pscp,pslurp使用实践
- UINavigationController详解(二)
- socket,HTTP,ASIHttpRequest的区别
- 二分图判定 nyoj1015(模板)
- 即时通讯之二次开发
- FZU1759 Super A^B mod C
- When should inner classes be used in Java Interview Question
- hdu2544最短路dij+stl 实现heap
- iOS第三方类库之-SDWebImage使用
- java中 所有事件侦听器接口(总结)
- CodeForces 2014.7.17 D. Multiplication Table
- 用集算器编写算二十四