I’m experiencing a very weird behavior on vector initialization. All codes are working fine on my local machine with inputs of 1M size, and they even pass half of systests. I simply can’t guess what could even possibly go wrong here.
Full codes are here (CodeChef: Practical coding for everyone), but what’s relevant is the following.
std::vector<int> a;
// working
void way1(){
a.resize(n);
for(int i=0; i<n; i++){
std::cin >> a[i];
}
}
// not working
void way2(){
a.reserve(n);
int ai;
for (int i = 0; i < n; i++){
std::cin >> ai;
a.push_back(ai);
}
}
// working
void way3(){
a = std::vector<int>(0);
a.reserve(n);
int ai;
for (int i = 0; i < n; i++){
std::cin >> ai;
a.push_back(ai);
}
}
How come push_back can possibly go wrong? And then here comes the standard graph representation.
std::vector<std::vector<int>> neighbors;
// working
void way1(){
neighbors = std::vector<std::vector<int>>(n);
}
// not working
void way2(){
neighbors.resize(n);
}
// not working
void way3(){
neighbors.resize(n, std::vector<int>(0));
}
// working
void way4(){
neighbors.resize(n);
for(int i=0; i<n; i++){
neighbors[i].resize(0);
}
}
Well, I really don’t know. Codechef’s compiler having some weird default constructor is my only guess.
Any help is appreciated!