Cod sursa(job #2368256)

Utilizator alexandru223Dan Alexandru Dicu alexandru223 Data 5 martie 2019 14:51:53
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>
using namespace std;

#define REP(i, a, b) for (int i = a; i <= b; i++)
#define TRvi(x, it) for (vector::iterator<int> it = x.begin(); it != x.end(); it++)
#define TRvii(x, it) for (vector::iterator< pair<int, int> > it = x.begin(); it != x.end(); it++)
#define pb push_back

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector< pair<int, int> > vii;

vi graph[101];
bool used[101];

void BFS(int x) {
	queue<int> display;
	used[x] = 1;
	display.push(x);
	while (display.size()) {
		x = display.front();
		cout << x << ' ';
		REP(i, 0, graph[x].size()-1)
			if (!used[graph[x][i]]) {
				display.push(graph[x][i]);
				used[graph[x][i]] = 1;
			}
		
		display.pop();
	}
}

int main() {
	freopen("bfs.in", "r", stdin);
	freopen("bfs.out", "w", stdout);
	int n, m;
	cin >> n >> m;
	int s;
	cin >> s;
	REP(i, 0, m-1) {
		int x, y;
		cin >> x >> y;
		graph[x].push_back(y);
		graph[y].push_back(x);
	}
	BFS(s);
	return 0;
}