0

I was trying to take input from a file and process it and then write the output to another file but I am getting runtime error. For small cases, the code works fine but for large cases, it gives runtime error.

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
int main()
{
    ofstream output;
    output.open("testOut.txt");
    ifstream input;
    input.open("input4.txt");
    if(input.is_open())cout<<"input opened\n";
    if(output.is_open())cout<<"output opened\n";
    int v, e;
    input >> v >> e;
    int connection[v + 1] = {0};
    int segTree[4 * v + 4];
    vector<int> graph[v + 1];
    int totalSum = 0;
    for(int i=0;i<e;i++)
    {
        int e1,e2;
        input>>e1>>e2;//Here I am getting the error
        graph[e1].push_back(e2);
        graph[e2].push_back(e1);
        connection[e1]++;
        connection[e2]++;
        totalSum += 2;
    }
    int size = v;
    buildTree(connection, segTree, 1, v, 1);
    while (totalSum > 0)
    {
        int index = segTree[1];
        for (int i = 0; i < graph[index].size(); i++)
        {
            if (connection[graph[index][i]] != -1)
            {
                update(connection, segTree, 1, v, graph[index][i], 1, connection[graph[index][i]] - 1);
                totalSum -= 2;
            }
        }
        update(connection, segTree, 1, v, index, 1, -1);
        size--;
    }
    cout << size << "\n";
    for (int i = 1; i < v + 1; i++)
    {
        if (connection[i] == -1)
            cout << "0 ";
        else
            cout << "1 ";
    }
    output.close();
    input.close();
    return 0;
}

Here is the link to the input file (B4.txt).It is an a zip file containing 4 text files.

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • 1
    Have you tried a debugger? Seems obvious that you are writing outside of the bounds of one of your arrays. Either a debugger, or simply adding some bounds checking will quickly tell you where the problem is – john Jun 26 '20 at 07:51
  • 3
    `vector graph[v + 1];` is really odd. An array of vectors? Why not always use vectors? VLAs are not part of C++ and are allocated on the stack, I think. The stack is quite small, maybe you just allocate too much stack memory. – Lukas-T Jun 26 '20 at 07:51
  • @churill I'm guessing the OP doesn't know how to handle 2D vectors. – john Jun 26 '20 at 07:52
  • @john Maybe, but OP has 2 more VLAs right above that could be easily converted to `vector` – Lukas-T Jun 26 '20 at 07:53
  • Which of course would be `vector> graph(v + 1);` – john Jun 26 '20 at 07:53
  • And (for completeness sake) `vector connection(v + 1);` and `vector segTree(4 * v + 4);`. (Maybe doesn't know how to handle a vector with an initial size either). – john Jun 26 '20 at 07:55
  • vector implementations often include a lot of debugging checks. Converting your code to use vectors thoughout (as suggested in comments above) will not only make your code compliant with standard C++, it might also identify your problem. – john Jun 26 '20 at 07:56
  • You should also read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h), Quite fascinating, that in competetive programming people try to write fast programs but then slow down compilation by a magnitude because they include the whole standard library for 50 lines of code. – Lukas-T Jun 26 '20 at 08:02

0 Answers0