Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <queue>
- #include <vector>
- #include <stack>
- using namespace std;
- int n, m;
- vector<int> graph[100005];
- bool visited[100005];
- stack<int> st;
- void dfs(int node) {
- visited[node] = true;
- for(int i = 0; i < graph[node].size(); i++) {
- if(!visited[graph[node][i]]) {
- dfs(graph[node][i]);
- }
- }
- st.push(node);
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin >> n >> m;
- for(int i = 0; i < m; i++) {
- int a, b;
- cin >> a >> b;
- graph[a].push_back(b);
- }
- for(int i = 0; i <= n; i++) {
- visited[i] = false;
- }
- for(int i = 1; i <= n; i++){
- if(!visited[i]) {
- dfs(i);
- }
- }
- while(!st.empty()) {
- cout << st.top() << " ";
- st.pop();
- }
- return 0;
- }
- // 4 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement