首页 > 其他 > 详细

SRM 608 D2 L3:BigOEasy,DFS

时间:2014-02-09 15:56:06      阅读:378      评论:0      收藏:0      [点我收藏+]

题目:http://community.topcoder.com/stat?c=problem_statement&pm=13002


看似复杂,其实只要判断图中是否有两个不同的环有至少一个相同的顶点。从不同的顶点出发进行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>
#include <ctime>

using namespace std;


#define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)

/*************** Program Begin **********************/
bool visit[55];
int ct;
int cur;

class BigOEasy {
public:
	vector <string> graph;
	int N;
	void DFS(int v)
	{
		visit[v] = true;
		for (int i = 0; i < N; i++) {
			if (graph[v][i] == ‘Y‘) {
				if (visit[i]) {
					if (i == cur) {
						++ct;
					}
				} else {
					DFS(i);
				}
			}
		}
	}

    string isBounded(vector <string> graph) {
	this->graph = graph;
	this->N = graph.size();

	string res = "Bounded";
	memset(visit, 0, sizeof(visit));
	for (int i = 0; i < N; i++) {
		memset(visit, 0, sizeof(visit));
		ct = 0;
		cur = i;
		DFS(i);
		if (ct > 1) {
			res = "Unbounded";
			break;
		}
	}
        return res;
    }
};


/************** Program End ************************/


SRM 608 D2 L3:BigOEasy,DFS

原文:http://blog.csdn.net/xzz_hust/article/details/18988847

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!