์๊ฐ ์ ํ | ๋ฉ๋ชจ๋ฆฌ ์ ํ | ์ ๋ต ๋น์จ |
2 ์ด | 128 MB | 30.405% |
https://www.acmicpc.net/problem/1260
ํด๊ฒฐ๋ฐฉ์
๊ฐ๊ฐ DFS์ BFS๋ฅผ ๋๋ ค์ ์ถ๋ ฅํ์๋ค. ์ฃผ์ํด์ผํ ์ ์ ๊ฐ์ ์ ์๋ฐฉํฅ์ด๋ผ๋ ์ !
๋ฉ๋ชจ๋ฆฌ | ์๊ฐ |
25568 KB | 20 ms |
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 | #include<iostream> #include<algorithm> #include<string.h> #include<vector> #include<queue> using namespace std; #define F(x,y,u,p) for(int x = 0; x<u; x++)for(int y = 0; y<p; y++) int n, m, start, chk[1005]; vector<vector<int>> v; void dfs(int start) { chk[start] = 1; printf("%d ", start); for (auto& e : v[start]) { if (chk[e]) continue; chk[e] = 1; dfs(e); } } void bfs(int start) { queue<int> q; q.push(start); chk[start] = 1; while (!q.empty()) { int present = q.front(); q.pop(); printf("%d ", present); for (auto& e : v[present]) { if (chk[e]) continue; chk[e] = 1; q.push(e); } } } int main() { scanf("%d %d %d", &n, &m, &start); v.resize(n * n); for (int i = 0, a, b; i < m; i++) { scanf("%d %d", &a, &b); v[a].push_back(b); v[b].push_back(a); } F(i, j, v.size(), v[i].size()) { sort(v[i].begin(), v[i].end()); } dfs(start); printf("\n"); memset(chk, 0, sizeof(chk)); bfs(start); return 0; } | cs |
'๐ฅ PS(Problem Solving) ๐ฅ > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] #1012 ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2019.10.14 |
---|---|
[BOJ] #10026 ์ ๋ก์์ฝ (0) | 2019.10.13 |
[BOJ] #11403 ๊ฒฝ๋ก ์ฐพ๊ธฐ (0) | 2019.10.13 |
[BOJ] #2589 ๋ณด๋ฌผ์ฌ (0) | 2019.10.13 |
[BOJ] #3187 ์์น๊ธฐ ๊ฟ (0) | 2019.10.13 |
๋๊ธ