阅读背景:

图的遍历(DFS,BFS)(邻接表)_weixin_30809333的博客

来源:互联网 
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;

const int Max_n=1001;
vector<int>G[Max_n];
int use[Max_n];

void dfs(int v){
	printf("%d ",v);//输出当前遍历的结点 
	use[v]=1;//标记 
	while(!G[v].empty()){//该结点有邻接点 
		int v1=G[v].back();//从邻接表中取出 
		G[v].pop_back();// 删除 
		if(!use[v1])//取出的结点是否使用过 
			dfs(v1);//未使用过继续向下递归 
	}
}
int main(){
	int v,e;
	scanf("%d%d",&v,&e);
	memset(use,0,sizeof(use));
	for(int i=1;i<=e;i++){
		int from,to;
		scanf("%d%d",&from,&to);
		G[from].push_back(to);//建立邻接表 
		G[to].push_back(from);  
	} 
	dfs(1);
	return 0;
} #include<cstdio>
#include<iostream>
#includ



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: