SRM 593 Div1 L1:HexagonalBoard,用染色法判断无向图是否为二分图
来源:互联网 发布:ai mac版 编辑:程序博客网 时间:2024/04/30 16:22
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12784
最近由于考研,一个多月没有做TopCoder了,第一次参加Div1,结果第一题都没搞出来,看了社论之后学到了用DFS染色法判断无向图是否是二分图的方法。深刻感觉本人太水了,要加油!
代码如下:
#include <algorithm>#include <iostream>#include <sstream>#include <string>#include <vector>#include <stack>#include <deque>#include <queue>#include <set>#include <map>#include <cstdio>#include <cstdlib>#include <cctype>#include <cmath>#include <cstring>using namespace std;/*************** Program Begin **********************/int dx[] = {0, -1, -1, 0, 1, 1}, dy[] = {-1, 0, 1, 1, 0, -1};class HexagonalBoard {private:vector <string> board;int color[50][50];int result, N;public:void DFS(int x, int y, int c){int nx, ny;if ('X' == board[x][y] && -1 == color[x][y]) {color[x][y] = c;result = max(result, 1);for (int i = 0; i < 6; i++) {nx = x + dx[i];ny = y + dy[i];if (nx < 0 || nx > N-1 || ny < 0 || ny > N-1) {continue;}if ('X' == board[nx][ny]) {result = max(result, 2);DFS(nx, ny, !c);if (c == color[nx][ny]) {result = 3;}}}}}int minColors(vector <string> board){this->board = board;memset(color, -1, sizeof(color));result = 0;N = board.size();for (int i = 0; i < N; i++) {for (int j = 0; j < N; j++) {DFS(i, j, 0);}}return result;}};/************** Program End ************************/
- SRM 593 Div1 L1:HexagonalBoard,用染色法判断无向图是否为二分图
- 判断是否为二分图 染色法
- 判断一个无向图是否为二分图
- [构造 二分图] SRM 693 div1 BipartiteConstruction
- 数据结构判断无向图是否为连通
- 3478 Catch (判断是否为二分图,dfs奇偶染色)
- hdu 2444 The Accomodation of Students 染色判断是否为二分图
- poj 2942 Knights of the Round Table 【无向图求BCC + 黑白染色判断二分图】
- 无向图dfs判断是否有环和是否二分图
- 判断是否为二分图
- 判断是否为二分图
- 交叉染色法判断二分图
- hdu 5285(染色法判断二分图)
- 判断二分图——染色法
- hdu 4751 染色法判断二分图
- 判断二分图 染色法 NYOJ 1015
- [数学 二分图匹配] SRM 456 div1 FunctionalEquation
- 判断无向图是否有回路
- Apache按天截断日志工具,无法生成访问日志
- JS通过事件的传递来间接调用定义在函数中的函数
- 工厂模式,简单工厂模式,抽象工厂模式三者有什么区别
- cocos2d 坐标系
- Oracle Data Integrator 介绍
- SRM 593 Div1 L1:HexagonalBoard,用染色法判断无向图是否为二分图
- Red hat AS oracle11g 手工创建OEM
- 软件架构师需要掌握哪些知识?
- 最长字符串匹配算法(KMP算法)
- UVA - 10558 A Brief Gerrymander
- 鼠标放上去显示全部文字,不放上去,显示部分文字jquery效果,包括隐藏部分文字
- 怎样找到sql报错的存储过程和触发器及语句
- 设计jquery插件
- STL vector常用的几个成员的用法