#P1090. 树上简单路径

image

简单的dfs从1节点开始往下深搜,然后回溯记录路径

#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
vector<int>a[N];
int n,m;
vector<int>p;
void dfs(int x,int y){
	if(x==m){
		cout<<p.size()<<"\n";
		for(auto c:p){
			cout<<c<<" ";
		}
		return;
	}
	for(auto c:a[x]){
		if(c==y)continue;
		p.push_back(c);
		dfs(c,x);
		p.pop_back();
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n-1;i++){
		int u,v;
		cin>>u>>v;
		a[u].push_back(v);
		a[v].push_back(u);
	}
	p.push_back(1);
	dfs(1,0);
	return 0;
} 

本文作者:yufan1102

本文链接:https://www.cnblogs.com/yufan1102/p/17869534.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   yufan1102  阅读(10)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.