1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include<bits/stdc++.h> #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> #define W while #define I inline #define RI register int #define LL long long #define Cn const #define CI Cn int& #define gc getchar #define D isdigit(c=gc()) #define pc(c) putchar((c)) #define min(x,y) ((x)<(y)?(x):(y)) #define max(x,y) ((x)>(y)?(x):(y)) using namespace std; namespace Debug{ Tp I void _debug(Cn char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts I void _debug(Cn char* f,Ty x,Ar... y){W(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,Cn vector<Ty>& V){os<<"[";for(Cn auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; namespace FastIO{ Tp I void read(Ty& x){char c;int f=1;x=0;W(!D) f=c^'-'?1:-1;W(x=(x<<3)+(x<<1)+(c&15),D);x*=f;} Ts I void read(Ty& x,Ar&... y){read(x),read(y...);} Tp I void write(Ty x){x<0&&(pc('-'),x=-x,0),x<10?(pc(x+'0'),0):(write(x/10),pc(x%10+'0'),0);} Tp I void writeln(Cn Ty& x){write(x),pc('\n');} }using namespace FastIO; Cn int N=3e5+10; int n,m,p,fir[N],nxt[N<<1],son[N<<1],tot,dep[N],f[N],w[N],mx[N],fa[N],vis[N];LL Ans; #define P pair<int,int> #define MP make_pair #define fi first #define se second vector<int> E[N],Y,X; vector<P> L[N]; I void Add(CI x,CI y){nxt[++tot]=fir[x],fir[x]=tot,son[tot]=y;} #define to son[i] struct Edge{int u,v,w,id;}e[N]; I bool cmp(Cn Edge& x,Cn Edge& y){return x.w<y.w;} I void Dfs(CI x){RI i;for(i=fir[x];i;i=nxt[i]) dep[to]=dep[x]+1,Dfs(to);} I void Dfs2(CI x){RI i;for(i=fir[x];i;i=nxt[i]) Dfs2(to),w[fa[x]]+=w[x],w[mx[x]]<w[to]&&(mx[x]=to);} I int GF(CI x){return f[x]?f[x]=GF(f[x]):x;} struct GetFa{ int F[N]; I int GF(CI x){return x==F[x]?x:F[x]=GF(F[x]);} }O,U; I bool Ct(RI x,RI y,RI k){RI t=0;W(x^y){dep[x]<dep[y]&&(swap(x,y),0),x=fa[x],t++;if(t>L[k].size()) return 1;}return t>L[k].size();} I void M(RI x,RI y,CI z){if((x=O.GF(x))^(y=O.GF(y))) O.F[x]=y,Ans+=z;} I void FU(RI x,RI y,CI z){W(x^y) dep[x]<dep[y]&&(swap(x,y),0),M(x,fa[x],z),U.F[x]=fa[x],x=U.GF(fa[x]);} int main(){ RI i,j,t,x,y,Mn;for(read(n,m,p),dep[1]=w[1]=1,i=2;i<=n;i++) read(x),Add(x,i),fa[i]=x,w[i]=1;Dfs(1);Dfs2(1); for(i=1;i<=m;i++) read(e[i].u,e[i].v,e[i].w),e[i].id=i;sort(e+1,e+m+1,cmp); for(i=1;i<=p;i++) read(t,x,y),L[t].push_back(MP(x,y)); for(i=1;i<=n;i++) O.F[i]=U.F[i]=i; for(i=1;i<=m;i++) if(Ct(e[i].u,e[i].v,e[i].id)) FU(e[i].u,e[i].v,e[i].w);else{ for(auto j:L[e[i].id]) E[j.fi].push_back(j.se),E[j.se].push_back(j.fi); Y.clear(),x=e[i].u,y=e[i].v;W(x^y) dep[x]<dep[y]&&(swap(x,y),0),Y.push_back(x),x=fa[x];Y.push_back(x); Mn=2e9,t=0;for(auto j:Y) E[j].size()<Mn&&(Mn=E[j].size(),t=j); X.clear();for(auto j:E[t]) vis[j]=1;for(auto j:Y) if(!vis[j]) X.push_back(j),M(j,t,e[i].w);for(auto j:E[t]) vis[j]=0; for(auto j:E[t]){ for(auto k:E[j]) vis[k]=1;for(auto k:E[t]) !vis[k]&&(M(j,k,e[i].w),0);for(auto k:E[j]) vis[k]=0; if(E[j].size()<X.size()) M(t,j,e[i].w); else{for(auto k:E[j]) vis[k]=1;for(auto k:X) !vis[k]&&(M(k,j,e[i].w),0);for(auto k:E[j]) vis[k]=0;} }for(auto j:L[e[i].id]) E[j.fi].clear(),E[j.se].clear(); }return writeln(Ans),0; }
|