SHARE
TWEET

Untitled

a guest Jan 26th, 2020 75 in 7 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <stack>
  5. using namespace std;
  6.  
  7.   const int maxn = 1e5;
  8.  vector<int> graph[maxn];
  9.  bool used[maxn];
  10.  int len[maxn];
  11.  int n, m;
  12.  
  13. void bfs(int v) {
  14.  used[v] = 1;
  15.  stack<int> q;
  16.  q.push(v);
  17.  while (!q.empty()) {
  18.  v = q.top;
  19.  q.pop();
  20.  cout << "killrov here ->" <<  v <<  endl;
  21.  for (int i = 0; i < graph[v].size(); ++i) {
  22.  int to = graph[v][i];
  23.  if (!used[to]) {
  24.  q.push(to);
  25.  used[to] = 1;
  26.     }
  27.    }
  28.   }
  29. }
  30. void dfs(int v) {
  31.  used[v] = 1;
  32.  queue<int> q;
  33.  q.push(v);
  34.  while (!q.empty()) {
  35.  v = q.front();
  36.  q.pop;
  37.  cout << "killrov here - >" << v << endl;
  38.  for (int i = 0; i < graph[v].size(); i++) {
  39.  int to = graph[v][i];
  40.  if (!len[to]) {
  41.  q.push(to);
  42.  len[to] = len[v] + 1;
  43.       }
  44.     }
  45.   }
  46. }
  47.    int main() {
  48.    cin >> n >> m;
  49.     for (int i = 0; i < m; ++i) {
  50.     int u, v;
  51.    cin >> u >> v;
  52.   graph[u].emplace_back(v);
  53.    graph[v].emplace_back(u);
  54. }
  55.   cout << "DFS: ";
  56.    dfs(1);
  57.   cout << endl;
  58.    cout << "BFS:";
  59.   bfs(1);
  60. return 0;
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top