DFS图的遍历

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> e[N];
bool del[N], st[N];    // 该点是否是陷阱,该点是否抵达过
void dfs(int u) {
    st[u] = true;
    for (int j : e[u])
        if (!del[j] && !st[j]) dfs(j);    // 既不是陷阱也没抵达过就递归过去
}
int main() {
    int n, m, x;
    cin >> n >> m >> x;
    while (x --) {
        int y;
        cin >> y;
        del[y] = true;
    }
    while (m --) {
        int a, b;
        cin >> a >> b;
        e[a].push_back(b);
        e[b].push_back(a);
    }
    dfs(1);
    int ans = 0;
    for (int i = 1; i <= n; i ++)
        if (st[i]) cout << i << ' ';
    return 0;
}