AC

lzhh_lzhh26  •  1个月前


include<bits/stdc++.h>

using namespace std; bool road[1005][1005]; bool vid[1005]; set yyjd; //记录已出现过的节点 int n,m,s; //n为节点数,m为边数 void dfs(int node){

vid[node]=1;
yyjd.insert(node); 
cout<<node<<' ';
for(int i=1;i<=n;i++){
	if(road[node][i]==1&&vid[i]==0){
		dfs(i);
		cout<<node<<' ';
		
	}
}

} int main(){

int t;
cin>>t;
while(t--){
	cin>>n>>m>>s;
	for(int i=0;i<m;i++){
		int from,to;
		cin>>from>>to;
		road[from][to]=1;
		road[to][from]=1;
	}
	dfs(s);
	if(yyjd.size()!=n){
		cout<<0;
	}
	cout<<endl; 
	memset(road,0,sizeof road);
	memset(vid,0,sizeof vid);
}
return 0;

}

/ Q: 1 4 4 1 1 2 1 3 1 4 2 3 /


评论:

请先登录,才能进行评论