# CF1681F. Unique Occurrences (可撤销并查集， 分治)

https://codeforces.com/contest/1681/problem/F

https://zhuanlan.zhihu.com/p/521111820
https://zhuanlan.zhihu.com/p/521344322

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false) ,cin.tie(0), cout.tie(0);
//#pragma GCC optimize(3,"Ofast","inline")
#define ll long long
#define PII pair<int, int>
//#define int long long
const int N = 5e5 + 5;
const int M = 1e6 + 5;
const int INF = 0x3f3f3f3f;
const ll LNF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double PI = acos(-1.0);
int n; ll ans = 0;
vector<PII> mp[N];
struct DSU {
int fa[N], sz[N];
vector<pair<int&, int>> his_sz;
vector<pair<int&, int>> his_fa;
void init( int n ) {for ( int i = 1; i <= n; ++ i ) fa[i] = i, sz[i] = 1;}
int find( int x ) {
while( x != fa[x] ) x = fa[x];
return x;
}
bool same(int u, int v) {
return find(u) == find(v);
}
int size(int u) {
return sz[find(u)];
}
//让sz小的x的爹变成y
//修改了sz[y]和fa[x],记录着俩值
void merge( int a, int b) {
int x = find(a), y = find(b);
if( x == y ) return;
if(sz[x] > sz[y]) swap(x, y);
his_sz.push_back({sz[y], sz[y]});
sz[y] += sz[x];
his_fa.push_back({fa[x], fa[x]});
fa[x] = y;
}
int history() { //历史戳
return his_fa.size();
}
void roll ( int h ) { //回滚到历史h处
while( his_fa.size() > h ) {
his_fa.back().first = his_fa.back().second;
his_fa.pop_back();
his_sz.back().first = his_sz.back().second;
his_sz.pop_back();
}
}
}dsu;
void dfs(int l, int r) {
if(l == r) { // 删去权值为l的边
for (auto i : mp[l]) {
int sb1 = dsu.size(i.first), sb2 = dsu.size(i.second);
ans += 1ll * dsu.size(i.first) * dsu.size(i.second);
}
return;
}
int mid = l + r >> 1;
int h = dsu.history(); //历史戳
for ( int i = l; i <= mid; ++ i ) {
for (auto [u, v] : mp[i]) {
dsu.merge(u, v);
}
}
dfs(mid + 1, r);
dsu.roll(h);//回滚到当前层的历史戳
for ( int i = mid + 1; i <= r; ++ i ) {
for (auto [u, v] : mp[i]) {
dsu.merge(u, v);
}
}
dfs(l, mid);
dsu.roll(h);
}
void solve() {
cin >> n;
dsu.init(n);
for ( int i = 1; i <= n - 1; ++ i ) {
int u, v, w; cin >> u >> v >> w;
mp[w].push_back({u, v});
}
dfs(1, n);
cout << ans << '\n';
}
int main() {
IOS
int t = 1; //cin >> t;
while( t -- ) solve();
return 0;
}

posted @ 2022-10-06 10:58  qingyanng  阅读(48)  评论(0编辑  收藏  举报