Preview:
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>

using namespace std;

// FAP nghĩa là find all path
vector<vector<int>> FAP(int A, int B, vector<vector<int>>& S) {
    deque<vector<int>> dq;
    vector<vector<int>> all;

    dq.push_back({A});
    while (!dq.empty()) {
        vector<int> path = dq.front();
        dq.pop_front();
        int end = path.back();

        if (end == B) {
            all.push_back(path);
        } else {
            for (int i = 0; i < S[end].size(); i++) {
                int nei = S[end][i];
                if (find(path.begin(), path.end(), nei) == path.end()) {
                    vector<int> newpath = path;
                    newpath.push_back(nei);
                    dq.push_back(newpath);
                }
            }
        }
    }

    // In tất cả các đường đi tìm được
    cout << "Tất cả các đường đi từ " << A << " đến " << B << ":\n";
    for (const auto& p : all) {
        for (int i = 0; i < p.size(); i++) {
            cout << p[i];
            if (i < p.size() - 1) cout << " -> ";
        }
        cout << endl;
    }

    return all; // Trả về kết quả tìm được
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> S(n + 1); // Sử dụng vector 2D để lưu đồ thị

    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        S[u].push_back(v);
        S[v].push_back(u); // Đồ thị vô hướng
    }

    int A, B; // Đỉnh bắt đầu A và đỉnh đích B
    cin >> A >> B;

    vector<vector<int>> all = FAP(A, B, S);

    vector<int> LT(n + 1, 0); // Khởi tạo mảng đếm với kích thước n+1
    for (const auto& p : all) {
        for (int i = 0; i < p.size(); i++) {
            LT[p[i]]++;
        }
    }

    int maxx = *max_element(LT.begin(), LT.end());
    for (int i = 0; i < LT.size(); i++) {
        if(i == A || i == B){
            continue;
        }
        else if (LT[i] == maxx) {
            cout << i << " ";
        }
    }
    return 0;
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter