알고리즘 문제/BOJ
1389번 케빈 베이컨의 6단계 법칙
parkit
2018. 8. 30. 01:44
728x90
반응형
기본적인 BFS 문제이다.
문제 유형에 '플로이드 와샬 알고리즘'이 있어서, 쫄았다.(?)
(배웠긴 했지만, 까먹었기 때문이다. 꼭 다음 주 내로 각종 알고리즘들을 복습해야겠다.)
https://www.acmicpc.net/problem/1389
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #include <iostream> #include <queue> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> using namespace std; int N = 0, M = 0; int s = 0, e = 0; vector<vector<int> > v; int BFS(int start) { int ret = 0; for (int index = 1; index <= N; index++) { bool stop = false; if (start != index) { queue<int> q; bool visit[102] = { false, }; q.push(start); while (!q.empty()) { int qSize = q.size(); while (qSize--) { int here = q.front(); q.pop(); if (here == index) { stop = true; } if (visit[here]) continue; visit[here] = true; for (int next : v[here]) { if (!visit[next]) { q.push(next); } } } if (stop) break; ++ret; } } } return ret; } int main(void) { int ans = 987654321; scanf("%d %d", &N, &M); v.resize(N + 1); while (M--) { scanf("%d %d", &s, &e); v[s].push_back(e); v[e].push_back(s); } int min_index = 0; for (int i = 1; i <= N; i++) { int var = BFS(i); if (ans > var) { ans = var; min_index = i; } } printf("%d\n", min_index); return 0; } | cs |
728x90
반응형