summaryrefslogtreecommitdiff
path: root/graphs/task2.cpp
blob: 17dcdbe4d2dd56dd14f86705e831045f73656a1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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);
    }

    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;
}