Problem p02241 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
using namespace std;
const int sss = 1 << 21;
int n;
int a[1000][1000];
int d[1000], p[10000], color[1000];
int prim() {
  int u, minv;
  for (int i = 0; i < n; i++) {
    d[i] = sss;
    p[i] = -1;
  }
  d[0] = 0;
  while (1) {
    minv = sss;
    u = -1;
    for (int i = 0; i < n; i++) {
      if (minv > d[i] && color[i] != 1) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1)
      break;
    color[u] = 1;
    for (int v = 0; v < n; v++) {
      if (color[v] != 1 && a[u][v] != sss) {
        if (d[v] > a[u][v]) {
          d[v] = a[u][v];
          p[v] = u;
        }
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1) {
      sum += a[p[i]][i];
    }
  }
  return sum;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int e;
      scanf("%d", &e);
      a[i][j] = e == -1 ? sss : e;
    }
  }
  printf("%d\\n", prim());
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
/*
5 6
1 2 5
2 3 8
3 4 1
4 5 2
2 5 3
2 4 6

5 6
1 3 4
1 4 3
3 4 5
2 4 1
2 5 2
4 5 6

5 6
0 2 23
0 3 1
2 3 6
3 1 8
1 4 5
3 4 1
*/

using namespace std;
const int maxn = 1e5 + 10;
int n, m;
vector<int> comp[maxn];
int num[maxn];
int ans = 0;
pair<int, pair<int, int>> edge[maxn];

void merge(int x, int y) {
  if (comp[y].size() < comp[x].size()) {
    // cout << "size of " << x << " = "<< comp [ x ].size() << "  " << "|| size
    // of " << y << " = "<< comp [ y ].size() << endl;
    for (int i = comp[y].size() - 1; i >= 0; i--) {
      // cout << comp [ y ][ i ] << " " << comp [ x ][ i ] << " ";
      comp[x].push_back(comp[y][i]);
      comp[y].pop_back();
      num[comp[y][i]] = num[comp[x][0]];
    }
    // cout <<endl;
    /*
    cout << "rasha_e moalefe1 = ";
    for (int i = 0 ; i < comp [ x ].size() ; i++ ){
            cout << comp [ x ][ i ] << " ";
    }
    cout <<endl;
    */
  } else {
    // cout << "size of " << y << " = "<< comp [ y ].size() << "  " << "|| size
    // of " << x << " = "<< comp [ x ].size() << endl;
    for (int i = comp[x].size() - 1; i >= 0; i--) {
      // cout << comp [ x ][ i ] << " " << comp [ y ][ i ] << " ";
      comp[y].push_back(comp[x][i]);
      comp[x].pop_back();
      num[comp[x][i]] = num[comp[y][0]];
    }
    // cout <<endl;
    /*
    cout << "rasha_e moalefe2 = ";
    for (int i = 0 ; i < comp [ y ].size() ; i++ ){
            cout << comp [ y ][ i ] << " ";
    }
    cout <<endl;
    */
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> edge[i].second.first >> edge[i].second.second >> edge[i].first;
  }
  sort(edge, edge + m);
  for (int i = 0; i <= n; i++) {
    num[i] = i;
    comp[i].push_back(i);
  }
  for (int i = 0; i <= m; i++) {
    // cout << edge [ i ].second.first << " " << edge [ i ].second.second <<
    // endl; cerr <<"i= " << i << "   " << num [ edge [ i ].second.first ] <<"
    // "<< num [ edge [ i ].second.second ] << endl; cerr << endl;
    if (num[edge[i].second.first] != num[edge[i].second.second]) {
      // cerr << endl;
      // cerr <<"i= " << i << "   " << num [ edge [ i ].second.first ] <<" "<<
      // num [ edge [ i ].second.second ] << endl;
      ans += edge[i].first;
      // cout << ans << " " << edge [ i ].second.first << " " << edge [ i
      // ].second.second << endl; cerr << endl;
      merge(num[edge[i].second.first], num[edge[i].second.second]);
      // cerr <<"i= " << i << "   " << num [ edge [ i ].second.first ] <<" "<<
      // num [ edge [ i ].second.second ] << endl;
    }
  }
  /*
  for ( int i = 1 ; i <= n ; i++ ){
          cout << num [ i ] << " ";
  }
  */
  cout << ans;
}