- hzoi013 的博客
DFS(vector容器)
- 2024-12-14 11:08:36 @
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int n = 0, m = 0;
bool v[maxn] = {};
vector<int> e[maxn];
void dfs(int x)
{
cout << x << ' ';
v[x] = true;
// for(auto i : e[x])
for(int i=0; i<e[x].size(); i++)
{
int y = e[x][i];
if(!v[y])
{
dfs(y);
}
}
}
int main()
{
int x = 0, y = 0;
cin >> n >> m;
for(int i=1; i<=m; i++)
{
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
dfs(1);
return 0;
}