#include #define REP_(i, a_, b_, a, b, ...) \ for (int i = (a), END_##i = (b); i < END_##i; ++i) #define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) #define ALL(x) std::begin(x), std::end(x) using i64 = long long; using u64 = unsigned long long; template inline bool chmax(T &a, U b) { return a < b and ((a = std::move(b)), true); } template inline bool chmin(T &a, U b) { return a > b and ((a = std::move(b)), true); } template inline int ssize(const T &a) { return (int)std::size(a); } template std::istream &operator>>(std::istream &is, std::vector &a) { for (auto &x : a) is >> x; return is; } template std::ostream &operator<<(std::ostream &os, const std::pair &a) { return os << "(" << a.first << ", " << a.second << ")"; } template std::ostream &print_seq(const Container &a, std::string_view sep = " ", std::string_view ends = "\n", std::ostream &os = std::cout) { auto b = std::begin(a), e = std::end(a); for (auto it = std::begin(a); it != e; ++it) { if (it != b) os << sep; os << *it; } return os << ends; } template struct is_iterable : std::false_type {}; template struct is_iterable())), decltype(std::end(std::declval()))>> : std::true_type {}; template ::value && !std::is_same::value && !std::is_same::value>> std::ostream &operator<<(std::ostream &os, const T &a) { return print_seq(a, ", ", "", (os << "{")) << "}"; } void print() { std::cout << "\n"; } template void print(const T &x) { std::cout << x << "\n"; } template void print(const Head &head, Tail... tail) { std::cout << head << " "; print(tail...); } void read_from_cin() {} template void read_from_cin(T &value, Ts &...args) { std::cin >> value; read_from_cin(args...); } #define VAR(type, ...) \ type __VA_ARGS__; \ read_from_cin(__VA_ARGS__); #ifdef ENABLE_DEBUG #include "debug_dump.hpp" #else #define DUMP(...) #endif using namespace std; // Most Significant Set Bit (Highest One Bit) = std::bit_floor(x) int mssb_pos(unsigned x) { static const int CLZ_WIDTH = __builtin_clz(1); assert(x != 0); return CLZ_WIDTH - __builtin_clz(x); } int mssb_pos(u64 x) { static const int CLZLL_WIDTH = __builtin_clzll(1LL); assert(x != 0); return CLZLL_WIDTH - __builtin_clzll(x); } template inline U bit_floor(U x) { if (x == 0) return 0; return U(1) << mssb_pos(x); } struct Edge { int to; }; template class InplaceRerooting { private: using NV = typename Task::NodeValue; using EV = typename Task::EdgeValue; Task task; int n; // number of nodes std::vector> g; // graph (tree) std::vector sub; // values for each subtree rooted at i std::vector full; // values for each entire tree rooted at i int base_root; // base root node where we start DFS public: explicit InplaceRerooting(Task task, std::vector> g, int r = 0) : task(move(task)), n((int)g.size()), g(move(g)), sub(n), full(n), base_root(r) {} const std::vector &run() { pull_up(base_root, -1); push_down(base_root, -1, std::nullopt); return full; } private: NV pull_up(int v, int par) { EV res = task.id(); for (auto &e : g[v]) { int u = e.to; if (u == par) continue; auto sub = task.add_edge(pull_up(u, v), e); task.merge_inplace(res, std::move(sub)); } return (sub[v] = task.add_node(res, v)); } void push_down(int v, int par, std::optional upper_sub) { int m = g[v].size(); EV agg = task.id(); for (int i = 0; i < m; ++i) { auto &e = g[v][i]; int u = e.to; if (u == par) { assert(upper_sub.has_value()); task.merge_inplace(agg, task.add_edge(std::move(*upper_sub), e)); } else { task.merge_inplace(agg, task.add_edge(sub[u], e)); } } full[v] = task.add_node(agg, v); for (int i = 0; i < m; ++i) { auto &e = g[v][i]; int u = e.to; if (u == par) continue; EV edge_value = task.add_edge(sub[u], e); task.subtract_inplace(agg, edge_value); push_down(u, v, std::optional{task.add_node(agg, v)}); task.merge_inplace(agg, std::move(edge_value)); } } }; struct InplaceTask { struct NodeValue { unsigned nimber; map> nexts; }; struct EdgeValue { map> nimbers; }; EdgeValue id() const { return {}; } NodeValue add_node(const EdgeValue &e, int node) const { NodeValue res; unsigned mex = 0; for (auto &[g, tos] : e.nimbers) { if (g == mex) { auto &nex = res.nexts[g]; for (auto &to : tos) { if (ssize(nex) >= 2) break; nex.insert(to); } mex++; } else if (g > mex) { break; } } res.nimber = mex; return res; } EdgeValue add_edge(const NodeValue &val, const Edge &edge) const { EdgeValue res; auto it = res.nimbers.find(val.nimber); if (it == res.nimbers.end() or ssize(it->second) < 2) { res.nimbers[val.nimber].insert(edge.to); } return res; } void merge_inplace(EdgeValue &agg, const EdgeValue &x) const { for (auto &[g, tos] : x.nimbers) { agg.nimbers[g].insert(ALL(tos)); auto &nex = agg.nimbers[g]; for (auto &to : tos) { if (ssize(nex) >= 2) break; nex.insert(to); } } } void subtract_inplace(EdgeValue &agg, const EdgeValue &x) const { for (auto &[g, tos] : x.nimbers) { auto &nimg = agg.nimbers[g]; for (auto to : tos) { nimg.erase(to); if (nimg.empty()) { agg.nimbers.erase(g); } } } } }; pair solve() { int n, m; cin >> n >> m; vector a(m); REP(i, m) { cin >> a[i]; --a[i]; } vector> g(n); REP(i, n - 1) { int u, v; cin >> u >> v; --u, --v; g[u].push_back({v}); g[v].push_back({u}); } InplaceTask task; InplaceRerooting solver(task, g); auto res = solver.run(); unsigned ag = 0; REP(i, m) { const auto &node = res[a[i]]; ag ^= node.nimber; } if (ag == 0) { return {-1, -1}; } unsigned ag_mssb = bit_floor(ag); int mi = -1; REP(i, m) { const auto &node = res[a[i]]; if (node.nimber & ag_mssb) { mi = i; break; } } DUMP(mi); assert(mi >= 0); const auto &nv = res[a.at(mi)]; unsigned tg = (nv.nimber ^ ag); auto it = nv.nexts.find(tg); assert(it != nv.nexts.end()); assert(not it->second.empty()); int to = *it->second.begin(); return {mi + 1, to + 1}; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); auto [i, v] = solve(); print(i, v); }