diff options
| author | Andrew Guschin <saintruler@gmail.com> | 2021-03-29 12:49:33 +0400 |
|---|---|---|
| committer | Andrew Guschin <saintruler@gmail.com> | 2021-03-29 12:49:33 +0400 |
| commit | f29b59c5244703475d2925dc6f8e9a63c982536d (patch) | |
| tree | 1a5cdf1214535cb6661d687e3c7dddb8cb77b1c0 | |
| parent | 8557b2421ffc5ab15e9aaf50a7dc07f97a926f4e (diff) | |
Добавил 12 задачу в графах
| -rw-r--r-- | graphs/Makefile | 3 | ||||
| -rw-r--r-- | graphs/task12_1.cpp | 63 | ||||
| -rw-r--r-- | graphs/task1_1.cpp | 31 |
3 files changed, 90 insertions, 7 deletions
diff --git a/graphs/Makefile b/graphs/Makefile index 85306ba..bde760b 100644 --- a/graphs/Makefile +++ b/graphs/Makefile @@ -17,8 +17,7 @@ test1_1: task1_1 task12_1: $(COMPILE) -o task.out task12_1.cpp test12_1: task12_1 - @printf "" | ./task.out - @printf "Answer: \n" + @printf "4\n5\n1 2\n1 3\n1 4\n2 3\n3 4\n4 2" | ./task.out task9_1: $(COMPILE) -o task.out task9_1.cpp diff --git a/graphs/task12_1.cpp b/graphs/task12_1.cpp new file mode 100644 index 0000000..380d5ac --- /dev/null +++ b/graphs/task12_1.cpp @@ -0,0 +1,63 @@ +#include<iostream> +#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 (auto node : g[i]) + cout << node + 1 << ", "; + cout << endl; + } + +} + +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); + + cout << "Введите вершины, которые необходимо соединить:" << endl; + int p, q; + cin >> p >> q; + p--; + q--; + + g[p].push_back(q); + g[q].push_back(p); + + cout << "Полученный граф:" << endl; + print(g); + + return 0; +} diff --git a/graphs/task1_1.cpp b/graphs/task1_1.cpp index 3963f5e..140c1bd 100644 --- a/graphs/task1_1.cpp +++ b/graphs/task1_1.cpp @@ -3,6 +3,19 @@ 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 (auto node : g[i]) + cout << node + 1 << ", "; + cout << endl; + } + +} int main() { @@ -14,26 +27,34 @@ int main() int k; cin >> k; - vector<vector<int>> graph(n); + graph g(n); + + cout << "o----------------------o" << endl; + cout << "| Нумерация вершин с 1 |" << endl; + cout << "o----------------------o" << endl; + - cout << "Введите рёбра (неориентированные), нумерация с 1:" << endl; + cout << "Введите рёбра (неориентированные):" << endl; for (int i = 0; i < k; ++i) { int a, b; cin >> a >> b; a--; b--; - graph[a].push_back(b); - graph[b].push_back(a); + g[a].push_back(b); + g[b].push_back(a); } + cout << "Введённый граф:" << endl; + print(g); + cout << "Введите исследуемую вершину: "; int q; cin >> q; q--; cout << "С данной вершиной смежны " - << graph[q].size() + << g[q].size() << " вершин" << endl; return 0; |