在acces上初始化为0的int向量给出非零值

时间:2019-01-24 20:27:28

标签: c++ vector

我写了一个简单的算法来查找图中的奇数循环。我有一个访问过的向量,它告诉我是否访问了向量,所以将其初始化为0。

#include <iostream>
#include <vector>
#include <set>

#define UNVISITED 0
#define VISITED 1

using namespace std;

int vertices, edges;

vector<vector<int>> graph;
vector<int> visited;
vector<int> times;

int time_ = 1;
int hasOddCycle = false;

void dfs(int vertex) {
    if (visited.at(vertex) == VISITED)
        return;
    visited.at(vertex) = VISITED;
    times.at(vertex) = time_;
    time_++;

    for (auto elem: graph.at(vertex)) {
        if (visited.at(elem) == VISITED) {
            if (times.at(vertex) - times.at(elem) % 2 == 0)
                hasOddCycle = true;
        } else {
            dfs(elem);
        }
    }
}

int main() {
    cin >> vertices >> edges;

    for (int i = 0; i <= vertices; i++) {
        visited.emplace_back(UNVISITED);
        graph.emplace_back(vector<int>());
        times.push_back(0);
    }

    int from, to;
    for (int i = 0; i < edges; i++) {
        cin >> from >> to;
        graph.at(from).push_back(to);
        graph.at(to).push_back(from);
    }

    for (int i = 1; i <= vertices; i++) {
        dfs(i);
        if (hasOddCycle) {
            cout << "NO" << endl;
            return 0;
        }
    }

    cout << "YES" << endl;

    return 0;
}

当我运行具有给定数据的代码时,将调用dfs(1)并将访问设置为1到0。dfs循环中的第一个元素为2,因此我检查是否访问了顶点2,并且无缘无故地给出了真值! !!我不知道为什么会这样...

输入数据(顶点,边数和顶点):

5 6
1 2
2 3
3 4
4 1
1 5
5 3

1 个答案:

答案 0 :(得分:0)

您的访问验证已关闭,并且全局time_变量使其难以跟踪。由于times向量提供的信息与visited向量更多,因此我删除了visited。每次您从dfs调用main时,图中每个顶点的访问时间计数都会增加1。函数返回时,它们都将具有相同的访问次数。这是跟踪访问的另一种方法,使跟踪变得更容易了:

#include <iostream>
#include <vector>
#include <set>

using namespace std; // bad practice

int vertices, edges;

vector<vector<int>> graph;
vector<int> times;

void dfs(int vertex) {
    static int indent = 1; // to indent recursion level in debug print

    int time_ = ++times.at(vertex);

    for (auto elem : graph.at(vertex)) {
        if (times.at(elem) != time_) {
            std::cout << std::string(indent, ' ') << "dfs(" << elem
                      << ") in graph @ vertex " << vertex << "\n";
            ++indent;
            dfs(elem);
            --indent;
        }
    }
}

int main() {
    cin >> vertices >> edges;

    times.resize(vertices+1);
    for (int i = 0; i <= vertices; i++) {
        graph.emplace_back(vector<int>());
    }

    int from, to;
    for (int i = 0; i < edges; i++) {
        cin >> from >> to;
        graph.at(from).push_back(to);
        graph.at(to).push_back(from);
    }

    for(int v=1; v<=vertices; ++v) {
    std::cout << "\nchecking graph from vertex " << v << "\n";
        dfs(v);
        for (int i = 1; i <= vertices; i++) {
            if (times[i] != v) {
                std::cout << " Error\n";
                return 0;
            }
        }
        std::cout << " all vertices has " << v << " visitation(s)\n";
    }

    return 0;
}

输出:

checking graph from vertex 1
 dfs(2) in graph @ vertex 1
  dfs(3) in graph @ vertex 2
   dfs(4) in graph @ vertex 3
   dfs(5) in graph @ vertex 3
 all vertices has 1 visitation(s)

checking graph from vertex 2
 dfs(1) in graph @ vertex 2
  dfs(4) in graph @ vertex 1
   dfs(3) in graph @ vertex 4
    dfs(5) in graph @ vertex 3
 all vertices has 2 visitation(s)

checking graph from vertex 3
 dfs(2) in graph @ vertex 3
  dfs(1) in graph @ vertex 2
   dfs(4) in graph @ vertex 1
   dfs(5) in graph @ vertex 1
 all vertices has 3 visitation(s)

checking graph from vertex 4
 dfs(3) in graph @ vertex 4
  dfs(2) in graph @ vertex 3
   dfs(1) in graph @ vertex 2
    dfs(5) in graph @ vertex 1
 all vertices has 4 visitation(s)

checking graph from vertex 5
 dfs(1) in graph @ vertex 5
  dfs(2) in graph @ vertex 1
   dfs(3) in graph @ vertex 2
    dfs(4) in graph @ vertex 3
 all vertices has 5 visitation(s)