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
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int n, m;

    std::cin >> n;

    std::vector<std::set<int>> startGraph(n + 1, std::set<int>());
    std::vector<std::set<int>> endGraph(n + 1, std::set<int>());

    std::cin >> m;

    for (int i = 0; i < m; i++)
    {
        int a, b;
        std::cin >> a >> b;
        startGraph[a].insert(b);
        startGraph[b].insert(a);
    }

    std::cin >> m;

    for (int i = 0; i < m; i++)
    {
        int a, b;
        std::cin >> a >> b;
        endGraph[a].insert(b);
        endGraph[b].insert(a);
    }

    std::vector<std::pair<int, int>> adds;
    std::vector<std::pair<int, int>> removes;

    std::vector<bool> visited(n + 1, false);
    std::queue<int> q;

    visited[1] = true;
    for (int i : startGraph[1])
    {
        visited[i] = true;
        q.push(i);
    }

    while (!q.empty())
    {
        int current = q.front();
        q.pop();

        for (int i : startGraph[current])
        {
            if (!visited[i])
            {
                visited[i] = true;
                q.push(i);
                startGraph[1].insert(i);
                startGraph[i].insert(1);
                adds.push_back({1, i});
            }
        }
    }

    for (int i = 2; i <= n; i++)
    {
        for (int j : endGraph[i])
        {
            if (!startGraph[i].count(j))
            {
                startGraph[i].insert(j);
                startGraph[j].insert(i);
                adds.push_back({i, j});
            }
        }
    }

    for (int i = 2; i <= n; i++)
    {
        for (int j : startGraph[i])
        {
            if (j != 1 && !endGraph[i].count(j))
            {
                startGraph[j].extract(i);
                removes.push_back({i, j});
            }
        }
    }

    visited = std::vector<bool>(n + 1, false);

    std::stack<int> removeOrder;
    visited[1] = true;

    for (int i : endGraph[1])
    {
        visited[i] = true;
        q.push(i);
    }

    while (!q.empty())
    {
        int current = q.front();
        q.pop();

        for (int i : endGraph[current])
        {
            if (!visited[i])
            {
                visited[i] = true;
                q.push(i);
                removeOrder.push(i);
            }
        }
    }

    while (!removeOrder.empty())
    {
        int current = removeOrder.top();
        removeOrder.pop();

        removes.push_back({current, 1});
    }

    int size = adds.size() + removes.size();

    std ::cout << size << std::endl;

    for (auto pair : adds)
    {
        std::cout << "+ " << pair.first << " " << pair.second << std::endl;
    }

    for (auto pair : removes)
    {
        std::cout << "- " << pair.first << " " << pair.second << std::endl;
    }
}