北航计算机夏令营机试(?)三叉树

题目详情请参见:https://blog.csdn.net/weixin_43347688/article/details/114538022

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
int n, m; int a[110], b[110];
map<int, vector<int>> mp;
int x,y,z,w; int goal[1000];
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=0; i<n; ++i){
cin>>x>>y>>z>>w;
if(i==0){
goal[0] = x; mp[x]=vector<int>();
mp[x].push_back(-1), mp[x].push_back(x);
}
if(y!=-1) mp[y]=vector<int>(mp[x]), mp[y].push_back(y);
if(z!=-1) mp[z]=vector<int>(mp[x]), mp[z].push_back(z);
if(w!=-1) mp[w]=vector<int>(mp[x]), mp[w].push_back(w);
}
cin>>m;
for(int i=0; i<m; ++i){
cin>>x>>y; goal[y+1]=x;
}
goal[m+1] = goal[0];
for(int i=1; i<=m+1; ++i){
vector<int> path1 = mp[goal[i-1]];
vector<int> path2 = mp[goal[i]];
int j;
for(j=0; j<path1.size() && j<path2.size(); ++j){
if(path1[j]!=path2[j]) break;
}
--j;
if(i==1)
for(int k=path1.size()-1; k>j; --k) cout<<path1[k]<<" ";
else
for(int k=path1.size()-2; k>j; --k) cout<<path1[k]<<" ";
for(int k=j; k<path2.size(); ++k) cout<<path2[k]<<" ";
cout<<endl;
}
}
/*
10
100 101 108 107
101 1 102 2
108 103 104 105
107 17 109 18
102 3 4 5
103 7 8 9
104 10 106 11
105 15 16 -1
109 19 20 21
106 12 13 14
5
8 1
14 3
16 2
5 0
19 4
*/

本文作者:MoonOut

本文链接:https://www.cnblogs.com/moonout/p/16454166.html

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

posted @   MoonOut  阅读(70)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Sibelius: Violin Concerto in D Minor, Op. 47:III. Allegro, ma non tanto Jascha Heifetz / Chicago Symphony Orchestra
Sibelius: Violin Concerto in D Minor, Op. 47:III. Allegro, ma non tanto - Jascha Heifetz / Chicago Symphony Orchestra
00:00 / 00:00
An audio error has occurred.