You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.8 KiB

2 years ago
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
/*
1DFS,
2DFSDFS
DFS2N
3https://www.cnblogs.com/fusiwei/p/11758087.html
https://www.luogu.com.cn/blog/p6174/dfs-xu-ru-men
DFS
线便
DFSDFS
DFS线线
*/
//邻接表
int idx, e[N], ne[N], h[N];
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int n;
int id[N];//id[] 数组就是DFS序的数组
int cnt; //cnt就是计时变量
/**
* dfs
* @param u
*/
void dfs(int u) {
id[++cnt] = u; //记录当前结点的dfs序号
for (int i = h[u]; i != -1; i = ne[i]) {//遍历当前结点的每一边条
int y = e[i];
dfs(y);
}
}
/*
98
9
1 2
1 7
1 4
2 8
2 5
4 3
4 6
3 9
*/
int main() {
//邻接表初始化
memset(h, -1, sizeof(h));
cin >> n;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
add(x, y);
}
dfs(1);
for (int i = 1; i <= cnt; i++)
printf("%d ", id[i]);
return 0;
}