Advertisement
karbaev

DFS2

Mar 28th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. vector < vector<int> > g; // граф
  2. int n; // число вершин
  3.  
  4. vector<int> color; // цвет вершины (0, 1, или 2)
  5.  
  6. vector<int> time_in, time_out; // "времена" захода и выхода из вершины
  7. int dfs_timer = 0; // "таймер" для определения времён
  8.  
  9. void dfs2 (int v) {
  10.     time_in[v] = dfs_timer++;
  11.     color[v] = 1;
  12.     for (vector<int>::iterator i=g[v].begin(); i!=g[v].end(); ++i)
  13.         if (color[*i] == 0)
  14.             dfs (*i);
  15.     color[v] = 2;
  16.     time_out[v] = dfs_timer++;
  17. }
  18.  
  19.  
  20. //Это наиболее общий код. Во многих случаях времена захода и выхода из вершины не важны,
  21. //так же как и не важны цвета вершин (но тогда надо будет ввести аналогичный по смыслу булевский массив used).
  22. //Вот наиболее простая реализация:
  23.  
  24. vector<char> used;
  25.  
  26. void dfs3 (int v) {
  27.     used[v] = true;
  28.     for (vector<int>::iterator i=g[v].begin(); i!=g[v].end(); ++i)
  29.         if (!used[*i])
  30.             dfs (*i);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement