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 65 66 67 68 69 70 71 72 73
| #include<bits/stdc++.h> #define pb push_back using namespace std; const int N=1e5+5,M=3e5+5; int n,m,d[N]; vector<int> G[N],C[N]; int col,low[N],dfn[N],tot,st[N],top; int num[N]; void Tarjan(int cur,int rt){ dfn[cur]=low[cur]=++tot; st[++top]=cur; for (int i=0;i<(int)G[cur].size();++i){ int to=G[cur][i]; if (!dfn[to]) Tarjan(to,rt),low[cur]=min(low[cur],low[to]); else if (dfn[to]>=rt) low[cur]=min(low[cur],dfn[to]); } if (low[cur]==dfn[cur]){ ++col; while (st[top+1]!=cur) num[st[top]]=col,C[col].pb(st[top--]); } } signed main(){ cin>>n>>m; if (n==1) return puts("1.000000"),0; for (int i=1;i<=m;++i){ int x,y; cin>>x>>y; G[x].pb(y); } for (int i=1;i<=n;++i){ if (!dfn[i]) Tarjan(i,tot+1); } for (int i=1;i<=col;++i){ for (int j=0;j<(int)C[i].size();++j){ int cur=C[i][j]; for (int k=0;k<(int)G[cur].size();++k){ int to=G[cur][k]; if (num[to]!=num[cur]) ++d[num[to]]; } } } int ans=0;bool tag=0; for (int i=1;i<=col;++i){ if (!d[i]){ if (C[i].size()==1&&!tag){ bool flag=0; int cur=C[i][0]; for (int j=0;j<(int)G[cur].size();++j){ int to=G[cur][j]; if (d[num[to]]==1){ flag=1;break; } } if (!flag) tag=1; } ++ans; } } if (ans>1&&tag) --ans; double res=1.0-(double)ans/n; printf("%.6lf",res); return 0; }
|