반응형
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
for (int T = 1; T <= 10; T++) {
int V, E;
cin >> V >> E;
vector<vector<int>> graph(V + 1);
vector<int> indegree(V + 1, 0);
for (int i = 1; i <= E; i++) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
indegree[v]++;
}
queue<int> q;
cout << '#' << T << ' ';
for (int i = 1; i <= V; i++) {
if (indegree[i] == 0) {
cout << i << ' ';
q.push(i);
}
}
while (!q.empty()) {
int front = q.front();
q.pop();
for (auto nextNode : graph[front]) {
indegree[nextNode] --;
if (indegree[nextNode] == 0) {
cout << nextNode << ' ';
q.push(nextNode);
}
}
}
cout << '\n';
}
}
반응형
'알고리즘 > 코드' 카테고리의 다른 글
에라토스테네스의 체 C++ 코드 (0) | 2024.06.26 |
---|---|
KMP C++ 코드 (0) | 2024.06.18 |
정렬,머지소트,퀵소트 C++ 코드 (0) | 2024.06.18 |
크루스칼, 프림 알고리즘 C++ 코드 (0) | 2024.06.06 |
세그먼트 트리 / Segment Tree / Segment Tree with Lazy Propagation C++ 코드 (0) | 2024.06.06 |
반응형
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
for (int T = 1; T <= 10; T++) {
int V, E;
cin >> V >> E;
vector<vector<int>> graph(V + 1);
vector<int> indegree(V + 1, 0);
for (int i = 1; i <= E; i++) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
indegree[v]++;
}
queue<int> q;
cout << '#' << T << ' ';
for (int i = 1; i <= V; i++) {
if (indegree[i] == 0) {
cout << i << ' ';
q.push(i);
}
}
while (!q.empty()) {
int front = q.front();
q.pop();
for (auto nextNode : graph[front]) {
indegree[nextNode] --;
if (indegree[nextNode] == 0) {
cout << nextNode << ' ';
q.push(nextNode);
}
}
}
cout << '\n';
}
}
반응형
'알고리즘 > 코드' 카테고리의 다른 글
에라토스테네스의 체 C++ 코드 (0) | 2024.06.26 |
---|---|
KMP C++ 코드 (0) | 2024.06.18 |
정렬,머지소트,퀵소트 C++ 코드 (0) | 2024.06.18 |
크루스칼, 프림 알고리즘 C++ 코드 (0) | 2024.06.06 |
세그먼트 트리 / Segment Tree / Segment Tree with Lazy Propagation C++ 코드 (0) | 2024.06.06 |