树与图的深度优先遍历

知识点

  • dfs
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 100;

int h[N], e[N], ne[N], idx;
int n, m;
bool st[N];

void add(int a, int b){
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}


void dfs(int u){
    st[u] = true;
    for(int i = h[u]; i != -1; i = ne[i]){
        int j = e[i];
        if(!st[j])
            dfs(j);
    }
}

int main(){
    memset(h, -1, sizeof h);
    dfs(1);
}

解题思路

实现代码




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Google Gemini updates: Flash 1.5, Gemma 2 and Project Astra
  • Displaying External Posts on Your al-folio Blog
  • 强化学习导论
  • 企业项目实训
  • 面试总结