其他分享
首页 > 其他分享> > [CERC2015]Juice Junctions(边双连通+字符串hash)

[CERC2015]Juice Junctions(边双连通+字符串hash)

作者:互联网

做法

考虑边数限制的特殊条件,显然答案仅有\(\{0,1,2,3\}\)

考虑每次删边后记录各点的边双染色情况来特判\(3\):是否所有情况都相同

Code

#include<bits/stdc++.h>
typedef int LL;
typedef long long ll;
const LL maxn=1e6+9,inf=0x3f3f3f3f;
const ll base_1=233,base_2=2333,mod1=99991,mod2=99989;
inline LL Read(){
    LL x(0),f(1); char c=getchar();
    while(c<'0' || c>'9'){
        if(c=='-') f=-1; c=getchar();
    }
    while(c>='0' && c<='9'){
        x=(x<<3)+(x<<1)+c-'0'; c=getchar();
    }return x*f;
}
struct node{
    LL to,nxt;
}dis[maxn];
LL n,m,top,num,nod,tim,ban,ans;
LL head[maxn],col[maxn],sta[maxn],dfn[maxn],low[maxn],f[maxn],hash_1[maxn],hash_2[maxn];
ll hash1[maxn],hash2[maxn];
inline void Add(LL u,LL v){
    dis[++num]=(node){v,head[u]}; head[u]=num;
}
void Tarjan(LL u,LL fa){
    dfn[u]=low[u]=++tim; sta[++top]=u;
    for(LL i=head[u];i;i=dis[i].nxt){
        LL v(dis[i].to);
        if(v==fa || i==ban || i==ban-1) continue;
        if(!dfn[v]){
            Tarjan(v,u); low[u]=std::min(low[u],low[v]);
        }else low[u]=std::min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u]){
        LL now; ++nod;
        do{
            now=sta[top--]; col[now]=nod;
        }while(now!=u);
    }
}
LL Find(LL x){
    return f[x]==x?x:f[x]=Find(f[x]);
}
inline void Union(LL u,LL v){
    f[Find(u)]=Find(v);
}
int main(){
    n=Read(); m=Read();
    for(LL i=1;i<=n;++i) f[i]=i;
    for(LL i=1;i<=m;++i){
        LL u(Read()),v(Read());
        Add(u,v); Add(v,u);
        Union(u,v);
    }
    for(LL i=1;i<=n;++i)
        if(!dfn[i])
            Tarjan(i,0);
    for(LL i=1;i<=n;++i)
        for(LL j=i+1;j<=n;++j)
            ans+=(Find(i)==Find(j)) + (col[i]==col[j]);
    for(ban=2;ban<=(m<<1);ban+=2){
        memset(dfn,0,4*(n+1)); nod=top=tim=0;
        for(LL i=1;i<=n;++i)
            if(!dfn[i])
                Tarjan(i,0);
        for(LL i=1;i<=n;++i){
            hash_1[i]=(hash_1[i]*base_1%mod1+col[i])%mod1;
            hash_2[i]=(hash_2[i]*base_2%mod1+col[i])%mod2;
        }
    }
    for(LL i=1;i<=n;++i)
        for(LL j=i+1;j<=n;++j)
            ans+=(hash_1[i]==hash_1[j] && hash_2[i]==hash_2[j]);
    printf("%d\n",ans);
    return 0;
}

标签:typedef,hash,边双,LL,连通,long,Juice,const
来源: https://www.cnblogs.com/y2823774827y/p/10952510.html