-
Notifications
You must be signed in to change notification settings - Fork 5
/
UVA-10410.cpp
61 lines (56 loc) · 1.18 KB
/
UVA-10410.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int n;
int bfs[1010];
int bfsPos[1010];
int dfs[1010];
int dfsPos[1010];
int near[1010];
vector<int> arr[1010];
void constructTree() {
if(n < 2) return;
int i, j, k;
for(i = n-1; i > 1; --i) {
for(j = i-1; j >= 0; --j) {
if(bfsPos[dfs[i]] == bfsPos[dfs[j]] + 1 && dfs[i] > dfs[j]) {
near[dfs[j]] = dfs[i];
break;
}
if(bfsPos[dfs[i]] >= bfsPos[dfs[j]] + 1) {
arr[dfs[j]].push_back(dfs[i]);
break;
}
}
}
arr[dfs[0]].push_back(dfs[1]);
for(i = 0; i < n; ++i) {
for(j = 0; j < arr[i].size(); ++j) {
if(near[arr[i][j]]) arr[i].push_back(near[arr[i][j]]);
}
}
}
int main() {
int i, j;
while(scanf("%d", &n) == 1 && n > 0) {
for(i = 0; i < n; ++i) scanf("%d", &bfs[i]);
for(i = 0; i < n; ++i) scanf("%d", &dfs[i]);
for(i = 0; i < n; ++i) {
bfsPos[bfs[i]] = i;
dfsPos[dfs[i]] = i;
}
for(i = 1; i <= n; ++i) {
arr[i] = vector<int>();
near[i] = 0;
}
constructTree();
for(i = 1; i <= n; ++i) {
sort(arr[i].begin(), arr[i].end());
printf("%d:", i);
for(j = 0; j < arr[i].size(); ++j) printf(" %d", arr[i][j]);
putchar('\n');
}
}
return 0;
}