Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <stack>
- #include <vector>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- int n, m;
- vector <int> v[32002];
- stack <int> st;
- bool visited[32002];
- bool flag;
- void dfs(int here) {
- visited[here] = true;
- for (int i = 0; i < v[here].size(); i++) {
- if (!visited[v[here][i]])dfs(v[here][i]);
- }
- st.push(here);
- }
- int main() {
- memset(visited, 0, sizeof(visited));
- scanf("%d %d\n", &n, &m);
- for (int i = 0; i < m; i++) {
- int a, b; scanf("%d %d", &a, &b);
- v[a].push_back(b); // a -> b
- }
- for (int i = 1; i <= n; i++) {
- if (!visited[i])dfs(i);
- }
- while (!st.empty()) {
- printf("%d ", st.top());
- st.pop();
- }
- printf("\n");
- }
Add Comment
Please, Sign In to add comment