#include #include #include #include #include #pragma warning(disable:4996) #define INF 2140000000 using namespace std; vector > > g; // to, edgeid vector parent; // parent[i]: parent of vertex i vector val; // val[2*i]:val for downward dir, val[2*i+1]: val for upward dir vector > z; // z[i]: vals conter for directed edges from vertex i vector ans; // ans[i]: Grundy number for vertex i int dfs(int par, int curr) { parent[curr] = par; int i; for (i = 0; i < (int)g[curr].size(); i++) { int next = g[curr][i].first; int edge = g[curr][i].second; if (next == par) continue; int tmp = dfs(curr, next); val[edge * 2] = tmp; z[curr][tmp]++; } for (i = 0; i < INF; i++) { if (z[curr].empty() || z[curr].find(i) == z[curr].end()) { break; } } return i; } void dfs2(int par, int curr) { int i; for (i = 0; i < (int)g[curr].size(); i++) { int next = g[curr][i].first; int edge = g[curr][i].second; if (next == par) continue; int k; for (k = 0; k < INF; k++) { if (z[curr].empty()) { break; } auto it = z[curr].find(k); if (it == z[curr].end()) { break; } int num=it->second; if (val[edge*2]==k) num--; if (num < 1) { break; } } val[edge * 2 + 1] = k; z[next][k]++; dfs2(curr, next); } for (i = 0; i < INF; i++) { if (z[curr].empty() || z[curr].find(i) == z[curr].end()) { break; } } ans[curr] = i; return; } void solve() { int n, m; scanf("%d%d", &n, &m); vector a(m); int i; for (i = 0; i < m; i++) { scanf("%d", &a[i]); a[i]--; } g.resize(n); parent.resize(n); z.resize(n); ans.resize(n); val.resize((n-1) * 2); for (i = 0; i < n - 1; i++) { int p, q; scanf("%d%d", &p, &q); p--; q--; g[p].push_back(make_pair(q, i)); g[q].push_back(make_pair(p, i)); } dfs(-1, 0); dfs2(-1, 0); int sum = 0; for (i = 0; i < m; i++) { sum ^= ans[a[i]]; } if (sum == 0) { printf("-1 -1\n"); } else { int max = -1; int maxi = -1; for (i = 0; i < m; i++) { int tmp = ans[a[i]] & sum; if (max < tmp) { max = tmp; maxi = i; } } int target = ans[a[maxi]]^sum; int nextvtx = -1; for (i = 0; i < (int)g[a[maxi]].size(); i++) { int next = g[a[maxi]][i].first; int edge = g[a[maxi]][i].second; int tmpval = (next == parent[a[maxi]] ? val[2 * edge + 1] : val[2 * edge]); if (target == tmpval) { nextvtx = next; break; } } printf("%d %d\n", maxi + 1, nextvtx + 1); } return; } int main() { solve(); return 0; }