题目分析:

差评,最大生成树裸题。hack数据还卡常。

代码:

 #include<bits/stdc++.h>
using namespace std; const int maxn = ; struct LCT{
int fa[maxn],lazy[maxn],ch[maxn][],d1[maxn],d2[maxn];
int val[maxn],tot[maxn],num;
stack<int> sta;
void push_up(int now){
val[now] = min(d1[now],min(val[ch[now][]],val[ch[now][]]));
tot[now] = d2[now] + tot[ch[now][]] + tot[ch[now][]];
}
void push_down(int now){
if(lazy[now]){
swap(ch[now][],ch[now][]);
lazy[now]=;
lazy[ch[now][]] ^= ; lazy[ch[now][]] ^= ;
lazy[] = ;
}
}
int is_root(int now){return !(ch[fa[now]][]==now||ch[fa[now]][]==now);}
void r0(int now,int dr){
int ff = fa[now],gf = fa[ff],son = ch[now][dr];
int flag = is_root(ff);
ch[ff][dr^] = son; fa[son] = ff;
ch[now][dr] = ff; fa[ff] = now;
fa[now] = gf;
if(!flag){if(ch[gf][] == ff) ch[gf][] = now; else ch[gf][] = now;}
push_up(ff); push_up(now);
fa[] = ;ch[][] = ch[][] = ;
}
void splay(int now){
int pp = now;
while(!is_root(pp)) sta.push(pp),pp = fa[pp];
sta.push(pp);
while(!sta.empty()) push_down(sta.top()),sta.pop();
while(!is_root(now)){
if(is_root(fa[now])){
if(ch[fa[now]][] == now) r0(now,); else r0(now,);
}else{
int ff = fa[now],gf = fa[ff];
int alpha = ch[gf][]==ff,beta = ch[ff][]==now;
if(alpha^beta) r0(now,beta),r0(now,alpha);
else r0(ff,alpha),r0(now,beta);
}
}
}
void access(int now){
splay(now);ch[now][] = ;push_up(now);
while(fa[now]){
int nxt=fa[now];splay(nxt);ch[nxt][]=now; push_up(nxt); now=nxt;
}
}
void make_root(int now){
access(now);
splay(now);
lazy[now] ^= ;
}
void cut(int u,int v){
make_root(u); access(v); splay(u);
fa[v] = ;
if(ch[u][] == v) ch[u][] = ; else ch[u][] = ;
push_up(u);
}
void link(int u,int v){make_root(u); fa[u] = v;}
int lft(int dt){
splay(dt);
if(lazy[dt]) push_down(dt);
while(ch[dt][]){
dt = ch[dt][];
if(lazy[dt]) push_down(dt);
}
return dt;
}
int find_min(int now){
splay(now);
while(d1[now] != val[now]){
if(val[ch[now][]] == val[now]) now = ch[now][];
else now = ch[now][];
}
return now;
}
}T; int n,m,pre[maxn]; struct edge{int from,to,tmp,len;}edges[maxn]; char str[]; int found(int x){
int rx = x; while(pre[rx] != rx) rx = pre[rx];
while(pre[x]!=rx){int tt = x; pre[x] = rx; x = tt;}
return rx;
} void work(){
T.d1[] = 2e9;T.val[] = 2e9;T.d2[] = ;
for(int i=;i<=n;i++){T.num++;T.d1[i] = 2e9; T.val[i] = 2e9; pre[i] = i;}
for(int i=;i<=m;i++){
scanf("%s",str);
if(str[] == 'f'){
int id;scanf("%d",&id);
scanf("%d%d",&edges[id].from,&edges[id].to);
scanf("%d%d",&edges[id].tmp,&edges[id].len);
edges[id].from++;edges[id].to++;
T.d1[n+id+] = edges[id].tmp; T.d2[n+id+] = edges[id].len;
T.val[n+id+] = edges[id].tmp; T.tot[n+id+] = edges[id].len;
T.make_root(edges[id].from);
T.access(edges[id].to);
int pd = T.lft(edges[id].to);
if(pd == edges[id].from){
if(T.val[edges[id].to] > edges[id].tmp) continue;
int pla = T.find_min(edges[id].to);
T.make_root(pla);T.cut(pla,edges[pla-n-].from);
T.cut(pla,edges[pla-n-].to);
}
T.link(edges[id].from,n+id+);T.link(edges[id].to,n+id+);
pre[found(edges[id].from)] = found(edges[id].to);
}else{
if(str[] == 'm'){
int u,v; scanf("%d%d",&u,&v); v++;u++;
if(found(u) != found(v)){puts("-1");continue;}
T.make_root(u); T.access(v); T.splay(v);
printf("%d\n",T.tot[v]);
}else{
int id,l; scanf("%d%d",&id,&l);
T.make_root(n+id+); T.d2[n+id+] = l;
T.push_up(n+id+);
}
}
}
} int main(){
scanf("%d%d",&n,&m);
work();
return ;
}
05-11 19:57