#include using namespace std; #define DEBUG(x) cerr<<#x<<": "< #define vl vector #define vii vector< vector > #define vll vector< vector > #define vs vector #define pii pair #define pis pair #define psi pair #define pll pair template pair operator+(const pair &s, const pair &t) { return pair(s.first + t.first, s.second + t.second); } template pair operator-(const pair &s, const pair &t) { return pair(s.first - t.first, s.second - t.second); } template ostream& operator<<(ostream& os, pair p) { os << "(" << p.first << ", " << p.second << ")"; return os; } #define X first #define Y second #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--) #define rrep1(i,n) for(int i=(int)(n);i>0;i--) #define REP(i,a,b) for(int i=a;i bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; } #define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end()); const ll inf = 1000000001; const ll INF = (ll)1e18 + 1; const long double pi = 3.1415926535897932384626433832795028841971L; #define Sp(p) cout<> mp(N); // map mp; vi first_par(N, -2); vi gr(N, -2); vii used(N); struct dice { mt19937 mt; dice() : mt(chrono::steady_clock::now().time_since_epoch().count()) {} // [0, x)の一様乱数 ll operator()(ll x) { return this->operator()(0, x); } // [x, y)の一様乱数 ll operator()(ll x, ll y) { uniform_int_distribution dist(x, y - 1); return dist(mt); } vl operator()(int n, ll x, ll y) { vl res(n); for (int i = 0; i < n; i++) res[i] = this->operator()(x, y); return res; } } rnd; int dfs1(int now, int par) { if (mp[now].count(par)) return mp[now][par]; fill(all(used[now]), 0); first_par[now] = par; vi temp; rep (i, G[now].size()) { int v = G[now][i]; if (v == par) continue; int res = dfs1(v, now); if (res < used[now].size()) { used[now][res]++; } } for (int i = 0; i <= N; i++) { if (used[now][i] == 0) { mp[now][par] = i; gr[now] = i; return i; } } assert(false); } int calc(int now, int par) { if (mp[now].count(par)) return mp[now][par]; assert(false); } void dfs2(int now, int par) { if (par != -1) { int x = calc(par, now); if (x < used[now].size()) { used[now][x]++; } for (int i = 0; i <= N; i++) { if (used[now][i] == 0) { mp[now][-1] = i; gr[now] = i; break; } } } for (int v: G[now]) { if (v == par) continue; int x = gr[v]; // if (x < used[now].size()) used[now][x]--; if (x < gr[now] and used[now][x] == 1) { mp[now][v] = x; } else { mp[now][v] = gr[now]; } // for (int i = 0; i <= N; i++) { // if (used[now][i] == 0) { // mp[now][v] = i; // break; // } // } // if (x < used[now].size()) used[now][x]++; } for (int v: G[now]) { if (v == par) continue; dfs2(v, now); } } signed main() { fio(); int n, m; cin >> n >> m; vi a(m); rep (i, m) { cin >> a[i]; a[i]--; } rep (i, n - 1) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } rep (i, n) { used[i].resize(G[i].size() + 1); } // rep (i, n) { // grundy[i].resize(G[i].size(), -1); // } vector gl(m); vi idx(m); rep (i, m) idx[i] = i; std::random_device seed_gen; std::mt19937 engine(seed_gen()); std::shuffle(idx.begin(), idx.end(), engine); dfs1(0, -1); dfs2(0, -1); int x = 0; rep (i, m) { int j = idx[i]; gl[j] = calc(a[j], -1); x ^= gl[j]; } if (x == 0) { cout << -1 << " " << -1 << endl; return 0; } rep (i, m) { int y = gl[i]; rep (j, G[a[i]].size()) { int v = G[a[i]][j]; // int par_idx = mp[v][a[i]]; // DEBUG(par_idx); // int z = grundy[v][par_idx]; int z = calc(v, a[i]); if ((x ^ y ^ z) == 0) { cout << i + 1 << " " << v + 1 << endl; return 0; } } } }