알고리즘 문제/BOJ
2252번 줄 세우기
parkit
2019. 4. 21. 22:54
728x90
반응형
https://www.acmicpc.net/problem/2252
위상 정렬 참고 블로그 : https://jason9319.tistory.com/93
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> #include <set> #include <sstream> #include <tuple> #pragma warning(disable:4996) #pragma comment(linker, "/STACK:336777216") using namespace std; int N = 0, M = 0; stack<int> stc; vector<int> v[100100]; bool visit[100100] = { false, }; void DFS(int s) { visit[s] = true; for (auto i : v[s]) if (!visit[i]) DFS(i); stc.push(s); } void print() { while (!stc.empty()) { printf("%d ", stc.top()); stc.pop(); } printf("\n"); } int main(void) { int s = 0, e = 0; scanf("%d %d", &N, &M); while (M--) { scanf("%d %d", &s, &e); v[s].push_back(e); } for (int i = 1; i <= N; i++) if (!visit[i]) DFS(i); print(); return 0; } | cs |
728x90
반응형