1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include<cstdio> #include<algorithm> #include<ctype.h> #include<string.h> #include<math.h> #include<vector>
using namespace std; #define ll long long
inline char read() { static const int IN_LEN = 1000000; static char buf[IN_LEN], *s, *t; return (s==t?t=(s=buf)+fread(buf,1,IN_LEN,stdin),(s==t?-1:*s++):*s++); } template<class T> inline void read(T &x) { static bool iosig; static char c; for (iosig=false, c=read(); !isdigit(c); c=read()) { if (c == '-') iosig=true; if (c == -1) return; } for (x=0; isdigit(c); c=read()) x=((x+(x<<2))<<1)+(c^'0'); if (iosig) x=-x; }
const int N = 100005; ll ans; int num=1, n, m, cnt, top, p, tot, h[N], dfn[N], low[N], stk[N], g[N<<1], siz[N<<1], e[N<<2], pre[N<<2]; vector<int> f[N<<1]; inline void add(int x, int y){ e[++num]=y, pre[num]=h[x], h[x]=num;} void tarjan(int u, int fa=0){ ++tot; dfn[u]=low[u]=++cnt; stk[++top]=u; int son=0; for(int i=h[u]; i; i=pre[i]) if(i!=fa){ if(!dfn[e[i]]){ tarjan(e[i], i^1), low[u]=min(low[u], low[e[i]]); ++son; if(low[e[i]]>=dfn[u]){ ++p; f[u].push_back(p); do f[p].push_back(stk[top]), ++g[p]; while(stk[top--]!=e[i]); } } else low[u]=min(low[u], dfn[e[i]]); } } void dfs(int u){ siz[u]=(u<=n); for(int i:f[u]) dfs(i), ans+=(ll)g[u]*siz[i]*(tot-siz[i]), siz[u]+=siz[i]; ans+=(ll)g[u]*siz[u]*(tot-siz[u]); } int main() { read(n), read(m), p=n; for(int i=1; i<=m; ++i){ static int x, y; read(x), read(y), add(x, y), add(y, x); } for(int i=1; i<=n; ++i) if(!dfn[i]) tot=0, tarjan(i), dfs(i), ans-=(ll)tot*(tot-1); return printf("%lld", ans), 0; }
|