holiday/8.19/C-killreference.cpp
2022-08-21 08:22:24 +08:00

96 lines
1.7 KiB
C++

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define MAXN 100001
#define MAXM 300001
struct people
{
int to,next,from;
};
people a[MAXM];
int N,M,last[MAXN],step=0,tot1=0,tot=0,LOW[MAXN],DFN[MAXN],stack[MAXN],ans=0;
int belong[MAXN],indegree[MAXN],IN,OUT,ans1=0,size1[MAXN],ans2=0;
bool instack[MAXN];
void add(int x,int y)
{
a[++tot].to=y;
a[tot].from=x;
a[tot].next=last[x];
last[x]=tot;
}
void Tarjan(int now)
{
DFN[now]=LOW[now]=++step;
stack[++tot1]=now;
instack[now]=true;
for(int i=last[now]; i; i=a[i].next)
{
int now_to=a[i].to;
if(!DFN[now_to])
{
Tarjan(now_to);
LOW[now]=min(LOW[now_to],LOW[now]);
}
else if(instack[now_to])
LOW[now]=min(DFN[now_to],LOW[now]);
}
if(DFN[now]==LOW[now])
{
int now_out;
ans++;
do
{
now_out=stack[tot1--];
instack[now_out]=false;
belong[now_out]=ans;
size1[ans]++;
}
while(now_out!=now);
}
}
int main()
{
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
memset(belong,0,sizeof(belong));
memset(DFN,0,sizeof(DFN));
memset(LOW,0,sizeof(LOW));
memset(last,0,sizeof(last));
memset(instack,false,sizeof(instack));
scanf("%d%d",&N,&M);
for(int i=1; i<=M; i++)
{
int A,B;
scanf("%d%d",&A,&B);
add(A,B);
}
for(int i=1; i<=N; i++)
if(!DFN[i])
Tarjan(i);
for(int i=1; i<=tot; i++)
{
if(belong[a[i].to]!=belong[a[i].from])
indegree[belong[a[i].to]]++;
}
for(int i=1; i<=ans; i++)
if(!indegree[i])
ans1++;
if(ans1>1)
for(int i=1; i<=ans; i++)
if(!indegree[i] && size1[i]==1)
{
ans1--;
break;
}
printf("%.6lf\n",1-(double)ans1/N);
//system("pause");
return 0;
}