显示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("\n"); } return 0; }
原文地址:https://www.cnblogs.com/zhang-zsq/p/12562728.html
时间: 2024-10-26 14:57:52