网易2018校招笔试题-游历魔法王国-只能过60%-如何优化
我的回溯法代码
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <unordered_map>
#include <cstdio>
using namespace std;
int F2(/*unordered_*/map<int, set<int>>&p, int left, int n, unordered_map<int,int>&mp, int city) {
int ans = 0;
ans = max(ans, (int)mp.size());
if (left) {
auto &ns = p[city];
for (auto i:ns) {
mp[i]++;
ans = max(ans, F2(p, left - 1, n, mp, i));
mp[i]--;
if (mp[i] == 0) mp.erase(i);
}
}
return ans;
}
int main() {
int n, L;
scanf("%d%d",&n,&L);
/*unordered_*/map<int, set<int>> p;
int i = 0, j = 0;
for (i = 0; i < n-1; i++) {
scanf("%d", &j);
p[i + 1].insert(j);
p[j].insert(i+1);
}
unordered_map<int, int>mp;
mp[0]++;
printf("%d\n",F2(p, L, n,mp,0));
return 0;
}
不知道如何继续优化,请大神帮忙orz