Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 程序因意外输出而中止,正在计算图形中无法访问的节点数_C++_Algorithm_Graph_Depth First Search - Fatal编程技术网

C++ 程序因意外输出而中止,正在计算图形中无法访问的节点数

C++ 程序因意外输出而中止,正在计算图形中无法访问的节点数,c++,algorithm,graph,depth-first-search,C++,Algorithm,Graph,Depth First Search,我刚刚实现了一个dfs算法,并考虑尝试一个问题。问题如下: 输入格式 第一行由2个整数N和M组成,表示此图中节点和边的数量。接下来的M行由2个整数a和b组成,表示节点a和b之间的无向边。下一行由单个整数x组成,表示头部节点的索引 输出格式: 10 10 8 1 8 3 7 4 7 5 2 6 10 7 2 8 10 9 2 10 5 10 2 您需要打印一个整数,表示无法从给定头节点访问的节点数 测试用例输入如下: 10 10 8 1 8 3 7 4 7 5 2 6 10 7 2 8 10 9

我刚刚实现了一个dfs算法,并考虑尝试一个问题。问题如下:

输入格式

第一行由2个整数N和M组成,表示此图中节点和边的数量。接下来的M行由2个整数a和b组成,表示节点a和b之间的无向边。下一行由单个整数x组成,表示头部节点的索引

输出格式:

10 10
8 1
8 3
7 4
7 5
2 6
10 7
2 8
10 9
2 10
5 10
2
您需要打印一个整数,表示无法从给定头节点访问的节点数

测试用例输入如下:

10 10
8 1
8 3
7 4
7 5
2 6
10 7
2 8
10 9
2 10
5 10
2

这种情况下的输出为:

0
我写的代码是:

        #include<iostream>
    #include<list>
    #include<queue>
    using namespace std;
    int mycount = 0;
    //Adj List Implementation for Integer Nodes
    class Graph {
    private:
        int V;
        
        //Array of Linked Lists of size V, V LL's are there
        list<int>* adjList;
        int* visited;
    
    public:
        Graph(int v) {
            V = v;
           
            adjList = new list<int>[V];
            visited = new int[V] { 0 };
        }
        void addEdge(int u, int v, bool bidir = true)
        {
            adjList[u].push_back(v);
            if (bidir) {
                adjList[v].push_back(u);
            }
        }
    
    
        int dfs(int vertex)
        {
            visited[vertex] = true;
            mycount++;
    
            for (auto node : adjList[vertex]) {
                if (!visited[node]) {
                    dfs(node);
                }
    
            }
            return mycount;
    
        }
    
    
    
    
    };
    
    
    
    int main() {
        //taking input of number of nodes and edges
        int inputNode, inputEdge;
        cin >> inputNode >> inputEdge;

    //graph object
        Graph g(inputNode);
//taking input
        for (int i = 0; i < inputEdge; i++) {
            int u, v;
            cin >> u >> v;
            g.addEdge(u, v);
        }
        int index;
        cin >> index;
        //run the dfs on index
        int reachabeNodes = g.dfs(index);
    
        cout << inputNode - reachabeNodes;
    
    
    
    
    
    
    
        return 0;
    }
#包括
#包括
#包括
使用名称空间std;
int mycount=0;
//整数节点的Adj列表实现
类图{
私人:
INTV;
//大小为V,V的链表数组存在
列表*调整列表;
国际*访问;
公众:
图形(INTV){
V=V;
adjList=新列表[V];
已访问=新int[V]{0};
}
无效加数(整数u,整数v,布尔比迪尔=真)
{
adjList[u]。推回(v);
if(bidir){
adjList[v]。向后推(u);
}
}
int dfs(int顶点)
{
访问[顶点]=真;
mycount++;
用于(自动节点:调整列表[顶点]){
如果(!已访问[节点]){
dfs(节点);
}
}
返回mycount;
}
};
int main(){
//获取节点数和边数的输入
int inputNode,inputtege;
cin>>inputNode>>InputGe;
//图形对象
图g(输入节点);
//接受输入
对于(int i=0;i>u>>v;
g、 附录(u,v);
}
整数指数;
cin>>指数;
//在索引上运行dfs
int reachabeNodes=g.dfs(索引);

cout您应该使用列表向量,而不是指向列表数组的指针,使用向量而不是数组。

我将数组更改为向量,但我不知道如何使用列表而不是指向列表的指针来实现它。即使将数组转换为向量,我也会遇到相同的错误。这不是真正的答案。应该是对数组的注释如果一个程序崩溃并进行原始分配,它是第一个需要解决的问题。我已经按照你的建议修改了ChrestHelyelel8Ltd.请看编辑。除了使用动态创建的列表/RAR数组之外,还有一个与实际问题的链接,你的核心问题是你所用的数据是1的,而C++使用0个索引。g、 addEdge(u-1,v-1);
我已对插入(u-1,v-1)进行了更改但程序仍在崩溃。这是问题页面的链接,因为你试图进行Christophers更改时破坏了它。不要随意编辑带有新代码的问题作为答案/注释,否则就没有意义了。天哪!它成功了。非常感谢。因此,请你详细解释一下为什么这样做有效,这样我就不必担心了我以后也会犯同样的错误。所以我使用了初始代码,并用addEdge(u,v)替换addEdge(u-1,v-1)