Relocation truncated to fit error

beginner
c-plus-plus

#1

The following program is giving a relocation truncated to fit error which I am not able to resolve, please help!!

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,m,k;
vector<ll> mus(n);
bool edge[1000000][1000000]={};
vector<bool> vis(n,false);
vector<long long> sum(n,0);
int comp=-1;
void dfs(int s){
if(vis[s])return;
vis[s]=true;
sum[comp]=sum[comp]+mus[s];
for(int i=0;i<n;i++)
{
if(edge[s]*)dfs(i);
}
}
int main(){
ll t;
cin>>t;
while(t--){
cin>>n>>m>>k;
for(ll i=0;i<m;i++){int a,b;
cin>>a>>b;edge[a]**=1;edge**[a]=1;
}
for(ll i=0;i<n;i++)
cin>>mus*;
for(ll i=0;i<n;i++)
{
if(!vis*){comp++;
dfs(i);}
}
if(comp<k-1){cout<<"-1

“;continue;}
sort(sum.begin(),sum.end());
long long suma=0;
int a=0,b=comp-1;for(ll i=0;i<k;i++){
if(i%2==0){suma=suma+sum**;b–;}
else {suma=suma+sum[a];a++;}
}
cout<<suma<<”
";
}
return 0;
}


#2

You just used 1 Terabytes of memory in edge[1000000][1000000]

Every question has a maximum memory limit given. Generally in Megabytes.

There is another way of storing connected components in a graph using less memory.

[Cpp] Simple dfs using vector

Geeksforgeeks Implementation

[Cpp] Simple dfs using Map


#3

it’s a bool, so it’s 1 TeraByte imo


#4

Oh yes, true. I’ve edited.


#5

Can a map also be used?


#6

Yes, you can use Map for dfs. I’ve added the simple implementation in the answer.