Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- int t;
- cin >> t;
- while(t--){
- int n; cin >> n;
- vector<int> a(n);
- for (auto &x : a) cin >> x;
- vector<vector<int>> g(n);
- for (int e = 0; e < n - 1; ++e){
- int u, v; cin >> u >> v;
- --u, --v;
- g[u].push_back(v);
- g[v].push_back(u);
- }
- int root = -1, mn = *min_element(a.begin(), a.end());
- for (int i = 0; i < n; ++i) {
- if (a[i] == mn) root = i;
- }
- cout << n - 1 << "\n";
- function<void(int, int)> dfs = [&](int u, int p){
- for (auto &v : g[u]){
- if (v == p) continue;
- dfs(v, u);
- }
- if (u != root) cout << u + 1 << ' ';
- };
- dfs(root, -1);
- cout << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment