#include <iostream>

using namespace std;

#define MAX_VERTEX 30

int num;

int map[MAX_VERTEX][MAX_VERTEX];

int visit[MAX_VERTEX];

int queue[MAX_VERTEX];

int rear, front;


void bfs(int vertex) {

int i;


visit[vertex] = 1;

cout << vertex;

queue[rear++] = vertex; // ?

while (front < rear) {

vertex = queue[front++]; // ?

for (i = 1; i <= num; i++) {

if (map[vertex][i] == 1 && !visit[i])

{

visit[i] = 1;

cout << i;

queue[rear++] = i;

}

}

}

}

int main() {

int T;

int test_case;

int i, j;

int start;

int v1, v2;


cin >> T;

for (test_case = 1; test_case <= T; test_case++) {

for (i = 0; i < MAX_VERTEX; i++) {

for (j = 0; j < MAX_VERTEX; j++) {

map[i][j] = 0;

}

visit[i] = 0;

queue[i] = 0;

}

front = 0;

rear = 0;


cin >> num >> start;

while (1) {

cin >> v1 >> v2;

if (v1 == -1 && v2 == -1)

break;

map[v1][v2] = map[v2][v1] = 1;

}

cout << test_case;

bfs(start);

cout << endl;

}

system("pause");

return 0;

}

'자윤이와고리즘 > Algorithm' 카테고리의 다른 글

그리디 & 구현  (0) 2020.10.02
스택, 큐, 재귀함수, DFS, BFS  (0) 2020.09.30
Brute-Force  (0) 2020.06.18
Hash  (0) 2020.06.15
시간복잡도  (0) 2020.03.12

+ Recent posts