另一个OJ上AC,luogu wa on #9 #12
查看原帖
另一个OJ上AC,luogu wa on #9 #12
482660
konyakest楼主2022/12/28 22:32
#include <bits/stdc++.h>
using namespace std;
#define F(i,j,k) for (signed i=signed(j);i<=signed(k);i++)
#define endl '\n'

#define DEBUG

#ifdef DEBUG
template<typename T>void dbg(const T& t){cerr<<t<<endl;}
template<typename T,typename... Args>void dbg(const T& t,const Args&...r){cerr<<t<<",";dbg(r...);}
#define debug(...) {cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<" = ";dbg(__VA_ARGS__);}
#else
#define debug(...) 
#endif

const int maxn=2e5;
int n,m,val[maxn],x[maxn],y[maxn],dfn[maxn],low[maxn];
int sta[maxn],top,rudu[maxn],inde,color[maxn],sum[maxn],f[maxn],tot;
bool flag[maxn];
vector<int> v[maxn];
void tarjan(int x){
    dfn[x]=low[x]=++inde;
    sta[++top]=x;
    flag[x]=1;
    for(auto i:v[x]){
        if(!dfn[i]) tarjan(i),low[x]=min(low[x],low[i]);
        else if(flag[i]) low[x]=min(low[x],dfn[i]);
    }
    if(low[x]==dfn[x]){
        tot++;
        while(sta[top+1]!=x){
            color[sta[top]]=tot;
            sum[tot]++;
            flag[sta[top--]]=0;
        }
    }
}

set<pair<int,int>> st;
bool check(int x){
    for(auto i:v[x]) if(rudu[i]==1) return 0;
    return 1;
}

signed main () {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    F(i,1,m){
        cin>>x[i]>>y[i];
        v[x[i]].push_back(y[i]);
        rudu[y[i]]++;
    }
    F(i,1,n) if(!dfn[i]) tarjan(i);
    F(i,1,n) v[i].clear();
    memset(rudu,0,sizeof rudu);
    F(i,1,m) {
        if(color[x[i]]!=color[y[i]]&&!st.count({color[x[i]],color[y[i]]})) 
            v[color[x[i]]].push_back(color[y[i]]),
            rudu[color[y[i]]]++,
            st.insert({color[x[i]],color[y[i]]});
    }
    int ans=0;
    F(i,1,tot) if(rudu[i]==0) ans++;
    F(i,1,tot) if(rudu[i]==0&&sum[i]==1&&check(i)) {ans--;break;}
    cout<<fixed<<setprecision(6)<<(n-ans)*1.0/n<<endl;
    return 0;
}
2022/12/28 22:32
加载中...