#include #include #include using namespace std; typedef vector> graph; void print(graph g) { for (int i = 0; i < int(g.size()); ++i) { cout << i + 1 << ": "; for (int j = 0; j < int(g[i].size()) - 1; ++j) cout << g[i][j] + 1 << ", "; if (g[i].size() > 0) cout << g[i].back() + 1; else cout << "нет смежных вершин"; cout << ";" << endl; } } void dfs(int x, graph g, vector *path, vector *used) { (*used)[x] = 1; path->push_back(x); for (int i = 0; i < int(g[x].size()); ++i) { int node = g[x][i]; if ((*used)[node] == 0) dfs(node, g, path, used); } } void dfs(int x, graph g, vector *path) { int n = int(g.size()); vector used(n); dfs(x, g, path, &used); } int main() { cout << "Введите количество вершин: "; int n; cin >> n; cout << "Введите количество рёбер: "; int k; cin >> k; graph g(n); cout << "o----------------------o" << endl; cout << "| Нумерация вершин с 1 |" << endl; cout << "o----------------------o" << endl; cout << "Введите рёбра (ориентированные):" << endl; for (int i = 0; i < k; ++i) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); } cout << "Введённый граф:" << endl; print(g); cout << "Введите анализируемую вершину: "; int a; cin >> a; a--; vector nodes; for (int i = 0; i < n; ++i) { if (i == a) continue; vector path; dfs(i, g, &path); if (find(path.begin(), path.end(), a) != path.end()) nodes.push_back(i); } if (nodes.size() == 0) cout << "В вершину " << a + 1 << " нет путей из других вершин" << endl; else { cout << "В вершину " << a + 1 << " есть пути из вершин: "; for (int i = 0; i < int(nodes.size()) - 1; ++i) cout << nodes[i] + 1 << ", "; cout << nodes.back() + 1 << endl; } return 0; }