Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
914 B
3
Indexable
Never
#include <iostream>
using namespace std;

int queue[1005];
int f, r;
int arr[1005][1005];
int dd[1005];
int v, e;

void push(int num) {
	r++;
	queue[r] = num;
}

int front() {
	int temp;
	f++;
	temp = queue[f];
	f--;
	return temp;
}

void pop() {
	f++;
}

bool empty() {
	if (f == r)
		return true;
	return false;
}

void BFS(int i) {
	push(i);
	dd[i] = 1;
	cout << i << " ";
	while (!empty()) {
		int x = front();
		pop();
		for (int j = 1; j <= v; j++) {
			if (arr[x][j] == 1 && dd[j] == 0) {
				dd[j] = 1;
				cout << j << " ";
				push(j);
			}
		}
	}
	cout << endl;
}

int main() {
	int t;
	cin >> t;
	while (t--) {
		int u;
		cin >> v >> e >> u;
		for(int i=0; i<e; i++){
			int x, y;
			cin >> x >> y;
			arr[x][y] = 1;
		}

		f = -1; r = -1;
		BFS(u);

		for (int i = 1; i <= v; i++) {
			dd[i] = 0;
		}

		for (int i = 1; i <= v; i++) {
			for (int j = 1; j <= v; j++) {
				arr[i][j] = 0;
			}
		}
	}
}