Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <list>
- #include <locale>
- using namespace std;
- int main() {
- setlocale(LC_ALL, "Russian");
- int n, type1, type2;
- cout << "Введите количество вершин: ";
- cin >> n;
- cout << "Введите тип хранения: \n"
- << "1) список смежности\n"
- << "2) список предков\n";
- cin >> type1;
- cout << "Введите способ хранения: \n"
- << "1) на связанной памяти\n"
- << "2) на смежной памяти\n";
- cin >> type2;
- if (type1 == 1) {
- if (type2 == 1) {
- vector<vector<int>> tree(n);
- for (int i = 0; i < n - 1; ++i) {
- int a, b;
- cin >> a >> b;
- --a, --b;
- tree[a].push_back(b);
- tree[b].push_back(a);
- }
- for (int i = 0; i < n; ++i) {
- cout << i + 1 << ": ";
- for (auto& to : tree[i]) {
- cout << to + 1;
- cout << (to != tree[i].back() ? ", " : ".\n");
- }
- }
- }
- else {
- list<list<int>> tree(n);
- for (int i = 0; i < n - 1; ++i) {
- int a, b;
- cin >> a >> b;
- --a, --b;
- next(tree.begin(), a)->push_back(b);
- next(tree.begin(), b)->push_back(a);
- }
- for (int i = 0; i < n; ++i) {
- cout << i + 1 << ": ";
- for (auto& to : *next(tree.begin(), i)) {
- cout << to + 1;
- cout << (to != next(tree.begin(), i)->back() ? ", " : ".\n");
- }
- }
- }
- }
- else if (type1 == 2) {
- if (type2 == 1) {
- vector<int> tree(n);
- for (int i = 1; i < n; ++i) {
- cin >> tree[i];
- }
- for (auto& to : tree) {
- cout << to << " ";
- }
- }
- else {
- list<int> tree(1);
- for (int i = 1; i < n; ++i) {
- int x;
- cin >> x;
- tree.push_back(x);
- }
- for (auto& to : tree) {
- cout << to << " ";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement