点击打开题目链接
#include <bits/stdc++.h>
using namespace std;
int n, m;
bool Graph[1010][1010];
queue<int>Q;
bool vis[1010];
void DFS(int s);
int main()
{
int k, s;
cin >> k;
while(k --)
{
memset(Graph, 0, sizeof(Graph));
memset(vis, 0, sizeof(vis));
cin >> n >> m >> s;
while(m --)
{
int u, v;
cin >> u >> v;
Graph[u][v] = Graph[v][u] = 1;
}
DFS(s);
int t = Q.size();
cout << Q.front();
Q.pop();
while(!Q.empty())
{
cout << ' ' << Q.front();
Q.pop();
}
if(t != 2*n-1)
{
cout << ' ' << 0;
}
cout << endl;
}
return 0;
}
void DFS(int s)
{
Q.push(s);
vis[s] = 1;
for(int i = 1; i <= n; i++)
{
if(!vis[i] && Graph[s][i])
{
DFS(i);
Q.push(s);//每次回溯时入队
}
}
}