Getting WA -Roads and Libraries HACKERRANK

#define ll long long 
#define pb push_back
using namespace std;
vector<int> adj[100001];
bool visited[100001];
int nodes;
void dfs(int node)
{
    visited[node]=true;
    nodes++;
    for(int child:adj[node])
    {
        if(!visited[node])
        {
            dfs(child);
        }
    }
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll n,m,clib,croad;
        cin>>n>>m>>clib>>croad;
        for(int i=0;i<m;i++)
        {
            int x,y;
            cin>>x>>y;
            adj[x].pb(y);
            adj[y].pb(x);
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(!visited[i])
            {
                nodes=0;
                dfs(i);
                if(clib<croad)
                {
                    ans+=(nodes*clib);
                }
                else
                {
                    ans+=(nodes-1)*croad;
                }
            }
        }
        cout<<ans<<"\n";

        for(int i=0;i<=n;i++)
        {
            adj[i].clear();
            visited[i]=false;
        }
    }
}```


can some tell me what is wrong with my solution?

https://www.hackerrank.com/challenges/torque-and-development/problem

you have to add the minimum of nodes*clib, clib+(nodes-1)*croad.

1 Like