// Afficher tous les chemins entre deux sommets donnés // Version en largeur (BFS) // https://www.geeksforgeeks.org/print-paths-given-source-destination-using-bfs/ #include using namespace std; // Affichage d'un chemin void printPath(const vector &path) { for(size_t i = 0; i < path.size(); i++) cout << path[i] << ' '; cout << '\n'; } // printPath() // Vérifier si un sommet x se trouve déjà dans le chemin bool isNotVisited(const int x, const vector &path) { return find(path.begin(), path.end(), x) == path.end(); } // isNotVisited() // Tous les chemins de src à dst void findPaths(const vector> &g, const int src, const int dst) { queue> q; // file d'attente des sommets traversés vector path; // chemin path.push_back(src); q.push(path); while(!q.empty()) { path = q.front(); q.pop(); int last = path[path.size() - 1]; // dernier sommet visité if(last == dst) // destination atteinte printPath(path); // On traverse tous les voisins directs du sommet courant (last) for(size_t i = 0; i < g[last].size(); i++) { if(isNotVisited(g[last][i], path)) { vector newpath(path); newpath.push_back(g[last][i]); // on ajoute le voisin q.push(newpath); } } } } // findPaths() // Programme principal int main() { int V = 0, A = 0; cin >> V; // sommets, arcs vector> graphe(V); int x, y; for(int k = 0; k < A; k++) { cin >> x >> y; graphe[x].push_back(y); } int src = 0, dst = V - 1; findPaths(graphe, src, dst); return 0; } // main()