diff options
| author | Andrew Guschin <saintruler@gmail.com> | 2021-03-30 09:43:56 +0400 |
|---|---|---|
| committer | Andrew Guschin <saintruler@gmail.com> | 2021-03-30 09:43:56 +0400 |
| commit | dab8347a008c78708c62e271ce21c2af83730629 (patch) | |
| tree | a2ae40928c329c999989cb05900d017fb236c4ae | |
| parent | 5e6dc29068bf980bacd9c5421341c66fabc838ac (diff) | |
Добавил четвёртую задачу
| -rw-r--r-- | graphs/task4.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/graphs/task4.cpp b/graphs/task4.cpp new file mode 100644 index 0000000..ff011cc --- /dev/null +++ b/graphs/task4.cpp @@ -0,0 +1,86 @@ +#include<iostream> +#include<stdio.h> +#include<vector> + +using namespace std; + +typedef vector<vector<int>> 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<int> *path, vector<int> *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<int> *path) +{ + int n = int(g.size()); + vector<int> used(n); + return 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); + g[b].push_back(a); + } + + cout << "Введённый граф:" << endl; + print(g); + + vector<int> path; + dfs(0, g, &path); + + if (int(path.size()) == n) + cout << "Граф является связным" << endl; + else + cout << "Граф не является связным" << endl; + + return 0; +} |