虚树建树

bool cmp(int x,int y){
    return dfn[x]<dfn[y];
}
void build(){
    sort(h+1,h+1+k,cmp);
    q[top=1]=1,g.h[1]=0,g.k=0;
    for(int i=1;i<=k;i++){
        if(h[i]==1) continue;
        int z=lca(h[i],q[top]);
        if(z!=q[top]){
            while(dfn[z]<dfn[q[top-1]]) g.cun(q[top-1],q[top],minn[q[top]]),top--;
            if(dfn[z]==dfn[q[top-1]]) g.cun(z,q[top],minn[q[top]]),top--;
            else g.h[z]=0,g.cun(z,q[top],minn[q[top]]),q[top]=z;
        } 
        g.h[h[i]]=0,q[++top]=h[i];
    }
    for(int i=1;i<top;i++) g.cun(q[i],q[i+1],minn[q[i+1]]); 
}
posted @ 2022-10-28 14:41  hubingshan  阅读(14)  评论(0)    收藏  举报  来源