网站建设各个模块的功能,中国企业网是国企吗,wordpress主题安装怎么更换内容,网站建设云浪科技当一道题的AC变成了找不同的时候#xff0c;一切就开始失去意义。 到底是谁#xff1f;把Search写成Seacrh#xff0c;害我一直找不同。
本实验实现邻接表表示下无向图的广度优先遍历。
程序的输入是图的顶点序列和边序列(顶点序列以*为结束标志#xff0c;边序列以-1,-1… 当一道题的AC变成了找不同的时候一切就开始失去意义。 到底是谁把Search写成Seacrh害我一直找不同。
本实验实现邻接表表示下无向图的广度优先遍历。
程序的输入是图的顶点序列和边序列(顶点序列以*为结束标志边序列以-1,-1为结束标志)。程序的输出为图的邻接表和广度优先遍历序列。例如 程序输入为 a b c d e f * 0,1 0,4 1,4 1,5 2,3 2,5 3,5 -1,-1 程序的输出为 the ALGraph is a 4 1 b 5 4 0 c 5 3 d 5 2 e 1 0 f 3 2 1 the Breadth-First-Seacrh list:aebfdc
#include iostream
#include vector
#include queue
using namespace std;struct Node
{char data;bool visited;vectorint edges;
};
void bfs(vectorNode nodeList, int startIndex)
{queueint nodeQueue;nodeQueue.push(startIndex);nodeList[startIndex].visited true;while (!nodeQueue.empty()){int currentIndex nodeQueue.front();nodeQueue.pop();cout nodeList[currentIndex].data;for (int i nodeList[currentIndex].edges.size() - 1; i 0; --i){int nextIndex nodeList[currentIndex].edges[i];if (!nodeList[nextIndex].visited){nodeQueue.push(nextIndex);nodeList[nextIndex].visited true;}}}
}int main()
{vectorNode nodeList;char ch;int indexA, indexB;while (cin ch ch ! *){Node node{ch, false, {}};nodeList.push_back(node);}while (cin indexA ch indexB !(indexA -1 indexB -1)){nodeList[indexA].edges.push_back(indexB);nodeList[indexB].edges.push_back(indexA);}cout the ALGraph is\n;for (const Node node : nodeList){cout node.data;for (int i node.edges.size() - 1; i 0; --i)cout node.edges[i];cout endl;}cout the Breadth-First-Seacrh list:;for (int i 0; i nodeList.size(); i)if (!nodeList[i].visited)bfs(nodeList, i);cout endl;
}