Hi, I was solving "Help The Commander in Chief" problem on SPOJ
SPOJ.com - Problem KFSTB[
^]My code is giving a wrong answer, and I'm really interested in knowing why..
My algorithm:
Use memorization to recursively calculate number of paths from the starting point to the end point.
And here's the code:
using size_type = unsigned long long int;
using ushort = unsigned short;
size_type paths(const Map &m)
{
if (m.start() == m.end()) return 1;
map<ushort, size_type> h;
h[m.end()] = 1;
count(m, m.start(), h);
return h[m.start()];
}
void count(const Map &m, ushort p, map<ushort, size_type> &h)
{
for (ushort i = 1; i < m[p].size(); i++) {
if (h.find(m[p][i]) == h.end())
count(m, m[p][i], h);
h[p] += h[m[p][i]];
}
}
I want to know what special cases, for example, that I didn't deal with correctly, What is fault about this algorithm (or code) ?
Thanks
- Sam
What I have tried:
I have tried some simple modifications, like trying in countHelp function to not visit the same node twice(or same p), but it didn't help, I also googled and found solutions like topological sort and dp. but actually I don't care about solving it right now..