Skip to content

Commit

Permalink
240218 문제집
Browse files Browse the repository at this point in the history
  • Loading branch information
bomik0221 committed Feb 18, 2024
1 parent 4377645 commit fe61ae5
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions bomik0221/그래프/240218.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <vector>
#include <queue>

int main() {
int N, M;
std::cin >> N >> M;

std::vector<std::vector<int>> graph(N + 1, std::vector<int>(0, 0));
std::vector<int> indegree(N + 1, 0);
std::vector<int> sorted;

//그래프 입력 받기
for (int i = 0; i < M; i++) {
int a, b;
std::cin >> a >> b;
graph[a].push_back(b);
indegree[b]++;
}

//위상정렬의 초기 상태
std::queue<int> q;
for (int i = 1; i < N + 1; i++) {
if (indegree[i] == 0) {
q.push(i);
break; //쉬운 문제는 먼저 출력하라는 조건이 있으므로 큐에 문제 하나가 들어오면 바로 다음 과정으로 넘어가야함.
}
}
//위상정렬 진행
while (!q.empty()) {
int temp = q.front();
sorted.push_back(temp);
q.pop();
indegree[temp] = -1;
for (auto next_node : graph[temp]) {
indegree[next_node]--;
}
for (int i = 1; i < N + 1; i++) {
if (indegree[i] == 0) {
q.push(i);
break;
}
}
}

//답 출력
for (int i = 0; i < sorted.size(); i++) {
std::cout << sorted[i] << " ";
}
return 0;
}

0 comments on commit fe61ae5

Please sign in to comment.