Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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+;中获取深度优先搜索的运行时错误+;代码_C++_Function_Graph_Runtime_Depth First Search - Fatal编程技术网

C++ 在c+;中获取深度优先搜索的运行时错误+;代码

C++ 在c+;中获取深度优先搜索的运行时错误+;代码,c++,function,graph,runtime,depth-first-search,C++,Function,Graph,Runtime,Depth First Search,我有一个无向图。我试图找到除了给定节点的相邻节点之外的最大节点值 例如: 6 // number of nodes 1 3 2 3 3 4 4 5 4 6// edges 我必须为所讨论的节点找到除直接相邻节点以外的最大节点值 例如,如果我使用DFS(1),答案必须是6。我的代码运行得非常好,但如果我多次调用DFS()时出现运行时错误 #include<iostream> #include<stack> #include<list> #inclu

我有一个无向图。我试图找到除了给定节点的相邻节点之外的最大节点值

例如:

6 // number of nodes

1 3

2 3

3 4

4 5

4 6// edges 
我必须为所讨论的节点找到除直接相邻节点以外的最大节点值

例如,如果我使用DFS(1),答案必须是6。我的代码运行得非常好,但如果我多次调用DFS()时出现运行时错误

#include<iostream>
#include<stack>
#include<list>
#include<map>
#include<utility>
#include<vector>
class graph
{
    std::list<int> *adj;
    int V;
public:
    graph(int V);
    void add(int v, int w);
    void DFS(int s);
};
int max = -1;
graph::graph(int V)
{
    this->V = V;
    adj = new std::list<int>[V];
}
void graph::add(int v, int w)
{
    adj[v].push_back(w);
    adj[w].push_back(v);
}
void graph::DFS(int s)
{
    std::vector<int> visited(V, false);
    std::stack<int> st;
    st.push(s);
    while (!st.empty())
    {
        int t = st.top();
        st.pop();
        if (!visited[t])
        {
            visited[t] = true;
        }
        for (auto i = adj[t].begin(); i != adj[t].end(); i++)
        {
            if (!visited[*i])
            {
                visited[*i] = true;
            }

        }
        for (int j = V; j >= 0; j--)
        {
            if (visited[j] == 0)
            {
                std::cout << j << " ";
                break;
            }
        }

    }

}
int main()
{
    int t = 0, k = 0;
    graph g(6);

    g.add(1, 3);

    g.add(2, 3);

    g.add(3, 4);

    g.add(4, 5);

    g.add(5, 6);

    g.DFS(1);
    g.DFS(2);
    g.DFS(3);
    return 0;

}
#包括
#包括
#包括
#包括
#包括
#包括
类图
{
标准::列表*adj;
INTV;
公众:
图形(INTV);
无效添加(整数v,整数w);
无效DFS(INTS);
};
int max=-1;
图形::图形(intv)
{
这个->V=V;
adj=新标准::列表[V];
}
无效图::添加(int v,int w)
{
形容词[v]。推回(w);
形容词[w]。推回(v);
}
无效图::DFS(int s)
{
std::访问的向量(V,false);
std::stack st;
圣普什(s);
而(!st.empty())
{
int t=st.top();
圣普();
如果(!已访问[t])
{
访问[t]=真;
}
for(auto i=adj[t].begin();i!=adj[t].end();i++)
{
如果(!访问[*i])
{
访问[*i]=正确;
}
}
对于(int j=V;j>=0;j--)
{
如果(访问[j]==0)
{

std::难道你不幸走了那么远吗。在
图中
构造函数
adj=new std::list[V];
生成一个大小为
V
list
数组
V
被指定为6。后面的'g.add(5,6);`尝试
adj[w]。push_back(V);
解析为
adj[6]。push_back(5);
并在数组外部写入。数组的原点是0,因此大小为6的数组可以从0到5进行索引。@user4581301非常感谢您理解了这个错误。