그림 에 있 는 DFS 를 옮 겨 다 니 며 고리 가 있 는 지 판단 합 니 다 (알고리즘 서론)

코드 와 10 월 22 일 에 수정 되 었 습 니 다. 감사합니다. lbhqfwj 제출:
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int MAX = 1000;
int color[MAX];
int Time;
bool is_DAG;   //       ,    
int first[MAX],last[MAX];   //          ,        
#define CLR(arr,val) memset(arr,val,sizeof(arr))
typedef struct
{
	char v[MAX];
	vector map[MAX];
	int vNum;  //     
	int eNum;  //     
}graph;
//      color[u]
//   0   ,           
//   -1   ,              
//      1   ,       (            )
void Create(graph * g);  //    
void DFS(graph *g);//       g
void dfs(graph *g,int curPoint);//   i              
void Create(graph *g)
{
	for(int i = 0;i < g->eNum;i++)    
	{
		int u,v;                  //      0,    1
		cin>>u>>v;
		g->map[u].push_back(v);
	}
}
void dfs(graph *g,int curPoint )
{
	cout << "       : " << curPoint<map[curPoint].size();i++) //     curPoint    
	{
		int now = g->map[curPoint][i];
		if(color[now] == 0)    //       
			dfs(g,now);
		else if(color[now] == -1)
			is_DAG = false;   //        ,        
	}
	color[curPoint] = 1;     //     ,    curPoint         
	cout << "  " << curPoint << "    " <eNum;i++)
	{
		color[i] = 0;
		first[i] = 0;
		last[i] = 0;
	}
	is_DAG = true;
	Time = 0;
}
void DFS(graph *g)   //       
{
	Init(g);   //       
	for(int i = 0;i < g->vNum; i++)
	{
		if(color[i] == 0)   //     ,       
			dfs(g,i);
	}
}
int main()
{
	graph *g = new graph;
	g->vNum = 5;
	g->eNum = 3;   //         
	Create(g);    //   
	DFS(g);       //   
	if(is_DAG)
		cout << "     " <

좋은 웹페이지 즐겨찾기