猿辅导8.1笔试第二题代码,请各位大佬看看错在哪
代码提交时一直段错误,是爆栈了吗?看了好几遍,实在不知道哪错了!😫😔
#include <bits/stdc++.h> using namespace std; #define LL long long const int MAXN = 1e5 +50; const int MOD = 1e9 +3; int N; vector<int> child[MAXN]; int val[MAXN]; int maxSub(int root) { int ret = 0; for(int ch:child[root]) { int cur = maxSub(ch); if(cur > 0) ret = (ret + cur)% MOD; } if(val[root] > 0) ret = (ret + val[root]) % MOD; return ret; } int main() { while(cin >> N) { int v,fa; for(int i = 0; i < N; i++) child[i].clear(); for(int i = 0; i < N; i++) { cin >> v >> fa; val[i] = v; if(i != 0) child[fa - 2].push_back(i); } /*for(int i = 0; i < N; i++) { cout << "ch:" << " "; for(int ch:child[i]) cout << ch << " "; cout << endl; }*/ int res = maxSub(0); cout << res << endl; } return 0; }