/** * 图的广度优先遍历 **/
void bfs(vector<vector<int> >& g, int s) {
queue<int> q;
q.push(s);
unordered_set<int> visited;
visited.insert(s);
while (!q.empty()) {
int cur = q.front();
cout<<cur<<",";
q.pop();
for (int i = 0; i < g.size(); ++i) {
if (g[cur][i] > 0 && visited.count(i) == 0) {
q.push(i);
visited.insert(i);
}
}
}
cout<<endl;
}
/** * 图的深度优先遍历 **/
void dfs_inner(vector<vector<int> >& g, int s, unordered_set<int>& visited) {
visited.insert(s);//一定在这里着色
cout<<s<<endl;
for (int i = 0; i < g.size(); ++i) {
if (visited.count(i) == 0 && g[s][i] > 0) {
dfs_inner(g, i, visited);
}
}
}
void dfs(vector<vector<int> >& g) {
unordered_set<int> visited;
for (int i = 0; i < g.size(); ++i) {
if (visited.count(i) == 0) {
dfs_inner(g, i, visited);
}
}
}