zeosutt's blog

By zeosutt, 10 years ago, In English

As you might know, stderr doesn't affect a judgement. Therefore, if you use stderr instead of stdout for debugging, you won't get WA even if you forget to delete (or comment out) the lines for debugging.

Of course, you can never get AC if your solution is wrong in the first place :)

  • Vote: I like it
  • +13
  • Vote: I do not like it

»
10 years ago, # |
  Vote: I like it +22 Vote: I do not like it
vector<int> adj[N];

void dfs(int node, int level) {
	cerr << node << " " << level << "\n";
	// do something
	for (int neighbor : adj[node])
		dfs(neighbor);
}

the runtime of the above code would surely decrease if the cerr statements were removed.
and if the recursion runs for nearly 106 nodes, it could easily result in the solution getting TLE instead of AC.

  • »
    »
    10 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    We are facing a trade-off between coding time and running time.

    When our solution seems good, we should remove some debugging statements which consump much time. I think it is good idea.

  • »
    »
    10 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Oh, sorry. I completely forgot an effect on running time. Certainly, the effect of debugging statements in such a case cannot be ignored. Therefore, please think using stderr instead of stdout is only an insurance against WA.

    • »
      »
      »
      10 years ago, # ^ |
        Vote: I like it +27 Vote: I do not like it

      Codeforces defines an ONLINE_JUDGE macro for C/C++ programs. Therefore it is possible to write a template where the debugging output works on a local machine, and turns into nothing on Codeforces. A very simple implementation:

      #ifndef ONLINE_JUDGE
      #  define LOG(x) (cerr << #x << " = " << (x) << endl)
      #else
      #  define LOG(x) 0
      #endif

      Then just write LOG(i); in the program.

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

Deleting cerr's when submitting can be done in a following way: 1. Replace (Ctrl+R in Kate)

  1. cerr -> //cerr

:P

Defining own macros is a good idea, but sometimes we want something mote than just printing the value of a variable (for example additional comments written by us to make them readable). By the way if we have macro in our code like that LOG(x) and use it in a code, we should take care of line with that define in a replace method I explained earlier :P.

  • »
    »
    10 years ago, # ^ |
      Vote: I like it -14 Vote: I do not like it

    Well, we may define so that it's as easy to write comments to it as to cerr (Well not very easy:) )

    #ifndef ONLINE_JUDGE
    #  define LOG(x) cerr << x << endl; //you may call it as LOG("i want to print " << x << "  thats a value of x")
    #else
    #  define LOG(x)
    #endif
    

    But you should be careful with values like LOG(1<<2)

  • »
    »
    10 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    In my actual implementation of LOG() (which uses C++11 variadic templates) this works: the code

    int a = 42;
    vector<int> b = { 1, 2, 3 };
    
    LOG(a, b, "comment");

    will print

     main 80: a, b, "comment" = 42, [1, 2, 3], comment

    It's not much, but I'll try to polish the code and put it on Github so that other people can see it sometime later.

»
5 years ago, # |
  Vote: I like it -18 Vote: I do not like it

It doesn't give WA but actually it does give TLE check out 76023053 gives TLE and the other 76022926 is Accepted.

So it's always better to comment out or delete stderr statements before submitting

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

    Yes.

    That was the idea behind the very first comment written under this post.

    Six years ago.

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

I'm getting WA while using stderr. wrong solution using cerr (debug(u)) 178151957. Correct solution by commenting that line 178151812. Am I missing something?

  • »
    »
    2 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    It has nothing to do with cerr rather your for-loop and #define debug(x).

    When you uncomment

    // for(auto u: temp) debug(u)

    m[h]++;

    m[h] get incremented because for-loop never finds a ; neither after you debug(u) nor in your #define debug(u)

    Either put a ; in #define debug(u) ; or after debug(x) in for loop or wrap it in {}.

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

      Flower08 thanks for helping I was missing such a silly thing. Btw I saw your profile, it seems like your cf journey is quite similar to mine.