Skip to main content
Code Review

Return to Revisions

3 of 3
Commonmark migration

Your code is fairly readable, which is good. I see a few things that could be improved, though.

Bugs

In main() you are allocating n + 1 elements and then iterating from 1 to n. C-based languages use 0-based arrays, so always allocating 1 extra and ignoring index 0 is likely to introduce bugs as future programmers (including a future you) assume that an array goes from 0 to n - 1. I know this because I've had to maintain such code and it was a source of bugs. It's also wasteful because every array has an extra element in it that's never used.

That said you have a current bug in this code:

edges = new vector<int>[n + 1];
for (int j = 0; j < m; ++j) {
 int s, e;
 cin >> s >> e;
 edges[s].push_back(e);
}

The instructions say there are m edges. You've allocated n + 1 edges which may be too many or too few. If it's too few, then you will overwrite memory and mess up your heap.

Simplify

Your length() function runs a loop when it doesn't need to. You can do it like this:

int length(const int n)
{
 if ((n == 0) || (n == 1))
 {
 return 1;
 }
 
 return ceil(log10(n));
}

Likewise, your concat() function could remove the loop with a call to log10() and a call to pow().

Whether either of these is faster than what you've written or not probably depends on the number of decimal digits in the vertices. I recommend trying it and profiling to see which is faster.

The end of your dfs() function is written very confusingly and is redundant. I would simplify it by doing this instead:

if (q == -1)
{
 valueC[i][reminder] = -2;
}
else
{
 valueC[i][reminder] = concat(value[i], q);
}
return valueC[i][reminder];
user1118321
  • 11.9k
  • 1
  • 20
  • 46
default

AltStyle によって変換されたページ (->オリジナル) /