Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- dfs
- Spring
- GCP
- ๋ฐฑํธ๋ํน
- ์ฌ๊ท
- web
- CVE
- BFS
- ์์ ์ ๋ ฌ
- ๊ทธ๋ฆฌ๋
- ์ด๋ถ ํ์
- DP
- dynamic debugging
- ๊ตฌํ
- ์ฐ์ ์์ ํ
- java
- error
- ๋์ ํฉ
- ๋ถํ ์ ๋ณต
- JPA
- ์ต๋จ ๊ฒฝ๋ก
- ์๋ฎฌ๋ ์ด์
- Reversing
- ์คํ
- c++
- ๋งต
- thymeleaf
- OS
- ๋ฐ์ดํฌ์คํธ๋ผ
- ๋ฌธ์์ด
Archives
- Today
- Total
hades
[Baekjoon] 1766๋ฒ: ๋ฌธ์ ์ง ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/1766
๐ ์ค๊ณ
๋จผ์ ํธ๋ ๊ฒ์ด ์ข์ ๋ฌธ์ ๋ฅผ ๋จผ์ ํผ๋ค๋ ๊ฒ์์ ๋ ๋ฌธ์ ๊ฐ์ ์์๊ฐ ์ ํด์ ธ ์๊ณ , ์์ ์ ๋ ฌ์์ ์ฝ๊ฒ ํ์ ํ ์ ์์๋ค.
์ถ๊ฐ๋ก ์ฐ์ ์์๊ฐ ๊ฐ์ ๊ฒฝ์ฐ, ์ฌ์ด ๋ฌธ์ ๋ฅผ ๋จผ์ ํ๊ธฐ ๋๋ฌธ์ ์ง์ ์ฐจ์๊ฐ 0์ด ๋์์ ๋, ๋ฐ๋ก ์ถ๋ ฅํ์ง ๋ง๊ณ , greater ์ฐ์ ์์ ํ๋ฅผ ์ด์ฉํ์ฌ ์ง์ ์ฐจ์๊ฐ 0์ธ ๋ฌธ์ ๋ฒํธ๋ฅผ ๋ด์ ๋นผ๋ผ ๋๋ง๋ค ์ถ๋ ฅํ๋๋ก ํ๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
int n, m, a, b;
priority_queue<int, vector<int>, greater<int>> pq;
vector<int> degree(100001);
vector<vector<int>> adj_list(100001);
void topology_sort() {
for (int i = 1; i <= n; i++) {
if (degree[i] == 0) {
pq.push(i);
}
}
while (!pq.empty()) {
int cur = pq.top();
pq.pop();
cout << cur << " ";
int size = adj_list[cur].size();
for (int i = 0; i < size; i++) {
int next = adj_list[cur][i];
degree[next] -= 1;
if (degree[next] == 0) {
pq.push(next);
}
}
}
}
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;
adj_list[a].push_back(b);
}
topology_sort();
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 16928๋ฒ: ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ ๊ฒ์ (2) | 2024.10.15 |
---|---|
[Baekjoon] 1138๋ฒ: ํ ์ค๋ก ์๊ธฐ (0) | 2024.09.25 |
[Baekjoon] 2252๋ฒ: ์ค์ธ์ฐ๊ธฐ (0) | 2024.09.23 |
[Baekjoon] 2143๋ฒ: ๋ ๋ฐฐ์ด์ ํฉ (0) | 2024.09.20 |
[Baekjoon] 1202๋ฒ: ๋ณด์ ๋๋ (0) | 2024.09.19 |