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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Kacper Orszulak
#ifndef DEBUG
    #define NDEBUG
#endif

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvll = vector<vll>;
#define st first
#define nd second
#define pb push_back
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define whole(x) x.begin(), x.end()
[[maybe_unused]] constexpr int INF = 1e9+7;
template<class T> bool minR(T &a, const T &b) {
	if (b < a) { a = b; return true; }
	else return false;
}
template<class T> bool maxR(T &a, const T &b) {
	if (b > a) { a = b; return true; }
	else return false;
}

constexpr int ROOT = 0;
int n;
int startEdgesCnt, wantedEdgesCnt;
vector<set<int>> G, wantedG;
set<pii> wantedAddEdges, wantedRemoveEdges;
set<pii> temporaryEdges;

struct Step {
    bool add = false;
    int a;
    int b;
};

vector<Step> steps;
// vector<pii> addSteps, removeSteps;

pair<set<int>&, set<int>&> smallerAndGreaterSet(set<int> &A, set<int> &B) {
    if (A.size() <= B.size())
        return {A, B};
    else
        return {B, A};
}

template<class T>
pair<T&, T&> getMinMax(T &a, T &b) {
    if (a <= b)
        return {a, b};
    else
        return {b, a};
}

bool isWellConnected(int a, int b) {
    auto [smaller, greater] = smallerAndGreaterSet(G[a], G[b]);
    bool hasCommon = false;
    for (const int c : smaller) {
        if (greater.find(c) != greater.end()) {
            hasCommon = true;
            break;
        }
    }
    return hasCommon;
}

bool edgeExists(int a, int b) {
    return G[a].find(b) != G[a].end();
}

void addEdge(int a, int b) {
    assert(isWellConnected(a, b));

    steps.push_back({true, a, b});
    // addSteps.push_back({a, b});
    const auto [it1, inserted1] = G[a].insert(b);
    const auto [it2, inserted2] = G[b].insert(a);
    assert(inserted1);
    assert(inserted2);
}

void removeEdge(int a, int b) {
    assert(isWellConnected(a, b));

    steps.push_back({false, a, b});
    // removeSteps.push_back({a, b});
    const int cnt1 = G[a].erase(b);
    const int cnt2 = G[b].erase(a);
    assert(cnt1);
    assert(cnt2);
}

void handleAddSteps() {
    // vector<pii> toUndo;
    vector<pii> toAdd;
    vi nextV;
    vb visited(n, false);

    nextV.push_back(ROOT);
    visited[ROOT] = true;
    while (not nextV.empty()) {
        const int v = nextV.back();
        nextV.pop_back();
        for (const int w : G[v]) {
            if (visited[w])
                continue;
            const int a = ROOT;
            const int b = w;
            if (not edgeExists(a, b)) {
                toAdd.push_back({a, b});
                if (wantedAddEdges.find({a, b}) == wantedAddEdges.end())
                    // toUndo.push_back({a, b});
                    temporaryEdges.insert({a, b});
                else
                    wantedAddEdges.erase({a, b});
            }
            visited[w] = true;
            nextV.push_back(w);
        }
    }

    for (const auto &[a, b] : toAdd)
        addEdge(a, b);
    for (const auto &[a, b] : wantedAddEdges) {
        addEdge(a, b);
    }
    // while (not toUndo.empty()) {
    //     const auto [a, b] = toUndo.back();
    //     removeEdge(a, b);
    //     toUndo.pop_back();
    // }
}

void handleRemoveSteps() {
    // vector<pii> toUndo;
    vector<pii> toAdd;
    vi nextV;
    vb visited(n, false);

    nextV.push_back(ROOT);
    visited[ROOT] = true;
    while (not nextV.empty()) {
        const int v = nextV.back();
        nextV.pop_back();
        for (const int w : G[v]) {
            if (visited[w])
                continue;
            if (wantedRemoveEdges.find(getMinMax(v, w)) != wantedRemoveEdges.end())
                continue;
            if (not edgeExists(ROOT, w)) {
                toAdd.push_back({ROOT, w});
                temporaryEdges.insert({ROOT, w});
                // toUndo.push_back({ROOT, w});
            } else if (wantedRemoveEdges.find({ROOT, w}) != wantedRemoveEdges.end()) {
                wantedRemoveEdges.erase({ROOT, w});
                temporaryEdges.insert({ROOT, w});
                // toUndo.push_back({ROOT, w});
            }
            visited[w] = true;
            nextV.push_back(w);
        }
    }

    for (const auto &[a, b] : toAdd) {
        addEdge(a, b);
    }
    for (const auto &[a, b] : wantedRemoveEdges) {
        removeEdge(a, b);
    }

    // while (not toUndo.empty()) {
    //     const auto [a, b] = toUndo.back();
    //     removeEdge(a, b);
    //     toUndo.pop_back();
    // }
}

void removeTemporary() {
    vb visited(n, false);
    vector<pii> toUndo;
    vi nextV;

    nextV.push_back(ROOT);
    visited[ROOT] = true;
    while (not nextV.empty()) {
        const int v = nextV.back();
        nextV.pop_back();
        for (const int w : G[v]) {
            if (visited[w])
                continue;
            if (temporaryEdges.find(getMinMax(v, w)) != temporaryEdges.end())
                continue;

            visited[w] = true;
            nextV.push_back(w);

            if (temporaryEdges.find(getMinMax(ROOT, w)) != temporaryEdges.end()) {
                // removeEdge(ROOT, w);
                toUndo.push_back({ROOT, w});
            }
        }
    }

    while (not toUndo.empty()) {
        const auto [a, b] = toUndo.back();
        removeEdge(a, b);
        toUndo.pop_back();
    }

    // function<void(const int)> dfs = [&](const int v) {
    //     if (visited[v])
    //         return;
    //     visited[v] = true;
    //     for (const int w : G[v]) {
    //         dfs(w);
    //
    //         if (temporaryEdges.find(getMinMax(v, w)) != temporaryEdges.end()) {
    //             removeEdge(v, w);
    //         }
    //     }
    // };
    // dfs(0);
}

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

    cin >> n;
    G.resize(n);
    wantedG.resize(n);

    cin >> startEdgesCnt;
    rep(i, startEdgesCnt) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        G[a].insert(b);
        G[b].insert(a);
    }

    cin >> wantedEdgesCnt;
    rep(i, wantedEdgesCnt) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        wantedG[a].insert(b);
        wantedG[b].insert(a);

        if (b < a)
            swap(a, b);
        if (G[a].find(b) == G[a].end()) {
            wantedAddEdges.insert({a, b});
        }
    }

    rep(a, n) {
        for (const int b : G[a]) {
            if (b < a)
                continue;
            if (wantedG[a].find(b) == wantedG[a].end()) {
                wantedRemoveEdges.insert({a, b});
            }
        }
    }


    handleAddSteps();
    handleRemoveSteps();
    removeTemporary();

    const int allStepsCnt = steps.size();
    cout << allStepsCnt << '\n';
    for (const Step &step : steps) {
        if (step.add)
            cout << "+ ";
        else
            cout << "- ";
        cout << step.a+1 << " " << step.b+1 << '\n';
    }
    // const int allStepsCnt = addSteps.size() + removeSteps.size();
    // for (const auto &[a, b] : addSteps) {
    //     cout << "+ " << a+1 << " " << b+1 << '\n';
    // }
    // for (const auto &[a, b] : removeSteps) {
    //     cout << "- " << a+1 << " " << b+1 << '\n';
    // }
#ifdef DEBUG
    cout << flush;
    if (allStepsCnt > 200000) {
        cerr << allStepsCnt << '\n';
    }
    assert(allStepsCnt <= 200000);
    assert(G == wantedG);
#endif
}