思路:
tarjan;
代码:
#include <bits/stdc++.h> using namespace std; #define maxn 100005 #define maxm 500005 #define ll long long ll Count[maxn],E[maxm<<1],V[maxm<<1],head[maxn],cnt; ll size[maxm],dfn[maxn],n,m,tot,low[maxn]; inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>‘9‘||Cget<‘0‘)Cget=getchar(); while(Cget>=‘0‘&&Cget<=‘9‘) { now=now*10+Cget-‘0‘; Cget=getchar(); } } void tarjan(ll now) { ll tmp=0; size[now]=1,dfn[now]=low[now]=++tot; for(ll i=head[now];i;i=E[i]) { if(dfn[V[i]]) low[now]=min(low[now],dfn[V[i]]); else { tarjan(V[i]),size[now]+=size[V[i]],low[now]=min(low[now],low[V[i]]); if(dfn[now]<=low[V[i]])Count[now]+=tmp*size[V[i]],tmp+=size[V[i]]; } } Count[now]+=tmp*(n-tmp-1); } int main() { in(n),in(m);ll u,v; while(m--) { in(u),in(v); E[++cnt]=head[u],V[cnt]=v,head[u]=cnt; E[++cnt]=head[v],V[cnt]=u,head[v]=cnt; } tarjan(1); for(ll i=1;i<=n;i++) printf("%lld\n",(Count[i]+n-1)<<1); return 0; }
时间: 2024-10-21 18:59:29