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
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <queue>
#include <utility>

using namespace std;

struct pair_hash {
    inline std::size_t operator()(const std::pair<int, int> &v) const {
        return v.first * 31 + v.second;
    }
};

class Move {
public:
    char type;
    pair<int, int> edge;
};

unordered_set<pair<int, int>, pair_hash> edges;
unordered_set<pair<int, int>, pair_hash> finalEdges;
vector<unordered_set<int>> adjacency;
vector<unordered_set<int>> finalAdjacency;
vector<Move> moves;

void addEdge(int a, int b, bool isMove) {
    pair<int, int> edge = {min(a, b), max(a, b)};
    edges.insert(edge);
    adjacency[a].insert(b);
    adjacency[b].insert(a);
    if (isMove) {
        Move move;
        move.type = '+';
        move.edge = edge;
        moves.push_back(move);
    }
}

void removeEdge(int a, int b) {
    pair<int, int> edge = {min(a, b), max(a, b)};
    edges.erase(edge);
    adjacency[a].erase(b);
    adjacency[b].erase(a);
    Move move;
    move.type = '-';
    move.edge = edge;
    moves.push_back(move);
}

void addEdgesToRoot() {
    queue<int> q;
    unordered_set<int> visited;
    vector<pair<int, int>> toAdd;

    q.push(1);
    visited.insert(1);

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

        for (int v : adjacency[u]) {
            if (visited.find(v) == visited.end()) {
                q.push(v);
                visited.insert(v);
                if (u != 1) {
                    pair<int, int> edge = {1,v};
                    toAdd.push_back(edge);
                }
            }
        }
    }

    for (int i = 0; i < toAdd.size(); i++) {
        addEdge(toAdd[i].first, toAdd[i].second, true);
    }
}

void removeEdgesToRoot() {
    queue<int> q;
    unordered_set<int> visited;
    vector<pair<int, int>> toRemove;

    q.push(1);
    visited.insert(1);

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

        for (int v : finalAdjacency[u]) {
            if (visited.find(v) == visited.end()) {
                q.push(v);
                visited.insert(v);
                pair<int, int> edge = {1, v};
                if (finalEdges.find(edge) == finalEdges.end()) {
                    toRemove.push_back(edge);
                }
            }
        }
    }

    for (int i = toRemove.size() - 1; i >= 0; i--) {
        removeEdge(toRemove[i].first, toRemove[i].second);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    adjacency.resize(n + 1);
    finalAdjacency.resize(n + 1);

    int startSize, a, b;
    cin >> startSize;
    for (int i = 0; i < startSize; i++) {
        cin >> a >> b;
        addEdge(a, b, false);
    }

    int endSize;
    cin >> endSize;
    for (int i = 0; i < endSize; i++) {
        cin >> a >> b;
        finalEdges.insert({min(a, b), max(a, b)});
        finalAdjacency[a].insert(b);
        finalAdjacency[b].insert(a);
    }

    addEdgesToRoot();

    for (auto &edge : finalEdges) {
        if (edges.find(edge) == edges.end()) {
            addEdge(edge.first, edge.second, true);
        }
    }
    vector<pair<int, int>> toRemove;
    for (auto &edge : edges) {
        if (finalEdges.find(edge) == finalEdges.end() && edge.first > 1) {
            toRemove.push_back(edge);
        }
    }
    for (int i = 0; i < toRemove.size(); i++) {
        removeEdge(toRemove[i].first, toRemove[i].second);
    }

    removeEdgesToRoot();

    cout << moves.size() << endl;
    for (auto &move : moves) {
        cout << move.type << " " << move.edge.first << " " << move.edge.second << endl;
    }
}