提交时间:2022-07-20 11:55:24
运行 ID: 52785
//30pts(?) #include <bits/stdc++.h> using namespace std; struct node{ int v,nxt; }edge[42];int head[20],cntE; void add(int u,int v){ edge[++cntE]=(node){v,head[u]}; head[u]=cntE; } int n,a,b; int val[22],id[22]; int vis[22]; int ans,scc,cnt; void dfs(int u){ id[u]=scc;cnt++; for(int i=head[u];i;i=edge[i].nxt){ int v=edge[i].v; if(id[v]){ ans+=cnt-1;return; }dfs(v); } } int main(){ scanf("%d%d%d",&n,&a,&b); for(int i=1;i<=n;i++) scanf("%d",&val[i]); for(int i=1;i<=n;i++){ if(val[i]==i) continue; if(vis[i]) continue; vis[i]=1; while(true){ add(i,val[i]); i=val[i];if(vis[i]) break; vis[i]=1; } } for(int i=1;i<=n;i++){ if(val[i]==i||id[i]) continue; ++scc,cnt=0;dfs(i); } printf("%.10f",(double)ans*a); }