因为删去一条边图不连通,很容易想到割边
也就是说只有割边有用
那我们先去缩点,变成一棵树,这样树上每条边都是割边
连接树上任意两点,中间的边都不是割边了
所以直径最划算
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
const int maxn = 2e5+10;
struct edge
{
int u,to,nxt;
}d[maxn<<1]; int head[maxn<<1],cnt=1,n,m,ans,cut;
vector<int>vec[maxn];
void add(int u,int v) { d[++cnt] = (edge){ u,v,head[u] },head[u] = cnt; }
int stac[maxn],vis[maxn],dfn[maxn],low[maxn],top,id,bccn,bcc[maxn];
int f[maxn];
void tarjan(int u,int fa)
{
dfn[u] = low[u] = ++id; stac[++top] = u, vis[u] = 1;
int flag = 0;
for(int i=head[u];i;i=d[i].nxt )
{
int v = d[i].to;
if( !dfn[v] )
{
tarjan(v,u);
low[u] = min( low[u],low[v] );
if( u!=fa&&low[v]>dfn[u] ) cut++;
}
else if( vis[v] )
{
if( v!=fa||flag ) low[u] = min( low[u],dfn[v] );
else flag = 1;
}
}
if( low[u]==dfn[u] )
{
int temp; ++bccn;
while( temp=stac[top--] )
{
bcc[temp] = bccn; vis[temp] = 0;
if( temp==u ) break;
}
}
}
void dfs(int u,int fa)
{
for(auto v:vec[u] )
{
if( v==fa ) continue;
dfs(v,u);
ans = max( ans,f[u]+f[v]+1 );
f[u] = max( f[u],f[v]+1 );
}
ans = max( ans,f[u] );
}
int quick(int x,int n)
{
int ans = 1;
for( ; n ; n>>=1,x=1ll*x*x%mod )
if( n&1 ) ans = 1ll*ans*x%mod;
return ans;
}
int main()
{
cin >> n >> m;
for(int i=1;i<=m;i++)
{
int l,r; cin >> l >> r;
add(l,r); add(r,l);
}
tarjan(1,0);
for(int i=2;i<=cnt;i++)
{
int u = d[i].u, v = d[i].to;
if( bcc[u]!=bcc[v] )
{
vec[bcc[u]].push_back(bcc[v]);
}
}
dfs(1,1);
cout << 1ll*(cut-ans)*quick(m+1,mod-2)%mod;
}