Ankit_132

D

Jun 26th, 2024
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. int main() {
  6.     int t;
  7.     cin >> t;
  8.     while(t--){
  9.      
  10.         int n; cin >> n;
  11.      
  12.         vector<int> a(n);
  13.         for (auto &x : a) cin >> x;
  14.      
  15.         vector<vector<int>> g(n);
  16.         for (int e = 0; e < n - 1; ++e){
  17.             int u, v; cin >> u >> v;
  18.             --u, --v;
  19.             g[u].push_back(v);
  20.             g[v].push_back(u);
  21.         }
  22.      
  23.         int root = -1, mn = *min_element(a.begin(), a.end());
  24.         for (int i = 0; i < n; ++i) {
  25.             if (a[i] == mn) root = i;
  26.         }
  27.        
  28.         cout << n - 1 << "\n";
  29.      
  30.         function<void(int, int)> dfs = [&](int u, int p){
  31.             for (auto &v : g[u]){
  32.                 if (v == p) continue;
  33.                 dfs(v, u);
  34.             }
  35.             if (u != root) cout << u + 1 << ' ';
  36.         };
  37.      
  38.         dfs(root, -1);
  39.         cout << "\n";
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment