#include #include #include #include #define MAX_N 2000 int n, d; std::vector graph[MAX_N]; int v1s[MAX_N], v2s[MAX_N]; std::vector< std::pair > demand[MAX_N]; bool color[MAX_N]; uint64_t weights[MAX_N]; uint64_t weight_sum; void add_edge(int v1, int v2) { graph[v1].push_back(v2); graph[v2].push_back(v1); } void remove_edge(int v1, int v2) { auto i1 = std::remove(graph[v1].begin(), graph[v1].end(), v2); graph[v1].erase(i1, graph[v1].end()); auto i2 = std::remove(graph[v2].begin(), graph[v2].end(), v1); graph[v2].erase(i2, graph[v2].end()); } void color_dfs(int v) { color[v] = true; for (int next : graph[v]) { if (!color[next]) color_dfs(next); } } void count_dfs(int v, int p, int length) { weight_sum += length * weights[v]; for (int next : graph[v]) { if (next != p) { count_dfs(next, v, length + 1); } } } uint64_t evaluate_graph() { weight_sum = 0; for (int i = 0; i < n; ++i) { for (auto d : demand[i]) { weights[d.first] += d.second; } count_dfs(i, -1, 0); for (auto d : demand[i]) { weights[d.first] -= d.second; } } return weight_sum / 2; } int main() { scanf("%d", &n); for (int i = 1; i < n; ++i) { int v1, v2; scanf("%d%d", &v1, &v2); v1s[i] = --v1; v2s[i] = --v2; add_edge(v1, v2); } scanf("%d", &d); for (int i = 0; i < d; ++i) { int v1, v2, cost; scanf("%d%d%d", &v1, &v2, &cost); --v1, --v2; demand[v1].emplace_back(v2, cost); demand[v2].emplace_back(v1, cost); } for (int i = 1; i < n; ++i) { remove_edge(v1s[i], v2s[i]); for (int j = 0; j < n; ++j) color[j] = false; color_dfs(0); uint64_t best_cost = -1; int u1 = -1, u2 = -1; for (int v1 = 0; v1 < n; ++v1) { if (graph[v1].size() == 3) continue; for (int v2 = v1 + 1; v2 < n; ++v2) { if (graph[v2].size() == 3) continue; if (color[v1] == color[v2]) continue; add_edge(v1, v2); uint64_t cost = evaluate_graph(); if (best_cost == -1 || cost < best_cost) { best_cost = cost; u1 = v1; u2 = v2; } remove_edge(v1, v2); } } add_edge(u1, u2); printf("%d %d\n", u1 + 1, u2 + 1); } return 0; }