Advertisement
Georgiy031

Untitled

Apr 14th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <locale>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     setlocale(LC_ALL, "Russian");
  10.     int n, type1, type2;
  11.     cout << "Введите количество вершин: ";
  12.     cin >> n;
  13.     cout << "Введите тип хранения: \n"
  14.         << "1) список смежности\n"
  15.         << "2) список предков\n";
  16.     cin >> type1;
  17.     cout << "Введите способ хранения: \n"
  18.         << "1) на связанной памяти\n"
  19.         << "2) на смежной памяти\n";
  20.     cin >> type2;
  21.     if (type1 == 1) {
  22.         if (type2 == 1) {
  23.             vector<vector<int>> tree(n);
  24.             for (int i = 0; i < n - 1; ++i) {
  25.                 int a, b;
  26.                 cin >> a >> b;
  27.                 --a, --b;
  28.                 tree[a].push_back(b);
  29.                 tree[b].push_back(a);
  30.             }
  31.             for (int i = 0; i < n; ++i) {
  32.                 cout << i + 1 << ": ";
  33.                 for (auto& to : tree[i]) {
  34.                     cout << to + 1;
  35.                     cout << (to != tree[i].back() ? ", " : ".\n");
  36.                 }
  37.             }
  38.         }
  39.         else {
  40.             list<list<int>> tree(n);
  41.             for (int i = 0; i < n - 1; ++i) {
  42.                 int a, b;
  43.                 cin >> a >> b;
  44.                 --a, --b;
  45.                 next(tree.begin(), a)->push_back(b);
  46.                 next(tree.begin(), b)->push_back(a);
  47.             }
  48.             for (int i = 0; i < n; ++i) {
  49.                 cout << i + 1 << ": ";
  50.                 for (auto& to : *next(tree.begin(), i)) {
  51.                     cout << to + 1;
  52.                     cout << (to != next(tree.begin(), i)->back() ? ", " : ".\n");
  53.                 }
  54.             }
  55.         }
  56.     }
  57.     else if (type1 == 2) {
  58.         if (type2 == 1) {
  59.             vector<int> tree(n);
  60.             for (int i = 1; i < n; ++i) {
  61.                 cin >> tree[i];
  62.             }
  63.             for (auto& to : tree) {
  64.                 cout << to << " ";
  65.             }
  66.         }
  67.         else {
  68.             list<int> tree(1);
  69.             for (int i = 1; i < n; ++i) {
  70.                 int x;
  71.                 cin >> x;
  72.                 tree.push_back(x);
  73.             }
  74.             for (auto& to : tree) {
  75.                 cout << to << " ";
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement