I have to solve a problem and i need your help, so thanks in advance. As you can see, title says almost everything. There is a file "domes.in" (which is a textfile), which contains 10.000.000 pairs of integers From (1 - 100.000), so in the first part, I have to save all those numbers, in less than 1 sec.
First, I have tried creating an int array of 20.000.000 places, so that i could save the numbers one by one The size of the array is so big that the program does not respond.
Before you read the code, read the task, but please just answer my question and do not post any code, because it is a problem of an online competition and i would like to write it by myself.
#include <fstream>
using namespace std;
int main()
{
int n, m, a, b; //N(Value) M(Pairs)
int t = 0;
int r = 0;
int j;
ifstream infile("domes.in");
infile >> n >> m; //N = 100.000 M = 10.000.000
int domes[m*2];
for (j=0; j<m; j++) //For 1 to 10.000.000
{
infile >> a >> b; //Save 10.000.000 Integers
domes[t++] = a;
domes[t++] = b;
}
for (j=1; j<=n; j++) //For J = 1 - 100.000
{
int i=0;
for (int k=0; k<t; k++) //If the point J is appeared
if (domes[k] == j) //+1 Link
i++;
if (i == 1) //If Links < 2 (of number J)
r++; //+1 Point is connected with less than 2 points
} //Else move on to ++J
infile.close();
ofstream outfile("domes.out");
outfile << r;
return 0;
}
Now it seems that it might work, but again the program doesn't respond (In runtime, in raises an error, which says that the program stopped running. There are any other build errors or mistakes in the code).
What am i doing wrong?
The TextFile: 100000 10000000
2344 3444
3345 4564
5566 9455 //Random integers up to 100.000
//..........
//Edit: I Removed the struct "DOMES" from the code, since it was the same thing
The Task: I will explain it with my own words, because it is complicated. The are 100.000 points on the map. These points, are connected (There are 10.000.000 links). I have to find the points which have only a single connection (Each Point has at least 1 connection).
For example:
5 4 (N = 5, M = 4) N(Max Value) M(Pairs)
1 2
2 3
4 5
3 4
There are 2 points that have a single connection: 1 and 5 (1 is connected with 2) and (5 is connected with 4). The rest of the points are connected at least twice:
2: 1-2, 2-3
3: 2-3, 3-4
4: 4-3, 4-5