60分求调!有没有犇犇帮帮本蒟蒻
查看原帖
60分求调!有没有犇犇帮帮本蒟蒻
1044521
zhz_cptbtptp楼主2025/1/19 20:59

求救

不知道为什么爆60bts了

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5,maxm=5e5+5;
struct edge{
    int u,v,nxt;
}e[maxm*2];

int len,head[maxn];

void insert(int u,int v){
    e[++len]={u,v,head[u]};
    head[u]=len;
}

int n,m;

void read(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;++i){
        int u,v;
        scanf("%d%d",&u,&v);
        insert(u,v);
        insert(v,u);
    }
}

int low[maxn],dfn[maxn],tim;
long long ans[maxn];
int vis[maxn],siz[maxn];

void tarjan(int u,int fa){
    dfn[u]=low[u]=++tim;
    siz[u]=1;
    int kuai=0,son=0;
    for(int i=head[u];i;i=e[i].nxt){
        int v=e[i].v;
        if(v==fa){
            continue;
        }
        if(dfn[v]==0){
            tarjan(v,u);
            siz[u]+=siz[v];
            low[u]=min(low[u],low[v]);
            if(low[v]>dfn[u]){
                ans[u]+=(long long)siz[v]*(n-siz[v]);
                son+=siz[v];
                kuai++;
                if(u!=1||kuai>1){
                    vis[u]=1;
                }
            }
        }else{
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(vis[u]==0){
        ans[u]=(long long)2*(n-1);
    }else{
        ans[u]+=(long long)(son+1)*(n-son-1)+(n-1);
    }
}

void sol(){
    read();
    tarjan(1,0);
    for(int i=1;i<=n;++i){
        printf("%lld\n",ans[i]);
    }
}

int main(){
    sol();
    return 0;
}
2025/1/19 20:59
加载中...