#include<bits/stdc++.h>
using namespace std;
#define I inline
#define int long long
#define gc getchar
const int N = 3010;
namespace SlowIO {
const int SZ = (1 << 20) + 1;
char buf[SZ], *p1 = buf, *p2 = buf;
char buffer[SZ];
int op1 = -1;
const int op2 = SZ - 1;
I char gc() {
if(p1 != p2) return *p1++;
return p1 == (p2 = (p1 = buf) + fread(buf, 1, SZ - 1, stdin)) ? EOF : *p1++;
}
I void flush() {
fwrite(buffer, 1, op1 + 1, stdout);
op1 = -1;
}
I void pc(const char &x) {
if(op1 == op2) flush();
buffer[++op1] = x;
}
I int read() {
int x = 0, f = 1; char ch = gc();
while(ch < '0' || ch > '9') {if(ch == '-') f = -f; ch = gc();}
while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = gc();
return x * f;
}
I void write(int x) {
if(x < 0) pc('-'), x = -x;
char s[25];
int tot = 0;
while(x || !tot) {
s[tot++] = x % 10 + 48;
x/=10;
}
while(tot--) pc(s[tot]);
}
} using namespace SlowIO;
int h[N], to[N * 2], nxt[N * 2], idx;
void add(int a, int b) {
to[idx] = b;
nxt[idx] = h[a];
h[a] = idx++;
}
int dp[N][14];
int dep[N], n;
void dfs(int u, int fa) {
dp[u][0] = fa;
dep[u] = dep[fa] + 1;
for(int i = h[u]; i != -1; i = nxt[i]) {
int v = to[i];
if(v == fa) continue;
dfs(v, u);
}
}
void ST() {
for(int i = 1; i < 14; i++)
for(int j = 1; j <= n; j++)
dp[j][i] = dp[dp[j][i - 1]][i - 1];
}
int lca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
int det = dep[x] - dep[y];
for(int i = 0; i < 14; i++) if(det & (1 << i)) x = dp[x][i];
if(x == y) return x;
for(int i = 13; i >= 0; i--) {
if(dp[x][i] != dp[y][i]) {
x = dp[x][i], y = dp[y][i];
}
}
return dp[x][0];
}
int d1[N], d2[N];
int x, y, g;
void exgcd(int a, int b) {
if(!b) {x = 1; y = 0; return ;}
exgcd(b, a % b); int tx = x; x = y; y = tx - a / b * y;
}
signed main() {
int T = read();
while(T--) {
memset(h, -1, sizeof h); idx = 0;
n = read(); int q = read();
for(int i = 1; i < n; i++) {
int a = read(), b = read();
add(a, b); add(b, a);
}
dfs(1, 0); ST();
while(q--) {
int sa = read(), ta = read(), sb = read(), tb = read();
if(sa == sb) {
write(sa); pc('\n');
continue;
}
for(int i = 1; i <= n; i++) d1[i] = d2[i] = -1;
int la = lca(sa, ta), lb = lca(sb, tb);
int len1 = dep[sa] + dep[ta] - 2 * dep[la];
int len2 = dep[sb] + dep[tb] - 2 * dep[lb];
len1 <<= 1; len2 <<= 1;
g = __gcd(len1, len2);
int lcm = len1 / g * len2;
int a = len1 / g, b = len2 / g;
d1[sa] = 0; d2[sb] = 0;
for(int i = sa; i != la; i = dp[i][0]) d1[dp[i][0]] = d1[i] + 1;
for(int i = ta; i != la; i = dp[i][0]) d1[i] = d1[la] + dep[i] - dep[la];
for(int i = sb; i != lb; i = dp[i][0]) d2[dp[i][0]] = d2[i] + 1;
for(int i = tb; i != lb; i = dp[i][0]) d2[i] = d2[lb] + dep[i] - dep[lb];
int mi = 2e18, pos = -1;
x = y = 0;
exgcd(a, b);
int x0 = x, y0 = y;
for(int i = 1; i <= n; i++) if(~d1[i] && ~d2[i]) {
if((d2[i] - d1[i]) % g) continue;
int c = (d2[i] - d1[i]) / g;
x = x0 * c; y = y0 * c;
if(x < 0) {
int k = (-x + b - 1) / b;
x += b * k;
y -= a * k;
}
if(y > 0) {
int k = (y + a - 1) / a;
y -= a * k;
x += b * k;
}
int step = (x * len1 + d1[i]) % lcm;
if(step < mi) {
pos = i;
mi = step;
}
}
for(int i = 1; i <= n; i++) if(~d1[i]) d1[i] = len1 - d1[i];
for(int i = 1; i <= n; i++) if(~d1[i] && ~d2[i]) {
if((d2[i] - d1[i]) % g) continue;
x = y = 0;
exgcd(a, b);
int c = (d2[i] - d1[i]) / g;
x = x0 * c; y = y0 * c;
if(x < 0) {
int k = (-x + b - 1) / b;
x += b * k;
y -= a * k;
}
if(y > 0) {
int k = (y + a - 1) / a;
y -= a * k;
x += b * k;
}
int step = (x * len1 + d1[i]) % lcm;
if(step < mi) {
pos = i;
mi = step;
}
}
for(int i = 1; i <= n; i++) {
if(~d1[i]) d1[i] = len1 - d1[i];
if(~d2[i]) d2[i] = len2 - d2[i];
}
for(int i = 1; i <= n; i++) if(~d1[i] && ~d2[i]) {
if((d2[i] - d1[i]) % g) continue;
int c = (d2[i] - d1[i]) / g;
x = x0 * c; y = y0 * c;
if(x < 0) {
int k = (-x + b - 1) / b;
x += b * k;
y -= a * k;
}
if(y > 0) {
int k = (y + a - 1) / a;
y -= a * k;
x += b * k;
}
int step = (x * len1 + d1[i]) % lcm;
if(step < mi) {
pos = i;
mi = step;
}
}
for(int i = 1; i <= n; i++) if(~d1[i]) d1[i] = len1 - d1[i];
for(int i = 1; i <= n; i++) if(~d1[i] && ~d2[i]) {
if((d2[i] - d1[i]) % g) continue;
int a = len1 / g, b = len2 / g;
int c = (d2[i] - d1[i]) / g;
x = x0 * c; y = y0 * c;
if(x < 0) {
int k = (-x + b - 1) / b;
x += b * k;
y -= a * k;
}
if(y > 0) {
int k = (y + a - 1) / a;
y -= a * k;
x += b * k;
}
int step = (x * len1 + d1[i]) % lcm;
if(step < mi) {
pos = i;
mi = step;
}
}
write(pos); pc('\n');
}
}
flush();
return 0;
}剪贴板
剪贴板 wqy0s1cq
剪贴板信息
剪贴板信息
- 发布
- 收录
- 更新