https://www.acmicpc.net/problem/1260
내가 푼건아니고 다이나믹 프로그래밍 강의에서 이 문제를 빠르게 적으시는데
좋은 코드여서 옮겨놓는것
| 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 | #include <iostream> #include <algorithm> #include <vector> #include <queue> using namespace std; vector<int> a[1001]; bool c[1001]; void dfs(int x) {     if (c[x])          return;     cout << x << ' ';     c[x] = true;     for (int i = 0; i < a[x].size(); i++)     {         int y = a[x][i];         dfs(y);     } } void bfs(int start) {     queue<int> q;     q.push(start);     c[start] = true;     while (!q.empty())     {         int x = q.front();         q.pop();         cout << x << ' ';         for (int i = 0; i < a[x].size(); i++)         {             int y = a[x][i];             if (c[y] == false)             {                 q.push(y);                 c[y] = true;             }         }     } } int main() {     int n, m, start;     cin >> n >> m >> start;     while (m--)     {         int x, y;         cin >> x >> y;         a[x].push_back(y);         a[y].push_back(x);     }     for (int i = 1; i <= n; i++)     {         sort(a[i].begin(), a[i].end());     }     dfs(start);     cout << "\n";     for (int i = 1; i <= n; i++)         c[i] = false;     bfs(start);     cout << "\n";     return 0; } | cs | 
'NOTE > Algorithm' 카테고리의 다른 글
| [알고리즘문제] 평균점수 / 숫자의 개수 (0) | 2018.04.01 | 
|---|---|
| [알고리즘] 에러의 종류 (펌) (0) | 2018.04.01 | 
| [자료구조] 다이나믹 프로그래밍 강의 정리 (0) | 2018.04.01 | 
| [알고리즘문제] 백준_음계 (0) | 2018.04.01 | 
| [알고리즘문제] 백준_OX퀴즈 (0) | 2018.04.01 |