AB.devil's blog

By AB.devil, history, 4 years ago, In English

Why are the methods Vector.empty() and Vector.size() == 0 not working. I get the following error- Probably, the solution is executed with error 'not valid value for bool' on the line 34. Here are the submission links - 1. https://codeforces.com/contest/744/submission/90258791 2. https://codeforces.com/contest/744/submission/90257488 Please help!

  • Vote: I like it
  • -11
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +2 Vote: I do not like it

Replace bool[] arrays with vector

»
4 years ago, # |
  Vote: I like it +2 Vote: I do not like it

I believe the error is thrown by the "isGov" array, in which you have possibly not assigned values to all elements, but later attempt to access all of them. But since in arrays, the initial value is random, it is not recognized as a boolean value and causes an error.

I would suggest either using a vector or setting all values to false initially.

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

In which order is this evaluated? if (v[i].size() == 0 && !isGov[i])

»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

The problem is not that, I think using vector instead of bool [] will work, or you can use memset and initialize all values of isGov as false at the beginning.

»
4 years ago, # |
  Vote: I like it +10 Vote: I do not like it

In your code isGov is uninitialized

bool isGov[n+1];

should be replaced with

bool isGov[n+1]{};

You should use a vector though as others have suggested.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Does the latter guarantee that everything is 0? Same of an array of ints?

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 5   Vote: I like it +5 Vote: I do not like it

      Yes, the latter guarantees everything is zero. This feature for VLAs is a G++ extension, however. (variable-length arrays in C++ are also a G++ extension). For non-VLAs, array brace initialization is part of C++. (Variable-length arrays are arrays with a dynamic size)