#include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector &vec, int len) { vec.resize(len); } template void ndarray(vector &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template void ndfill(V &x, const T &val) { x = val; } template void ndfill(vector &vec, const T &val) { for (auto &v : vec) ndfill(v, val); } template bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template vector srtunq(vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template istream &operator>>(istream &is, vector &vec) { for (auto &v : vec) is >> v; return is; } template istream &operator>>(istream &is, tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template ostream &operator<<(ostream &os, const vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const tuple &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; } template ostream &operator<<(ostream &os, const deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const pair &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template ostream &operator<<(ostream &os, const map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl #else #define dbg(x) #endif int mex(vector v) { sort(v.begin(), v.end()); int ret = 0; for (auto x : v) { if (x == ret) ret++; if (x > ret) break; } return ret; } struct rand_int_ { using lint = long long; mt19937 mt; rand_int_() : mt(chrono::steady_clock::now().time_since_epoch().count()) {} lint operator()(lint x) { return this->operator()(0, x); } // [0, x) lint operator()(lint l, lint r) { uniform_int_distribution d(l, r - 1); return d(mt); } } rnd; int main() { int N, M; cin >> N >> M; vector A(M); cin >> A; for (auto &a : A) a--; vector> to(N); int root = rnd(N); REP(_, N - 1) { int u, v; cin >> u >> v; u--, v--; to[u].emplace_back(v); to[v].emplace_back(u); } vector gchild(N), gpar(N, 1e9), grundy(N), par(N, -1); auto gdfs = [&](auto &&gdfs, int now) -> void { vector gs; for (auto nxt : to[now]) if (nxt != par[now]) { par[nxt] = now; gdfs(gdfs, nxt); gs.emplace_back(gchild[nxt]); } gchild[now] = mex(gs); }; gdfs(gdfs, root); auto pardfs = [&](auto &&pardfs, int now, int prv) -> void { vector cnt(to[now].size() + 3); cnt[min(cnt.size() - 1, gpar[now])]++; for (auto nxt : to[now]) if (nxt != prv) { int g = min(cnt.size() - 1, gchild[nxt]); cnt[g]++; } set ok; REP(i, cnt.size()) if (!cnt[i]) ok.insert(i); dbg(now); dbg(to[now]); dbg(cnt); dbg(ok); grundy[now] = *ok.begin(); for (auto nxt : to[now]) if (nxt != prv) { int g = min(cnt.size() - 1, gchild[nxt]); cnt[g]--; if (!cnt[g]) ok.insert(g); gpar[nxt] = *ok.begin(); cnt[g]++; ok.erase(g); } for (auto nxt : to[now]) if (nxt != prv) pardfs(pardfs, nxt, now); }; pardfs(pardfs, root, -1); dbg(gchild); dbg(gpar); dbg(grundy); int XOR = 0; for (auto a : A) { XOR ^= grundy[a]; } if (XOR) { vector checked(N); REP(i, A.size()) { int r = A[i]; if (checked[r]) continue; checked[r] = 1; for (auto s : to[r]) { if (par[r] == s and (grundy[r] ^ gpar[r]) == XOR) { cout << i + 1 << ' ' << s + 1 << '\n'; return 0; } if (par[r] != s and (grundy[r] ^ gchild[s]) == XOR) { cout << i + 1 << ' ' << s + 1 << '\n'; return 0; } } } } else puts("-1 -1"); }