The program does not return the correct output

So I was training to solve problems with old tracks of old mock interview videos and I came across this track:

Consider an undirected graph with ???? n nodes e ???? w arcs. It requires writing a program in C++ that finds a node such that its removal completely disconnects the graph, that is, it divides the graph into two or more connected components.

The program should read the number of nodes from input ???? n is the number of edges ???? w, followed by the list of edges of the graph. Next, it should determine and output a node that, if removed, completely disconnects the graph. If there is no node disconnecting the graph, the program should print an appropriate message.

Now I’ve done the program but I have a problem with the final result as it comes out as 0 regardless and I don’t understand where the problem is, below you will find the code I wrote:

#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;

// Funzione per eseguire una DFS per contare il numero di componenti connesse nel grafo
void dfs(int node, vector<vector<int>>& graph, vector<bool>& visited) {
    visited[node] = true;
    for (int neighbor : graph[node]) {
        if (!visited[neighbor]) {
            vector<bool> visited_copy = visited; 
            dfs(neighbor, graph, visited);
        }
    }
}

// Funzione per trovare il nodo che disconnette completamente il grafo
int findDisconnectingNode(vector<vector<int>>& graph, int n) {
    for (int node = 0; node < n; ++node) {
        // Memorizza lo stato originale del grafo prima di rimuovere temporaneamente il nodo
        vector<vector<int>> original_graph = graph;
        // Rimuovi il nodo dalla lista di adiacenza del grafo temporaneamente
        graph[node].clear();
        
        // Esegui una DFS per contare il numero di componenti connesse nel grafo senza il nodo
        vector<bool> visited(n, false);
        int components = 0;
        for (int i = 0; i < n; ++i) {
            if (!visited[i]) {
                dfs(i, graph, visited);
                components++;
            }
        }
        
        // Ripristina lo stato originale del grafo
        graph = original_graph;
        
        // Se ci sono più di una componente connessa, restituisci il nodo che disconnette il grafo
        if (components > 1) {
            return node;
        }
    }
    
    // Se non viene trovato nessun nodo che disconnette il grafo, restituisci -1
    return -1;
}


int main() {
    int n, w;
    cout << "Inserisci il numero di nodi e di archi:n";
    cin >> n >> w;

    // Inizializza il grafo con n nodi
    vector<vector<int>> graph(n);

    cout << "Inserisci gli archi del grafo:n";
    for (int i = 0; i < w; ++i) {
        int u, v;
        cin >> u >> v;
        // Aggiungi l'arco u-v al grafo
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    int disconnectingNode = findDisconnectingNode(graph, n);

    if (disconnectingNode != -1) {
        cout << "Il nodo che disconnette completamente il grafo e': " << disconnectingNode << "n";
    } else {
        cout << "Non esiste un nodo che disconnette completamente il grafo.n";
    }

    // Attendi l'input dall'utente prima di terminare il programma
    cin.get();
    return 0;
}
Input:
6 7
0 1
0 2
1 2
1 3
2 4
3 5
4 5

Output atteso:
The node that completely disconnects the graph is: 2

If anyone can explain to me where I went wrong and tell me what I can study more to stop making these mistakes I would be very grateful.

New contributor

Ciro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật