// Ahhh, unfortunately of fortunately machines are getting too smart! // This solution was generated by chatGPT-o1 // https://chatgpt.com/share/66e62b88-ce90-800d-a76b-2fa729d6480a #include #include #include using namespace std; typedef long long ll; const int MAXN = 100005; int n; int colors[MAXN]; vector> adj[MAXN]; // adj[u] = list of {v, edge_index} ll ans[MAXN]; // ans[edge_index] ll total_counts[MAXN]; unordered_map dfs(int u, int parent) { unordered_map counts_u; counts_u[colors[u]] = 1; for (auto& edge : adj[u]) { int v = edge.first; int idx = edge.second; if (v != parent) { auto counts_v = dfs(v, u); // Process edge (u, v) ll total_pairs_using_e = 0; for (auto& p : counts_v) { int c = p.first; ll counts_v_c = p.second; ll counts_rest_c = total_counts[c] - counts_v_c; ll num_pairs_using_e_for_c = counts_v_c * counts_rest_c; total_pairs_using_e += num_pairs_using_e_for_c; } ans[idx] = total_pairs_using_e; // Merge counts_v into counts_u if (counts_u.size() < counts_v.size()) { swap(counts_u, counts_v); } for (auto& p : counts_v) { counts_u[p.first] += p.second; } } } return counts_u; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 1; i <= n; ++i) { cin >> colors[i]; total_counts[colors[i]]++; } for (int i = 0; i < n - 1; ++i) { int a, b; cin >> a >> b; adj[a].emplace_back(b, i); adj[b].emplace_back(a, i); } dfs(1, -1); for (int i = 0; i < n - 1; ++i) { cout << ans[i] << '\n'; } return 0; }