1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int N = 2e5 + 10, Log = 12, inf = 0x3f3f3f3f;
#define gc()(is==it?it=(is=in)+fread(in,1,Q,stdin),(is==it?EOF:*is++):*is++) const int Q=(1<<24)+1; char in[Q],*is=in,*it=in,c; void read(long long &n){ for(n=0;(c=gc())<'0'||c>'9';); for(;c<='9'&&c>='0';c=gc())n=n*10+c-48; } void read(int &n){ for(n=0;(c=gc())<'0'||c>'9';); for(;c<='9'&&c>='0';c=gc())n=n*10+c-48; }
int exgcd(int a, int b, int &x, int &y){ if(!b){ x = 1; y = 0; return a; } int g = exgcd(b, a % b, x, y); int t = x; x = y; y = t - a / b * y; return g; }
vector<int> e[N]; int anc[N][Log + 5], depth[N];
void dfs(int u, int fa){ anc[u][0] = fa; depth[u] = depth[fa] + 1; for(int v : e[u]){ if(v == fa) continue; dfs(v, u); } }
void init(int root, int n){ depth[0] = 0; dfs(root, 0); for(int j = 1; j <= Log; j++){ for(int i = 1; i <= n; i++){ anc[i][j] = anc[anc[i][j - 1]][j - 1]; } } }
int rush(int u, int h){ for(int i = 0; i < Log; i++){ if(h >> i & 1) u = anc[u][i]; } return u; }
int qry(int u, int v){ if(depth[u] < depth[v]) swap(u, v); u = rush(u, depth[u] - depth[v]); if(u == v) return u; for(int i = Log; i >= 0; i--){ if(anc[u][i] != anc[v][i]){ u = anc[u][i]; v = anc[v][i]; } } return anc[u][0]; }
int dfn_a[N], dfn_b[N];
void gao(int u, int fa, int l, int step, int t[]){ while(u != fa){ t[u] = l; l += step; u = anc[u][0]; } t[u] = l; }
int upper(int m, int n){ if(m <= 0) return m / n; return (m - 1) / n + 1; }
int lcmp;
int calc(int x1, int a, int y1, int b, int g, int x, int y, int now){ int cx = y1 - x1; if(cx % g) return -1; cx /= g; x *= cx; int t = upper(now - x1 - x * a, lcmp); int ans = x1 + x * a + t * lcmp;
return ans; }
void get_dfn(int u, int v, int dfn[], int &len){ int lca = qry(u, v); len = depth[u] + depth[v] - depth[lca] * 2 + 1; gao(u, lca, 1, 1, dfn); gao(v, lca, len, -1, dfn); }
int run(int len1, int len2, int n){ int x, y, a = (len1 - 1) * 2, b = (len2 - 1) * 2; int g = exgcd(a, b, x, y); lcmp = a * b / g; int now = 1, ans = inf, location = -1; for(int i = 1; i <= n; i++){ if(!dfn_a[i] || !dfn_b[i]) continue; vector<int> tx{dfn_a[i], dfn_a[i] + (len1 - dfn_a[i]) * 2}; vector<int> ty{dfn_b[i], dfn_b[i] + (len2 - dfn_b[i]) * 2}; for(int x1 : tx){ for(int y1 : ty){ int res = calc(x1, a, y1, b, g, x, y, max(x1, y1)); if(res != -1){ if(res < ans){ ans = res; location = i; } } } } } return location; }
void kaibai() { int n, m; read(n); read(m); for(int i = 1, u, v; i < n; i++){ read(u); read(v); e[u].push_back(v); e[v].push_back(u); } init(1, n); while(m--){ for(int i = 1; i <= n; i++) dfn_a[i] = dfn_b[i] = 0; int sa, ta, sb, tb, len1, len2; read(sa);read(ta); read(sb);read(tb); get_dfn(sa, ta, dfn_a, len1); get_dfn(sb, tb, dfn_b, len2); int ans = run(len1, len2, n); cout << ans << '\n'; } for(int i = 1; i <= n; i++){ e[i].clear(); } }
int main(void) { int T = 1; read(T); while (T--) { kaibai(); } }
|