Advertisement
Josif_tepe

Untitled

May 15th, 2022
526
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. int n, m;
  5. vector<int> graph[1005];
  6. bool visited[1005];
  7. void dfs(int node) {
  8.     visited[node] = true;
  9.     cout << node << " ";
  10.     for(int i = 0; i < graph[node].size(); i++) {
  11.         int sosed = graph[node][i];
  12.         if(!visited[sosed]) {
  13.             dfs(sosed);
  14.         }
  15.     }
  16. }
  17. int main() {
  18.     cin >> n >> m;
  19.     memset(visited, false, sizeof visited);
  20.     for(int i = 0; i < m; i++) {
  21.         int a,b;
  22.         cin >> a >> b;
  23.         graph[a].push_back(b);
  24.     }
  25.     dfs(0);
  26.     return 0;
  27. }
  28.  
Advertisement
RAW Paste Data Copied
Advertisement