1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 typedef long long LL; 7 8 const int MAXC = 10000; 9 const int MAXN = 100010; 10 const int MAXV = 2 * 100010; 11 const int MAXE = 2 * 200010; 12 13 int head[MAXV], fa[MAXV]; 14 bool vis[MAXV]; 15 int to[MAXE], next[MAXE], weight[MAXE]; 16 int T, n, m, ecnt; 17 18 void init() { 19 memset(head, 0, sizeof(head)); 20 memset(vis, 0, sizeof(vis)); 21 for(int i = 1; i <= n; ++i) fa[i] = i; 22 ecnt = 2; 23 } 24 25 void add_edge(int u, int v, int c) { 26 to[ecnt] = v; weight[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++; 27 to[ecnt] = u; weight[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++; 28 } 29 30 struct QUERY { 31 int s, t, k, a, b, lca; 32 void read(int i) { 33 scanf("%d%d%d%d%d", &s, &t, &k, &a, &b); 34 add_edge(s + n, t + n, i); 35 } 36 } Query[MAXV]; 37 38 int get_set(int x) { 39 return fa[x] == x ? x : fa[x] = get_set(fa[x]); 40 } 41 42 void dfs_lca(int u, int f) { 43 for(int p = head[u]; p; p = next[p]) { 44 int &v = to[p]; 45 if(v == f) continue; 46 dfs_lca(v, u); 47 fa[v] = u; 48 } 49 vis[u] = true; 50 for(int p = head[u + n]; p; p = next[p]) { 51 int v = to[p] - n; 52 if(vis[v]) Query[weight[p]].lca = get_set(v); 53 } 54 } 55 56 struct Node { 57 int L, R, sum, cnt; 58 } tree[20 * MAXN]; 59 int root[MAXV], Tcnt; 60 61 void init_Tree() { 62 root[1] = 0; 63 Tcnt = 1; 64 } 65 66 void insert(int &x, int left, int right, int &val) { 67 tree[Tcnt] = tree[x]; x = Tcnt++; 68 ++tree[x].cnt; tree[x].sum += val; 69 if(left == right) return ; 70 int mid = (left + right) >> 1; 71 if(val <= mid) insert(tree[x].L, left, mid, val); 72 else insert(tree[x].R, mid + 1, right, val); 73 } 74 75 void dfs_build_tree(int u, int f, int w) { 76 root[u] = root[f]; 77 if(f) insert(root[u], 0, MAXC, w); 78 for(int p = head[u]; p; p = next[p]) { 79 int &v = to[p]; 80 if(v == f) continue; 81 dfs_build_tree(v, u, weight[p]); 82 } 83 } 84 85 int query(int x, int y, int lca, int left, int right, int k) { 86 if(left == right) return left; 87 int t = tree[tree[x].L].cnt + tree[tree[y].L].cnt - 2 * tree[tree[lca].L].cnt; 88 int mid = (left + right) >> 1; 89 if(k <= t) return query(tree[x].L, tree[y].L, tree[lca].L, left, mid, k); 90 else return query(tree[x].R, tree[y].R, tree[lca].R, mid + 1, right, k - t); 91 } 92 93 int query(int x, int y, int lca, int cap) { 94 int l = 1, r = MAXC, cnt = 0, sum = 0; 95 while(l < r) { 96 int mid = (l + r) >> 1; 97 int tmp_cnt = tree[tree[x].L].cnt + tree[tree[y].L].cnt - 2 * tree[tree[lca].L].cnt; 98 int tmp_sum = tree[tree[x].L].sum + tree[tree[y].L].sum - 2 * tree[tree[lca].L].sum; 99 if((cnt + tmp_cnt) * mid - (sum + tmp_sum) > cap) 100 x = tree[x].L, y = tree[y].L, lca = tree[lca].L, r = mid; 101 else x = tree[x].R, y = tree[y].R, lca = tree[lca].R, l = mid + 1, cnt += tmp_cnt, sum += tmp_sum; 102 } 103 return l - 1; 104 } 105 106 int main() { 107 scanf("%d", &T); 108 for(int t = 1; t <= T; ++t) { 109 scanf("%d%d", &n, &m); 110 init(); 111 for(int i = 1; i < n; ++i) { 112 int u, v, c; 113 scanf("%d%d%d", &u, &v, &c); 114 add_edge(u, v, c); 115 } 116 for(int i = 1; i <= m; ++i) Query[i].read(i); 117 printf("Case #%d:\n", t); 118 dfs_lca(1, 0); 119 //for(int i = 1; i <= m; ++i) printf("%d\n", Query[i].lca); 120 init_Tree(); 121 dfs_build_tree(1, 0, 0); 122 //for(int i = 1; i <= m; ++i) printf("%d\n", query(root[Query[i].s], root[Query[i].t], root[Query[i].lca], 0, MAXC, 1)); 123 for(int i = 1; i <= m; ++i) { 124 int ans = query(root[Query[i].s], root[Query[i].t], root[Query[i].lca], 0, MAXC, 1); 125 if(Query[i].a < Query[i].b) ans += Query[i].k / Query[i].a; 126 else { 127 if(Query[i].k >= Query[i].a) ans += (Query[i].k - Query[i].a) / Query[i].b + 1; 128 ans = max(ans, query(root[Query[i].s], root[Query[i].t], root[Query[i].lca], Query[i].k / Query[i].b)); 129 } 130 printf("%d\n", ans); 131 } 132 } 133 }