asas

#include<bits/stdc++.h>
using namespace std;

int a[1005][1005];
int n;
bool b[10005];
int c[10005][10005];
int t=0;

void dfs_xian(int x){
	cout<<x<<" ";
	for(int i=1; i<=n; i++){
		if(a[x][i]==1){
			dfs_xian(i);
		}
	}
}

void dfs_hou(int x){
	for(int i=1; i<=n; i++){
		if(a[x][i]==1){
			dfs_hou(i);
		}
	}
	cout<<x<<" "; 
}

void dfs_cen(int x){
	t=0;
	for(int i=1; i<=n; i++){
		if(a[x][i]==1){
			t++;
			c[t]=i;
			cout<<i<<endl;
		}
	}
}

int main(){
	cin>>n;
	int du[100005]={0};
	for(int i=1; i<=n-1; i++){
		cin>>xa>>xb;
		a[xa][xb]=1;
		du[xb]++;
	}
	int s;
	for(int i=1; i<=n; i++){
		if(du[i]==0){
			s=i;	
		}
	}
	dfs_xian(s);
	cout<<endl;
	dfs_hou(s);
	cout<<endl;
	dfs_cen(s);
	return 0;
}

  

posted @ 2023-09-22 21:30  竹余居居居居居  阅读(56)  评论(0)    收藏  举报