Relocation truncated to fit error

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][i])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][b]=1;edge[b][a]=1;
}
for(ll i=0;i<n;i++)
cin>>mus[i];
for(ll i=0;i<n;i++)
{
if(!vis[i]){comp++;
dfs(i);}
}
if(comp<k-1){cout<<"-1\n";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];b--;}
else {suma=suma+sum[a];a++;}
}
cout<<suma<<"\n";
}
return 0;
}

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

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

2 Likes

Oh yes, true. I’ve edited.

Can a map also be used?

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