#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; }
标签:10005,asas,int,xa,xb,du From: https://www.cnblogs.com/dxy09tj/p/17723441.html