-
줄 세우기 - 2252Algorithm/BOJ 2020. 3. 4. 03:12123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051//2252 - 줄 세우기#include <iostream>#include <vector>using namespace std;int N, M;vector<vector<int>> adj;vector<int> order;vector<bool> seen;void init(){cin >> N >> M;adj = vector<vector<int>>(N+1);seen = vector<bool>(N+1, false);int u, v;for(int i = 0; i<M; ++i){cin >> u >> v;adj[v].push_back(u);}}void dfs(int here){seen[here] = true;for(int i = 0; i<adj[here].size(); ++i){int there = adj[here][i];if(!seen[there])dfs(there);}order.push_back(here);}void topologicalSort(){for(int i = 1; i<=N; ++i)if(!seen[i])dfs(i);for(int i = 0; i<N; ++i)cout << order[i] << " ";}int main(){init();topologicalSort();}
cs https://www.acmicpc.net/problem/2252
2252번: 줄 세우기
첫째 줄에 N(1≤N≤32,000), M(1≤M≤100,000)이 주어진다. M은 키를 비교한 회수이다. 다음 M개의 줄에는 키를 비교한 두 학생의 번호 A, B가 주어진다. 이는 학생 A가 학생 B의 앞에 서야 한다는 의미이다. 학생들의 번호는 1번부터 N번이다.
www.acmicpc.net
줄 세우기
위상정렬 문제
DAG가 아닌 입력이 없어 싸이클이 생기는 경우를 골라내는 부분은 넣지 않았습니다.
'Algorithm > BOJ' 카테고리의 다른 글
구간 합 구하기 5 - 11660 (0) 2020.03.04 임계경로 - 1948 (0) 2020.03.04 플로이드 - 11404 (0) 2020.03.03 섬의 개수 - 4963 (0) 2020.03.03 행렬 곱셈 순서 - 11049 (0) 2020.03.03