Recursive DFS produce Segmentation fault (core dumped) in c++

34 Views Asked by At

I have the following code and have been trying for hours to find the segmentation fault (core dumped). Unfortunately, I haven't found it yet and am a bit desperate. I get the error with the following input: 8 0 4 5 3 1 2 5 1 1 5 4 8 3 6 3 7 As background information, this is a task related to Game Theory, where one player always chooses the highest value and the other tries to take the lowest value.

And I know that I shouldn't use the namespace and should rather include the individual libraries.

This is my code who produce the Error, hopefully someone else see the Problem

#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> graph;
vector<vector<int>> dp;
vector<int> price;

int dfs(int a, int b){  
    if(dp[b][a]!= -1){
        return dp[b][a];
    }

    if(!graph[a].size()){
        return dp[b][a] = price[a];
    }

    int pri = 0;
    if(b == 0){
        pri = dfs(a,1);
    }else{
        pri = INT_MAX;
    }
    int siz = graph[a].size();
    for(int i = 0; i < siz; i++){
        cout << " t:"<< i<< endl;
        int ed= graph[a][i];
        if(b == 0){
            pri = max(pri, dfs(graph[a][i],1));
        }else{
            pri = min(pri, dfs(graph[a][i],0));
        }
    }    
    return dp[b][a] = pri;
}

int main(){
    int n;
    cin >> n;
    dp.resize(2,vector<int>(n+1,-1));
    graph.resize(n+1);
    price.resize(n);
    for(int i = 0; i < n; i++){
        int x,y;
        cin >> x >> y;
        graph[x].push_back(i);
        price[i] = y;
    }
    cout << dfs(1,0) << endl;
}
0

There are 0 best solutions below