图的深度遍历
Posted zhang-zsq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图的深度遍历相关的知识,希望对你有一定的参考价值。
显示PE
#include <iostream> #include<string.h> #include<stdio.h> using namespace std; class Graph { private: char name[100][10]; int graph[100][100]; int vertex,arc; bool visited[100]; public: Graph(int a,int b):vertex(a),arc(b) { memset(graph,0,sizeof(graph)); memset(visited,false,sizeof(visited)); set_graph(); }; Graph() {}; ~Graph() {}; void set_vertex(int a) { vertex = a; } void set_arc(int a) { arc = a; } void set_graph() { for(int i = 0; i < arc; i++) { int p,q; scanf("%d %d",&q,&p); graph[p][q] = graph[q][p] = 1;// to show that p->q and q->p; } } void set_name() { for(int i = 0; i < vertex; i++) { scanf("%s",name[i]); } } void DFS(int v) { if(visited[v] == false) { visited[v] = true; printf("%d ",v); } for(int i = 0; i < vertex; i++) { if(visited[i]==false && graph[v][i] == 1) { DFS(i); } } } }; int main() { ios::sync_with_stdio(false); int n; scanf("%d",&n); while(n--) { int k,m;// k is the number of the arc, m is the number of vertex; scanf("%d %d",&k,&m); Graph G(k,m); G.DFS(0); printf(" "); } return 0; }
以上是关于图的深度遍历的主要内容,如果未能解决你的问题,请参考以下文章