For a tree, 1 is the root. It is required to traverse all vertices in the tree, give the access sequence of the leaf nodes, and limit each side to access at most two times.
First of all, this is a tree, so the route between the two points is determined, So I first store the DFS pre-processing path from the root node to each leaf for later output.
Then, output the leaf node in sequence according to the requirements of the question, and then from a leaf to the next leaf, from their recent public ancestor turning point, so I have preprocessed the two adjacent leaf nodes.
# Include <iostream> # include <cstdlib> # include <cstring> # include <string> # include <cstdio> # include <cmath> # include <algorithm> # include <vector> # include <queue> # include <map> # define INF 0x3f3f3f3f # pragma comment (linker, "/Stack: 16777216") # define EPS 1e-6 # define ll long longusing namespace STD; const int maxn = 310; struct node {int V, ID, W; // ID is the edge number or query number node * Next;} ed [maxn <2], * head [maxn], * Q [maxn]; struct Qn Ode {int U, V, ans; // storage query node, ANS recent public ancestor} Qu [maxn]; int Fa [maxn], vis [maxn], CNT; void Init (int n) {CNT = 0; memset (VIS, 0, sizeof vis); memset (Head, 0, sizeof head); memset (Q, 0, sizeof Q); For (INT I = 0; I <= N; I ++) Fa [I] = I;} int getfa (INT X) {If (Fa [x] = x) return X; return Fa [x] = getfa (Fa [x]);} void LCA (int u) {Fa [u] = u, vis [u] = 1; for (node * P = Q [u]; P! = NULL; P = p-> next) {If (vis [p-> V]) {int id = p-> ID; qu [ID]. ans = getfa (p-> V) ;}}for (node * P = head [u]; P! = NULL; P = p-> next) {If (! Vis [p-> V]) {LCA (p-> V); Fa [p-> V] = u ;}} void Adde (node * E [], int U, int V, int W, int ID) // create an array of edge e upload nodes. It is the query edge or the tree edge. {Ed [CNT]. V = V; Ed [CNT]. W = W; Ed [CNT]. id = ID; Ed [CNT]. next = E [u]; E [u] = & ED [CNT ++];} inline int readint () {char CH = getchar (); if (CH = EOF) Return-1; int DATA = 0; while (CH <'0' | ch> '9') {CH = getchar (); if (CH = EOF) Return-1;} do {DATA = Data * 10 + CH-'0'; CH = getchar ();} while (CH> = '0' & Ch <= '9'); return data;} int f [maxn], leaf [maxn], ANS [610], flag; void DFS (INT s) {for (node * P = head [s]; P! = NULL; P = p-> next) {If (! Vis [p-> V]) {vis [p-> V] = 1; F [p-> V] = s; DFS (p-> V );}}} void output (int s, int t) // R-> leaf {If (F [T]! = S) output (S, F [T]); ans [CNT ++] = T; flag ++;} void output1 (int s, int T) // leaf-> r {do {flag ++; ans [CNT ++] = f [s]; S = f [s];} while (s! = T) ;}int main () {int N, A, B, I, K; scanf ("% d", & N); Init (N ); for (I = 0; I <n-1; I ++) {scanf ("% d", & A, & B); Adde (Head, A, B, 1, I); Adde (Head, B, A, 1, I);} k = 0; while (leaf [k] = readint ())! =-1) K ++; for (I = 1; I <K; I ++) {Adde (Q, leaf [I], leaf [I-1], 1, i); Adde (Q, leaf [I-1], leaf [I], 1, I);} LCA (1); memset (VIS, 0, sizeof vis ); memset (F,-1, sizeof F); vis [1] = 1; DFS (1); CNT = 0; flag = 1; ans [CNT ++] = 1; output (1, leaf [0]); If (flag <= N + N-1) for (I = 1; I <K; I ++) {output1 (leaf [I-1], Qu [I]. ans); If (flag> N + N-1) break; output (qu [I]. ANS, leaf [I]); If (flag> N + N-1) break;} If (flag <= N + N-1) output1 (leaf [k-1], 1 ); if (flag <= N + N-1) {printf ("1"); for (I = 1; I <CNT; I ++) printf ("% d ", ans [I]); puts ("");} else printf ("-1 \ n"); Return 0 ;}