I’m finding the size of all the strongly connected components and further to find the probability placing both soldiers one by one at each location why is my answer not accepted .Here is my source code for reference any help will be appreciated.
Thanks in advance
@@@@@@
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll timer=0;
vector<vector> bridge;
void dfs(ll u,vector<vector>&adj,vector&vis,vector&ti,vector&low,ll par)
{
vis[u]=true;
ti[u]=low[u]=timer;
timer++;
for(auto &x:adj[u])
{
if(par==x) continue;
if(!vis[x])
{
dfs(x,adj,vis,ti,low,u);
low[u]=min(low[u],low[x]);
if(low[x]>ti[u])
{
bridge.push_back({x,u});
}
}
else
{
low[u]=min(low[x],ti[u]);
}
}
}
ll DFS(ll u,vector<vector>&adj,vector&vis)
{
vis[u]=true;
ll ans=0;
for(int i=1;i<adj.size();i++)
{
if(vis[i]) continue;
if(adj[i][u]) ans+=DFS(i,adj,vis);
}
return ans+1;
}
int main() {
ll n,m;
cin>>n>>m;
vector<vector> adjMat(n+1,vector(n+1,0));
vector<vector> adjLis(n+1);
for(int i=0;i<m;i++)
{
ll a,b;
cin>>a>>b;
adjLis[a].push_back(b);
adjLis[b].push_back(a);
adjMat[a][b]=1;
adjMat[b][a]=1;
}
vector<ll> ti(n+1),low(n+1);
vector<bool> vis(n+1,false);
dfs(1,adjLis,vis,ti,low,-1);
for(int i=0;i<bridge.size();i++)
{
adjMat[bridge[i][0]][bridge[i][1]]=0;
adjMat[bridge[i][1]][bridge[i][0]]=0;
}
vector<ll> count;
vector<bool> visi(n+1,false);
for(int i=1;i<=n;i++)
{
if(!visi[i])
{
count.push_back(DFS(i,adjMat,visi));
}
}
double num=0;
for(int i=0;i<count.size();i++)
{
for(int j=i+1;j<count.size();j++)
{
num+=(count[i]*count[j]);
}
}
double den=(n*(n-1))/2;
setprecision(5);
cout<<num/den<<"\n";
return 0;
}
@@@@@@