Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix stack size limit #10

Closed
thecodingwizard opened this issue Jan 14, 2022 · 0 comments
Closed

fix stack size limit #10

thecodingwizard opened this issue Jan 14, 2022 · 0 comments

Comments

@thecodingwizard
Copy link
Member

CSES Subordinates:

AC Code

#include "bits/stdc++.h"
 
int dfs(int node, std::vector<std::vector<int>>& a, std::vector<int>& ans) {
    int temp = 0;
    for(auto& i : a[node]) temp += dfs(i, a, ans);
    return ans[node] = 1 + temp;
}
 
signed main() {
    std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);
    int n; std::cin >> n;
    std::vector<std::vector<int>> a(n + 1);
    std::vector<int> ans(n + 1, 0);
    for(int i = 2, x; i <= n; ++i) {
        std::cin >> x;
        a[x].push_back(i);
    }
    dfs(1, a, ans);
 
    for(int i = 1; i <= n; ++i) std::cout << ans[i] - 1 << " ";
}

gives RTE on our grader. I think this is because our stack size is too small. Fix with ulimit -s unlimited: https://stackoverflow.com/questions/14471564/what-does-ulimit-s-unlimited-do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant