import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } int N; int[] A, B; int[][] G; int[] par; int[] dp, DP; void dfs(int u, int p) { par[u] = p; // init const lim = cast(int)(G[u].length) + 2; auto cnt = new int[lim]; foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (v != p) { dfs(v, u); } } foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (v != p) { // add dp[v] if (dp[v] < lim) { ++cnt[dp[v]]; } } } // calc dp[u] foreach (g; 0 .. lim) { if (cnt[g] == 0) { dp[u] = g; break; } } } void DFS(int u, int p) { // init const lim = cast(int)(G[u].length + 2); auto cnt = new int[lim]; foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (v != p) { // add dp[v] if (dp[v] < lim) { ++cnt[dp[v]]; } } } if (p != -1) { // add DP[u] if (DP[u] < lim) { ++cnt[DP[u]]; } } int g0; foreach (g; 0 .. lim) { if (cnt[g] == 0) { g0 = g; break; } } foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (v != p) { // calc DP[v], removing dp[v] DP[v] = g0; if (dp[v] < lim) { --cnt[dp[v]]; if (cnt[dp[v]] == 0) { chmin(DP[v], dp[v]); } ++cnt[dp[v]]; } } } foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (v != p) { DFS(v, u); } } } void main() { try { for (; ; ) { N = readInt(); const M = readInt(); auto C = new int[M]; foreach (j; 0 .. M) { C[j] = readInt() - 1; } A = new int[N - 1]; B = new int[N - 1]; foreach (i; 0 .. N - 1) { A[i] = readInt() - 1; B[i] = readInt() - 1; } G = new int[][N]; foreach (i; 0 .. N - 1) { G[A[i]] ~= i; G[B[i]] ~= i; } par = new int[N]; dp = new int[N]; DP = new int[N]; par[] = -1; dp[] = -1; DP[] = -1; const rt = 0; dfs(rt, -1); DFS(rt, -1); debug { writeln("rt = ", rt); writeln("dp = ", dp); writeln("DP = ", DP); } auto gs = new int[N]; gs[] = -1; foreach (u; 0 .. N) { // init const lim = cast(int)(G[u].length) + 2; auto cnt = new int[lim]; foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (v != par[u]) { // add dp[v] if (dp[v] < lim) { ++cnt[dp[v]]; } } } if (u != rt) { // add DP[u] if (DP[u] < lim) { ++cnt[DP[u]]; } } // calc ans, rooted at u foreach (g; 0 .. lim) { if (cnt[g] == 0) { gs[u] = g; break; } } } debug { writeln("gs = ", gs); } int sum; foreach (j; 0 .. M) { sum ^= gs[C[j]]; } if (sum) { int jm = -1, vm = -1; foreach (j; 0 .. M) { const target = sum ^ gs[C[j]]; if (target < gs[C[j]]) { jm = j; const u = C[j]; foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (v != par[u]) { // add dp[v] if (dp[v] == target) { vm = v; goto found; } } } if (u != rt) { // add DP[u] if (DP[u] == target) { vm = par[u]; goto found; } } } } found: writeln(jm + 1, " ", vm + 1); } else { writeln("-1 -1"); } } } catch (EOFException e) { } }