C++ 邻接表图 DFS

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <climits>
#include <unordered_map>
#include <set>

using namespace std;

vector<int> read_line_data();
vector<int> read_line_data(char split);
void output_data(const vector<int>& data);

class Node {
public:
    int val;
    vector<Node*> neighbors;
    Node() {
        val = 0;
        neighbors = vector<Node*>();
    }
    Node(int _val) {
        val = _val;
        neighbors = vector<Node*>();
    }
    Node(int _val, vector<Node*> _neighbors) {
        val = _val;
        neighbors = _neighbors;
    }
};

class Graph {
public:
    int v_num, e_num;
    vector<Node*> headNodes;
    vector<bool> visited;

    Graph(int v, int e, vector<pair<int, int>> nodes) {
        v_num = v;
        e_num = e;
        headNodes.resize(v_num, nullptr);
        visited.resize(v_num + 1, false);
        set<int> head;
        for (int i = 0; i < e; i++) {
            int first = nodes[i].first;
            int second = nodes[i].second;
            if (headNodes[first- 1] == nullptr) {
                headNodes[first - 1] = new Node(first);
            }
            if (headNodes[second - 1] == nullptr) {
                headNodes[second - 1] = new Node(second);
            }

            headNodes[first - 1]->neighbors.push_back(headNodes[second - 1]);
            headNodes[second - 1]->neighbors.push_back(headNodes[first - 1]);
        }
    }

    void DFS(int index) {
        Node* node = headNodes[index];
        if (!visited[node->val]) {
            visited[node->val] = true;
            cout << node->val << " ";
        }
        for (int i = 0; i < headNodes[index]->neighbors.size(); i++) {
            int value = headNodes[index]->neighbors[i]->val;
            if (!visited[value]) {
                visited[value] = true;
                cout << value << " ";
                DFS(value - 1);
            }
        }
    }

};

int main() {
    int e;
    cin >> e;
    vector<pair<int, int>> nodes;
    set<int> nodes_v;
    for (int i = 0; i < e; i++) {
        pair<int, int> node;
        vector<int> data = read_line_data(',');
        node = make_pair(data[0], data[1]);
        nodes.push_back(node);
        nodes_v.insert(data[0]);
        nodes_v.insert(data[1]);
    }
    Graph g(nodes_v.size(), e, nodes);
    g.DFS(0);
}

vector<int> read_line_data() {
    string src;

    getline(cin, src);
    istringstream iss(src);

    vector<int> data;
    int token;
    while (iss >> token) {
        data.push_back(token);
    }

    return data;
}

vector<int> read_line_data(char split) {
    string input;

    cin >> input;
//    getline(cin, input);
    vector<int> data;

    // 使用字符串流 (stringstream) 分割输入数据
    stringstream ss(input);
    string token;
    while (getline(ss, token, split)) {
        // 将分割后的字符串转换为整数并添加到向量中
        int value = stoi(token);
        data.push_back(value);
    }

    return data;
}

void output_data(const vector<int>& data) {
    int n = data.size();
    for (int i = 0; i < n; i++) {
        cout << data[i] << " ";
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值