标签:bitset std merge const read using ret dig cal
解题思路
/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int ch = 0, f = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
const int N = 300005;
ll ans;
bitset<N> B, C;
vector<int> e[N];
vector<pair<int, int> > g[N];
int f[N][21], dep[N], ax[N], ay[N], aw[N], id[N], n, m, p, tot;
namespace Rose{
vector<int> g[N];
inline void dfs(int u, int fa){
dep[u] = dep[fa] + 1, f[u][0] = fa;
for(int i = 1; i <= 20; i++)
f[u][i] = f[f[u][i-1]][i-1];
for(auto v : g[u])
if(v != fa) dfs(v, u);
}
inline int lca(int x, int y){
if(dep[x] < dep[y]) swap(x, y);
for(int i = 20; ~i; i--)
if(dep[f[x][i]] >= dep[y]) x = f[x][i];
if(x == y) return x;
for(int i = 20; ~i; i--)
if(f[x][i] != f[y][i])
x = f[x][i], y = f[y][i];
return f[x][0];
}
}
struct Camlia{
int fa[N];
inline void init(){
for(int i = 1; i <= n; i++) fa[i] = i;
}
inline int ask(int x){
return x == fa[x] ? x : fa[x] = ask(fa[x]);
}
inline void merge(int x, int y, int z){
int p = ask(x), q = ask(y);
if(p == q) return;
tot++, fa[p] = q, ans += z;
}
}X1, X2;
inline bool cmp(int x, int y){ return aw[x] < aw[y]; }
int main(){
read(n), read(m), read(p);
for(int i = 2, x; i <= n; i++)
read(x), Rose::g[x].push_back(i);
for(int i = 1; i <= m; i++){
id[i] = i;
read(ax[i]), read(ay[i]), read(aw[i]);
}
for(int i = 1, x, y, z; i <= p; i++){
read(x), read(y), read(z);
g[x].push_back(make_pair(y, z));
}
sort(id + 1, id + m + 1, cmp);
Rose::dfs(1, 0);
X1.init(), X2.init();
for(int i = 1; i <= m; i++){
int x = id[i], u = ax[x], v = ay[x];
int lca = Rose::lca(u, v);
int dis = dep[u] + dep[v] - 2 * dep[lca] + 1;
if((int) g[x].size() < dis - 1){
u = X2.ask(u), v = X2.ask(v);
while(u != v){
if(dep[u] < dep[v]) swap(u, v);
X1.merge(u, f[u][0], aw[x]);
X2.fa[u] = X2.ask(f[u][0]), u = X2.ask(u);
}
}
else{
vector<int> A; A.push_back(lca);
int now = u;
while(now != lca)
A.push_back(now), now = f[now][0];
now = v;
while(now != lca)
A.push_back(now), now = f[now][0];
for(auto ed : g[x]){
e[ed.first].push_back(ed.second);
e[ed.second].push_back(ed.first);
}
int mndeg = (int) A.size(), pos = 0;
for(auto k : A)
if((int) e[k].size() < mndeg)
mndeg = (int) e[k].size(), pos = k;
int size = (int) A.size() - (int) e[pos].size();
for(auto k : e[pos]) B[k] = 1;
for(auto k : A) if(!B[k]) X1.merge(pos, k, aw[x]);
for(auto k1 : e[pos]){
for(auto k2 : e[k1]) C[k2] = 1;
for(auto k2 : e[pos])
if(!C[k2]) X1.merge(k1, k2, aw[x]);
for(auto k2 : e[k1]) C[k2] = 0;
}
for(auto k1 : e[pos]){
int tmp = 0;
for(auto k2 : e[k1]) if(!B[k2]) tmp++;
if(tmp < size) X1.merge(k1, pos, aw[x]);
}
for(auto k : e[pos]) B[k] = 0;
for(auto k : A) e[k].clear();
}
}
cout << ans << endl;
return 0;
}
标签:bitset std merge const read using ret dig cal
原文地址:https://www.cnblogs.com/mangoyang/p/11637834.html