图
图的遍历:DFS
#include <iostream>
#include <vector>
using namespace std;
const int MAXV = 1000;
int n;
const int INF = 1e9;
int G[MAXV][MAXV];
bool vis[MAXV] = {false};
void DFS(int u, int depth)
{
vis[u] = true;
for (int v = 0; v < n; v++)
{
if (!vis[v] && G[u][v] != INF)
{
DFS(v, depth + 1);
}
}
}
void DFSTrave()
{
for (int i = 0; i < n; i++)
{
if (!vis[i])
{
DFS(i, 1);
}
}
}
vector<int> Adj[MAXV];
bool vis[MAXV] = {false};
void DFS(int u, int depth)
{
vis[u] = true;
for (vector<int>::iterator it = Adj[u].begin(); it != Adj[u].end(); it++)
{
int v = *it;
if (!vis[v])
{
DFS(v, depth + 1);
}
}
}
void DFSTrave()
{
for (int i = 0; i < n; i++)
{
if (!vis[i])
{
DFS(i, 1);
}
}
}