正文
增加 57 行删除 0 行
293 行未改动全部展开294295296297293 行未改动全部展开cin >> T; while(T--) solve();return 0;}```293 行未改动全部展开294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354293 行未改动全部展开cin >> T; while(T--) solve();return 0;}```## P3359启发式合并。```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int N = 2e5 + 10;struct edge{int x, y, w;}a[N];struct node{int x, w;};int n, dep[N], f[N], id[N], sz[N], ans[N];vector<node> v[N];map<int, int> mp[N];int find(int x){return f[x] == x ? x : f[x] = find(f[x]);}void dfs(int x, int faa){for(node i : v[x]) if(i.x != faa) dep[i.x] = dep[x] ^ i.w, dfs(i.x, x);}signed main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);cin >> n;for(int i = 1; i <= n; i++) f[i] = i, sz[i] = 1;for(int i = 1, x, y, w; i < n; i++) cin >> x >> y >> w, a[i] = {x, y, w}, v[x].push_back({y, w}), v[y].push_back({x, w});for(int i = 1; i < n; i++) cin >> id[i];dfs(1, 0);for(int i = 1; i <= n; i++) mp[i][dep[i]]++;for(int ii = n - 1, i; ii >= 1; ii--){ i = id[ii];int x = a[i].x, y = a[i].y;int fx = find(x), fy = find(y);if(sz[fx] < sz[fy]) swap(fx, fy);f[fy] = fx, sz[fx] += sz[fy], ans[ii] = ans[ii + 1];for(auto zrr : mp[fy]){ int u = zrr.first, v = zrr.second;ans[ii] += mp[fx][u] * v, mp[fx][u] += v;}}for(int i = 1; i <= n; i++){cout << ans[i] << '\n';}return 0;}```