做视频赚钱的国外网站,自主网站建站,高考评卷工作全面展开,网站无法收录way#xff1a;栈#xff0c;map#xff08;或set#xff0c;只是我想用map#xff09;记录是否访问过#xff0c;放入时记录为已访问#xff0c;打印#xff0c;邻接的没访问过先入cur#xff0c;再入邻接的节点#xff0c;放入一个邻接的节点后及时break去下一个深…way栈map或set只是我想用map记录是否访问过放入时记录为已访问打印邻接的没访问过先入cur再入邻接的节点放入一个邻接的节点后及时break去下一个深度节点。为什么要放入cur因为需要遍历到全部节点而不只是一条
#includeiostream
#includevector
#includemap
#includeset
#includestack
using namespace std;class Node;//边结构的描述
class Edge
{
public://边的起始节点Node *from;//边的终点节点Node *to;//边的权重int weight;
public:Edge(Node *from, Node *to, int weight){this-from from;this-to to;this-weight weight;}
};//点结构的描述
class Node
{
public://编号值int value;//入度int in;//出度int out;//邻接的点vectorNode* nexts;//邻接的边vectorEdge* edges;
public:Node(){}Node(int value){this-value value;in 0;out 0;}
};//图结构的描述
class Graph
{
public:mapint, Node* nodes;setEdge* edges;Graph(){}
};//利用边结构描述的图来构建图结构
//[0,7,5] [from,to,weight]
//[0,1,3] [from,to,weight]
Graph* createGraph(vectorvectorint matrix)
{Graph *graph new Graph();int m matrix.size();for(int i0; im; i){int from matrix[i][0];int to matrix[i][1];int weight matrix[i][2];//将起点结构放到图里面if(!graph-nodes.count(from)){Node *fromNode new Node(from);graph-nodes[from] fromNode;}//将终点结构放到图里面if(!graph-nodes.count(to)){Node *toNodenew Node(to);graph-nodes[to] toNode;}//将起点和终点的边结构也放到图里面(点可能已经存在过,边一定是新的)Node *fromNode graph-nodes[from];Node *toNode graph-nodes[to];Edge *newEdge new Edge(fromNode, toNode, weight);fromNode-nexts.push_back(toNode);fromNode-edges.push_back(newEdge);fromNode-out;toNode-in;graph-edges.insert(newEdge);}return graph;
}void dfs(Node *start)
{mapNode*,boolvis;stackNode* st;st.push(start);vis[start]true;coutstart-value ;while(!st.empty()){Node *cur st.top();st.pop();for(auto next: cur-nexts){if(vis.count(next)0){st.push(cur);st.push(next);vis[next]true;coutnext-value ;break;}}}coutendl;
}