fork download
  1. #include<bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define pp push_back
  5. #define endl '\n'
  6. #define all(x) x.begin(),x.end()
  7. #define ld long double
  8. #define PI acos(-1)
  9. #define ones(x) __builtin_popcountll(x)
  10. //#define int ll
  11.  
  12. using namespace std;
  13.  
  14. void Drakon() {
  15. ios_base::sync_with_stdio(false);
  16. cin.tie(nullptr);
  17. cout.tie(nullptr);
  18. #ifdef Clion
  19. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  20. #endif
  21. }
  22.  
  23. unsigned long long inf = 1e10;
  24. const double EPS = 1e-6;
  25. const int MOD = 1000000007, N = 305, LOG = 25;
  26.  
  27. ll mul(const ll &a, const ll &b) {
  28. return (a % MOD + MOD) * (b % MOD + MOD) % MOD;
  29. }
  30.  
  31. ll add(const ll &a, const ll &b) {
  32. return (a + b + 2 * MOD) % MOD;
  33. }
  34.  
  35. ll pw(ll x, ll y) {
  36. ll ret = 1;
  37. while (y > 0) {
  38. if (y % 2 == 0) {
  39. x = mul(x, x);
  40. y = y / 2;
  41. } else {
  42. ret = mul(ret, x);
  43. y = y - 1;
  44. }
  45. }
  46. return ret;
  47. }
  48.  
  49. vector<int> adj[N];
  50. int par[N][N], leaves;
  51. void dfs(int u, int p, int root) {
  52. par[u][root] = p;
  53. if(adj[u].size() == 1 && u) leaves ++;
  54. for(auto v : adj[u]) {
  55. if(v == p) continue;
  56. dfs(v, u, root);
  57. }
  58. }
  59.  
  60. void solve() {
  61. int n;
  62. cin >> n;
  63. for (int i = 0; i < n - 1; ++i) {
  64. int u, v;
  65. cin >> u >> v;
  66. u--, v--;
  67. adj[u].push_back(v);
  68. adj[v].push_back(u);
  69. }
  70. dfs(0, 0, 0);
  71. vector<int> vec(leaves);
  72. for (int i = 0; i < leaves; ++i) {
  73. cin >> vec[i];
  74. vec[i] --;
  75. }
  76. vec.push_back(0);
  77. for (int i = 0; i < n; ++i) {
  78. dfs(i, i, i);
  79. }
  80. int root = 0;
  81. vector<int> ans = {0};
  82. for(auto l : vec) {
  83. vector<int> path = {l};
  84. int p = l;
  85. while (p != root) {
  86. p = par[p][root];
  87. path.push_back(p);
  88. }
  89. path.pop_back();
  90. reverse(all(path));
  91. for(auto x : path) ans.push_back(x);
  92. root = l;
  93. }
  94. if(ans.size() != 2 * n - 1) cout << -1;
  95. else {
  96. for(auto x : ans) cout << x + 1 << ' ';
  97. }
  98. }
  99.  
  100. signed main() {
  101. Drakon();
  102. int t = 1;
  103. //cin >> t;
  104. while (t--) {
  105. solve();
  106. }
  107. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
-1