์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- Spring
- error
- ๊ทธ๋ฆฌ๋
- ๋ฐฑํธ๋ํน
- ์์ ์ ๋ ฌ
- thymeleaf
- ์ด๋ถ ํ์
- DP
- CVE
- JPA
- dfs
- ์ฐ์ ์์ ํ
- BFS
- ๋ฌธ์์ด
- OS
- GCP
- ์ฌ๊ท
- Reversing
- ๊ตฌํ
- ๋ถํ ์ ๋ณต
- c++
- java
- ์๋ฎฌ๋ ์ด์
- ์คํ
- ์ต๋จ ๊ฒฝ๋ก
- dynamic debugging
- ๋ฐ์ดํฌ์คํธ๋ผ
- ๋์ ํฉ
- ๋งต
- web
- Today
- Total
hades
[Baekjoon] 2252๋ฒ: ์ค์ธ์ฐ๊ธฐ ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/2252
๐ ์ค๊ณ
๋ ๋ ธ๋ ๊ฐ์ ์์๊ฐ ์ ํด์ ธ ์๊ณ , ๋ต์ด ์ฌ๋ฌ๊ฐ์ง์ธ ๊ฒฝ์ฐ์๋ ์๋ฌด๊ฑฐ๋ ์ถ๋ ฅํ๋ผ๋ ๊ฒ์์ ์ ํ์ ์ธ ์์ ์ ๋ ฌ์์ ์ฝ๊ฒ ํ์ ํ ์ ์์๋ค. ๋ค๋ง, ๋ค์ ๊ธฐ์ต์ํค๊ธฐ ์ํด ์๊ณ ๋ฆฌ์ฆ ํํธ์ ์ ๋ฆฌํ์๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
int n, m, a, b;
vector<int> degree(32001);
vector<vector<int>> graph(32001);
void topology_sort() {
queue<int> q;
for (int i = 1; i <= n; i++) {
if (degree[i] == 0) {
q.push(i);
}
}
while (!q.empty()) {
int cur_x = q.front();
q.pop();
cout << cur_x << " ";
int size = graph[cur_x].size();
for (int i = 0; i < size; i++) {
int next_x = graph[cur_x][i];
degree[next_x] -= 1;
if (degree[next_x] == 0) {
q.push(next_x);
; }
}
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a >> b;
degree[b] += 1;
graph[a].push_back(b);
}
topology_sort();
return 0;
}
โ๏ธ์๊ณ ๋ฆฌ์ฆ
์์ ์ ๋ ฌ์ ๋ ธ๋ ๊ฐ์ ์์๊ฐ ์ ํด์ ธ ์์ ๋, ๋์ดํ๊ฑฐ๋ ์์ ์ ์ฒ๋ฆฌํ๋ ์ํฉ์์ ์ ๋ ฌํ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค.
์์ ์ ๋ ฌ์ DAG(Directed Acyclic Graph)์์๋ง ๊ฐ๋ฅํ๋ค. ์๋ฐฉํฅ์ด๊ฑฐ๋ ์ฌ์ดํด์ด ์์ผ๋ฉด, ์ฐ์ ์์๊ฐ ์๋ ๊ฒ๊ณผ ๊ฐ๋ค.
๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
1. ๊ฐ ๋ ธ๋๋ง๋ค ์ง์ ์ฐจ์๋ฅผ ๊ธฐ๋กํ๋ค. ์ฐ์ ์์๊ฐ ๋ฎ๋ค๋ฉด ์ง์ ์ฐจ์๋ฅผ ์ฆ๊ฐ์ํจ๋ค. ์ง์ ์ฐจ์๊ฐ 0์ด ๋์์ ๋, ๊ทธ ๋ ธ๋๋ฅผ ์ด์ฉํ ์ ์๋ค.
2. ์ง์ ์ฐจ์๊ฐ 0์ธ ๋ ธ๋๋ฅผ ํ์ ๋ด๊ณ , ํ๋์ฉ ๊บผ๋ด๋ฉด์ ์ฐ๊ฒฐ๋ ๋ ธ๋๋ค์ ์ง์ ์ฐจ์๋ฅผ 1์ฉ ์ค์ธ๋ค. ๊บผ๋ธ ๋ ธ๋๊ฐ ์ฌ์ฉ๋์ด ์ฌ๋ผ์ง๋ฉด์ ์ ํ๋์ด์ผ ํ ๊ฒ์ด ํ๋ ์ค์๊ธฐ ๋๋ฌธ์ด๋ค. ์ฐ๊ฒฐ๋ ๋ ธ๋๋ค์ ์ง์ ์ฐจ์๊ฐ 0์ด๋ผ๋ฉด ํ์ ๋ด๋๋ค.
์ด ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1766๋ฒ: ๋ฌธ์ ์ง (0) | 2024.09.30 |
---|---|
[Baekjoon] 1138๋ฒ: ํ ์ค๋ก ์๊ธฐ (0) | 2024.09.25 |
[Baekjoon] 2143๋ฒ: ๋ ๋ฐฐ์ด์ ํฉ (0) | 2024.09.20 |
[Baekjoon] 1202๋ฒ: ๋ณด์ ๋๋ (0) | 2024.09.19 |
[Baekjoon] 9527๋ฒ: 1์ ๊ฐ์ ์ธ๊ธฐ (0) | 2024.09.18 |