#include<bits/stdc++.h> using namespace std; int n,id[103],od[103],root; vector<int> G[103]; int ans[103],l; void dfs1(int r){ cout<<r<<" "; if(od[r]==0)ans[++l]=r; for(int i=0;i<G[r].size();i++){ dfs1(G[r][i]); } } void dfs2(int r){ for(int i=0;i<G[r].size();i++){ dfs2(G[r][i]); } cout<<r<<" "; } void bfs(int r){ queue<int> q; q.push(r); while(!q.empty()){ int h=q.front();q.pop();cout<<h<<" "; for(int i=0;i<G[h].size();i++){ q.push(G[h][i]); } } } int main(){ cin>>n; for(int i=1;i<n;i++){ int u,v; cin>>u>>v; G[u].push_back(v); id[v]++;od[u]++; } for(int i=1;i<=n;i++)if(id[i]==0)root=i; dfs1(root);cout<<endl; dfs2(root);cout<<endl; bfs(root);cout<<endl; for(int i=1;i<=l;i++)cout<<ans[i]<<" "; return 0; }
//D
标签:cout,10.6,int,++,103,id From: https://www.cnblogs.com/zangqy/p/17744993.html