#include using namespace std; typedef long long ll; typedef long double ld; void submit(){ freopen( "bochi.in", "r", stdin ); freopen( "bochi.out", "w", stdout ); } struct Edge { int to; int w; }; const int NMAX = 200005; int N, M; vector adj[NMAX]; // d[u] = sum of weights on edges incident to u. ld d[NMAX]; ld S = 0; // For LCA: int parent[20][NMAX]; // parent[k][u]: the 2^k-th ancestor of u int depth[NMAX]; // We'll root the tree at 1. // For DFS, also store: sub[u] = sum_{x in subtree(u)} d[x] ld sub[NMAX]; // F_A and F_B values: ld FA[NMAX], FB[NMAX]; void dfs(int u, int par) { parent[0][u] = par; depth[u] = (par == -1 ? 0 : depth[par] + 1); sub[u] = d[u]; for(auto &edge : adj[u]){ int v = edge.to; if(v == par) continue; dfs(v, u); sub[u] += sub[v]; } } // After DFS we compute FA and FB for non-root nodes. // For an edge from p to u with weight w, define: /// fA(u) = sub[u] / w, and fB(u) = (S - sub[u]) / w. // Then, FA[u] = FA[p] + fA(u) and FB[u] = FB[p] + fB(u]. void dfs2(int u, int par) { for(auto &edge : adj[u]){ int v = edge.to; if(v == par) continue; ld w = edge.w; ld fA = sub[v] / w; ld fB = (S - sub[v]) / w; FA[v] = FA[u] + fA; FB[v] = FB[u] + fB; dfs2(v, u); } } // Preprocess LCA: void buildLCA(int n) { for(int k = 1; k < 20; k++){ for (int i = 1; i <= n; i++){ if(parent[k-1][i] == -1) parent[k][i] = -1; else parent[k][i] = parent[k-1][ parent[k-1][i] ]; } } } int LCA(int u, int v) { if(depth[u] < depth[v]) swap(u,v); int dDiff = depth[u] - depth[v]; for(int k = 0; k < 20; k++){ if(dDiff & (1 << k)) u = parent[k][u]; } if(u == v) return u; for(int k = 19; k >= 0; k--){ if(parent[k][u] != parent[k][v]){ u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } // Main function – process input and queries int main(){ submit(); ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> M; for (int i = 1; i <= N; i++){ adj[i].clear(); d[i] = 0; } // Read tree edges for (int i = 1; i <= N-1; i++){ int u, v, w; cin >> u >> v >> w; adj[u].push_back({v, w}); adj[v].push_back({u, w}); d[u] += w; d[v] += w; } // Compute S = sum_{u} d[u] for (int i = 1; i <= N; i++){ S += d[i]; } // Root the tree arbitrarily at 1. dfs(1, -1); // Initialize FA and FB for root FA[1] = 0; FB[1] = 0; dfs2(1, -1); // Build LCA table. for (int i = 1; i <= N; i++){ if(parent[0][i] == -1) depth[i] = 0; } buildLCA(N); // Process queries. // For a query (a, b): let L = LCA(a, b) // Then H*(a,b) = (FA[a] - FA[L]) + (FB[b] - FB[L]) // and answer = H*(a,b) + S/d[b]. cout << fixed << setprecision(6); for (int i = 0; i < M; i++){ int a, b; cin >> a >> b; int l = LCA(a, b); ld hit = (FA[a] - FA[l]) + (FB[b] - FB[l]); ld ans = hit + (S / d[b]); cout << (long double)ans << "\n"; } return 0; }