Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- vector<int> graph[100000];
- bool used[100000]; //вместо visited массив меток обычно называют used.
- int main() {
- //Ввод графа...
- queue<int> q;
- q.push(0); //в качестве начальной вершины используем 0.
- used[0] = true;
- while (!q.empty()) {
- int cur = q.front(); //извлекаем из очереди текущую вершину
- q.pop();
- //Здесь должна быть обработка текущей вершины.
- cout << "BFS at vertex " << cur + 1 << endl;
- for (int neighbor: graph[cur]) { //добавляем всех непосещённых соседей.
- if (!used[neighbor]) {
- q.push(neighbor);
- used[neighbor] = true;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment