Warning: session_start(): open(/tmp/sess_02daedc37ac2aad7664eac917abf1765, O_RDWR) failed: No space left on device (28) in /data/wiki/inc/init.php on line 239

Warning: session_start(): Failed to read session data: files (path: ) in /data/wiki/inc/init.php on line 239

Warning: Cannot modify header information - headers already sent by (output started at /data/wiki/inc/init.php:239) in /data/wiki/inc/auth.php on line 430
Writing /data/wiki/data/cache/d/de2edb2fcb553ea79b79c722a4e13dbc.captchaip failed

Warning: Cannot modify header information - headers already sent by (output started at /data/wiki/inc/init.php:239) in /data/wiki/inc/actions.php on line 38

Warning: Cannot modify header information - headers already sent by (output started at /data/wiki/inc/init.php:239) in /data/wiki/lib/tpl/dokuwiki/main.php on line 12
2020-2021:teams:namespace:kongyou:深度优先搜索 [CVBB ACM Team]

用户工具

站点工具


2020-2021:teams:namespace:kongyou:深度优先搜索

到此差别页面的链接

2020-2021:teams:namespace:kongyou:深度优先搜索 [2020/05/16 11:23]
kongyou 创建
2020-2021:teams:namespace:kongyou:深度优先搜索 [2020/05/16 11:29] (当前版本)
kongyou
行 1: 行 1:
-====深度优先搜索(DFS)====+=========深度优先搜索(DFS)=========
  
 过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次. 过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次.
行 24: 行 24:
  
  
 +递归实现
 +<code c++>
 +void dfs(int v)//​以v开始做深度优先搜索
 +{
 +    list<​int>::​iterator it;
 +    visited[v] = true;
 +    cout << v << " ";
 +    for (it = graph[v].begin();​ it != graph[v].end();​ it++)
 +        if (!visited[*it])
 +            dfs(*it);
 +}
 +</​code>​
 +
 +非递归实现(栈)
 +<code c++>
 +void dfs(int v)//​以v开始做深度优先搜索
 +{
 +    list<​int>::​iterator it;
 +    visited[v] = true;
 +    cout << v << " ";
 +    stack<​int>​mystack;​
 +    mystack.push(v);​
 +    while (!mystack.empty())
 +    {
 +        v = mystack.top();​
 +        mystack.pop();​
 +        if (!visited[v])
 +        {
 +            cout << v << " ";
 +            visited[v] = true;
 +        }
 +
 +        for (it = graph[v].begin();​ it != graph[v].end();​ it++)
 +        {
 +            if (!visited[*it])
 +            {
 +                mystack.push(*it);​
 +
 +            }
 +        }
 +    }
 +    cout << endl;
 +}
 +</​code>​
    
2020-2021/teams/namespace/kongyou/深度优先搜索.1589599422.txt.gz · 最后更改: 2020/05/16 11:23 由 kongyou