Untitled

 avatar
unknown
plain_text
18 days ago
5.4 kB
2
Indexable
#include <bits/stdc++.h>

#define ll long long
#define vll vector<ll>
#define endl '\n'
#define Ones(n) __builtin_popcount(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define IOS ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define FilesIO freopen("input.txt", "r", stdin);  freopen("output.txt", "w", stdout);

using namespace std;

const double eps = 1e-9;
const ll sz = 2e5 + 5, MOD = 1000000007, inf = LONG_MAX, neg_inf = LONG_MIN;

int dx[] = {0, 1, -1, 0, -1, -1, 1, 1};
int dy[] = {1, 0, 0, -1, 1, -1, 1, -1};
//             r         d        u       l

ll n, pre, last, first = -1, posf = -1, poss = -1;
vll w, seq;
struct x {
    ll node, p1, p2;
};
vector<x> pre_pos;
string s;
map<ll, ll> mp, cur;
bool flag = false;
vector<bool> vis;
vector<vll > adj;

//bool valid(int x, int y) {
//    return x > 0 && x <= n && y > 0 && y <= m && arr[x][y] != '#';
//}

ll get(ll node, ll p1, ll p2) {
    ll sum = 0;
    if (w[node] == last)
        sum++;
    for (auto ch: adj[node]) {
        if (ch != p1 and ch != p2) {
            sum += get(ch, node, p2);
        }
    }
    return sum;
}

ll dfs1(ll node, ll p) {
    if (first == -1 and (w[node] == pre or w[node] == last)) {
        first = w[node];
        posf = node;
    }
    if (first == last and w[node] == pre and poss == -1) {
        poss = node;
    }
    if (first == pre and w[node] == last and poss == -1) {
        poss = node;
    }
    cur[w[node]]++;
    for (auto ch: adj[node]) {
        if (ch != p) {
            if (w[node] == pre) {
                pre_pos.push_back({node, p, ch});
            }
            ll res = dfs1(ch, node);
            if (res)
                return res;
        }
    }
    if (w[node] == pre) {
        pre_pos.push_back({node, p, -1});
    }
    x pos;
    if (pre_pos.size())
        pos = pre_pos.back();
    if (w[node] == pre) {
        while (pre_pos.size() and w[pre_pos.back().node] == pre)
            pre_pos.pop_back();
    }
    if (adj[node].size() == 1 and node != 1) {
        if (mp[last] - cur[last] > 0 and cur[pre]) {
            ll sum = get(pos.node, pos.p1, pos.p2);
            if (sum + cur[last] == mp[last]) {
                cur[w[node]]--;
                return 0;
            } else {
                cur[w[node]]--;
                return pos.node;
            }
        } else {
            cur[w[node]]--;
            return 0;
        }
    }
    return 0;
}

ll dfs2(ll node, ll p) {
    ll ans = 0;
    for (auto ch: adj[node]) {
        if (ch != p) {
            if (w[node] == last and adj[node].size() > 1) {
                ans = ch;
                return ch;
            }
            ans = dfs2(ch, node);
            if (ans)
                return ans;
        }
    }
    return ans;
}

void dfs3(ll node, ll p) {
    if (node == 1) {
        flag = true;
        return;
    }
    seq.push_back(w[node]);
    if (!flag)
        for (auto ch: adj[node]) {
            if (ch != p) {
                dfs3(ch, node);
            }
        }
    if (!flag)
        seq.pop_back();
}

ll dfs4(ll node, ll p, ll target) {
    ll res = 0;
    if (w[node] == target)
        return node;
    for (auto ch: adj[node]) {
        if (ch != p) {
            res = dfs4(ch, node, target);
            if (res)
                return res;
        }
    }
    return res;
}

void clear() {
    first = -1, posf = -1, poss = -1;
    adj.clear();
    flag = false;
    pre_pos.clear();
    mp.clear();
    seq.clear();
    cur.clear();
    vis.clear();
}


void solve() {
    cin >> n;
    adj.resize(n + 1);
    w.resize(n + 1);
    for (int i = 0; i < n; ++i) {
        ll W;
        cin >> W;
        w[i + 1] = W;
        mp[W]++;
    }
    for (int i = 0; i < n - 1; ++i) {
        ll u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    if (mp.size() == 1) {
        cout << 0 << endl;
        clear();
        return;
    }
    auto it = --mp.end();
    last = it->first;
    it--;
    pre = it->first;
    ll ans = dfs1(1, -1);
    if (ans) {
        cout << ans << endl;
        clear();
        return;
    }
    if (first == last) {
        cout << poss << endl;
        clear();
        return;
    }
    ans = dfs2(1, -1);
    if (ans) {
        dfs3(poss, ans);
    } else {
        dfs3(poss, -1);
    }
    for (int i = 1; i < seq.size(); ++i) {
        if (seq[i] > seq[i - 1]) {
            while (seq.size() > i)
                seq.pop_back();
            break;
        }
    }
    map<ll, ll> frq;
    for (int i = 0; i < seq.size(); ++i) {
        frq[seq[i]]++;
    }
    ll mx = 0;
    for (auto it: mp) {
        if (it.second - frq[it.first] > 0)
            mx = it.first;
    }
    sort(all(seq));
    ll res = lower_bound(all(seq), mx) - seq.begin();
    if (res) {
        if (ans) {
            res = dfs4(poss, ans, seq[res - 1]);
        } else {
            res = dfs4(poss, -1, seq[res - 1]);
        }

        cout << res << endl;
    } else
        cout << 0 << endl;
    clear();
}

signed main() {
    IOS
//    FilesIO
    int tc = 1;
    cin >> tc;
    while (tc--) {
        solve();
    }
}
Editor is loading...
Leave a Comment