Map

题目大意

对于一副图,若干询问每次询问加入一条边后原图增加多少安全点对,安全点对定义为两个点之间存在两条不经过相同边的路径。

裸题

缩点后,每个连通分量点权为包含点数量。
若加入边为(j,k),j在点x内,k在点y内。答案为x到y树路径上点权和平方减点权平方和,倍增维护一下。

#include<cstdio>
#include<algorithm>
#include<cmath>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxn=200000+10,maxm=400000+10;
int sta[maxn],belong[maxn],a[maxn],d[maxn],dfn[maxn],low[maxn],fa[maxn][25],g[maxn][25];
ll f[maxn][25];
int h[maxn],go[maxm*2],next[maxm*2],h2[maxn],g2[maxn*2],n2[maxn*2],dl[maxn][2];
bool bz[maxn],pd[maxn],dp[maxm];
struct dong{
    int x,y,t;
} s[maxn];
int i,j,k,l,t,n,m,q,top,tot,cnt,pot,euler;
ll ans,now;
int read(){
    int x=0;
    char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x;
}
void add(int x,int y){
    go[++tot]=y;
    next[tot]=h[x];
    h[x]=tot;
}
void tarjan(int x){
    /*bz[x]=1;
    pd[x]=1;
    sta[++top]=x;
    low[x]=dfn[x]=++euler;
    int t=h[x];
    while (t){
        if (!dp[(t+1)/2]){
            dp[(t+1)/2]=1;
            if (!bz[go[t]]){
                tarjan(go[t]);
                if (low[go[t]]<low[x]) low[x]=low[go[t]];
            }
            else if (pd[go[t]])
                if (dfn[go[t]]<low[x]) low[x]=dfn[go[t]];
        }
        t=next[t];
    }
    if (low[x]==dfn[x]){
        ++cnt;
        while (sta[top+1]!=x){
            belong[sta[top]]=cnt;
            a[cnt]++;
            pd[sta[top]]=0;
            top--;
        }
    }*/
    int t;
    bool czy;
    s[pot=1].x=x;
    s[1].t=-1;
    while (pot){
        x=s[pot].x;
        if (s[pot].t==-1){
            bz[x]=1;
            pd[x]=1;
            sta[++top]=x;
            low[x]=dfn[x]=++euler;
            s[pot].t=h[x];
        }
        else{
            t=s[pot].t;
            if (low[go[t]]<low[x]) low[x]=low[go[t]];
            s[pot].t=next[t];
        }
        t=s[pot].t;
        czy=0;
        while (t){
            if (!dp[(t+1)/2]){
                dp[(t+1)/2]=1;
                if (!bz[go[t]]){
                    s[++pot].x=go[t];
                    s[pot].t=-1;
                    s[pot-1].t=t;
                    czy=1;
                    break;
                    //if (low[go[t]]<low[x]) low[x]=low[go[t]];
                }
                else if (pd[go[t]])
                    if (dfn[go[t]]<low[x]) low[x]=dfn[go[t]];
            }
            t=next[t];
        }
        if (czy) continue;
        if (low[x]==dfn[x]){
            ++cnt;
            while (sta[top+1]!=x){
                belong[sta[top]]=cnt;
                a[cnt]++;
                pd[sta[top]]=0;
                top--;
            }
        }
        pot--;
    }
}
void add2(int x,int y){
    g2[++tot]=y;
    n2[tot]=h2[x];
    h2[x]=tot;
}
void bfs(int x){    
    int head=0,tail=1,t,y;
    dl[1][0]=x;
    while (head<tail){
        x=dl[++head][0];
        y=dl[head][1];
        fa[x][0]=y;
        d[x]=d[y]+1;
        t=h2[x];
        while (t){
            if (g2[t]!=y){
                dl[++tail][0]=g2[t];
                dl[tail][1]=x;
            }
            t=n2[t];
        }
    }
}
int lca(int x,int y){
    if (d[x]<d[y]) swap(x,y);
    if (d[x]!=d[y]){
        int j=floor(log(cnt)/log(2));
        while (j>=0){
            if (d[fa[x][j]]>=d[y]) x=fa[x][j];
            j--;
        }
    }
    if (x==y) return x;
    int j=floor(log(cnt)/log(2));
    while (j>=0){
        if (fa[x][j]!=fa[y][j]){
            x=fa[x][j];
            y=fa[y][j];
        }
        j--;
    }
    return fa[x][0];
}
int getsum(int x,int y){
    int j=floor(log(cnt)/log(2)),t=0;
    while (j>=0){
        if (d[fa[x][j]]>=d[y]){
            t+=g[x][j];
            x=fa[x][j];
        }
        j--;
    }
    return t;
}
ll getnum(int x,int y){
    int j=floor(log(cnt)/log(2));
    ll t=0;
    while (j>=0){
        if (d[fa[x][j]]>=d[y]){
            t+=f[x][j];
            x=fa[x][j];
        }
        j--;
    }
    return t;
}
int main(){
    n=read();m=read();q=read();
    fo(i,1,m){
        j=read();k=read();
        add(j,k);add(k,j);
    }
    fo(i,1,n)
        if (!bz[i]) tarjan(i);
    tot=0;
    fo(i,1,n){
        t=h[i];
        while (t){
            if (belong[go[t]]!=belong[i]) add2(belong[i],belong[go[t]]);
            t=next[t];
        }
    }
    euler=0;
    bfs(1);
    fo(i,1,cnt) f[i][0]=(ll)a[i]*a[i],g[i][0]=a[i];
    fo(j,1,floor(log(cnt)/log(2)))
        fo(i,1,cnt){
            fa[i][j]=fa[fa[i][j-1]][j-1];
            f[i][j]=f[i][j-1]+f[fa[i][j-1]][j-1];
            g[i][j]=g[i][j-1]+g[fa[i][j-1]][j-1];
        }
    while (q--){
        j=read();k=read();
        j=belong[j];k=belong[k];
        if (j==k) continue;
        l=lca(j,k);
        t=getsum(j,l)+getsum(k,l)+a[l];
        now=(ll)t*t;
        now-=getnum(j,l)+getnum(k,l)+(ll)a[l]*a[l];
        ans+=now;
    }
    printf("%lld\n",ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值