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
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define ST first
#define ND second
#define MP make_pair
#define PB push_back

typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef set<int> SI;
typedef long long LL;

#ifdef DEBUG
const bool debug = true;
#else
const bool debug = false;
#endif

// #define MULTI_TASK true

const int INF = 1000 * 1000 * 1000;
const LL INFLL = 1e18;
const int MAKSN = 30 * 1000 + 13;
const LL MOD = 123456789LL;
LL N, Ms, Mt, k, l, a, b, c, d;
vector<LL> v;

vector<int> startG[MAKSN], targG[MAKSN];
set<int> neigh[MAKSN], targNeigh[MAKSN];

vector<tuple<char, int, int>> moves;

void zeruj() {
    v.clear();
}

void readIn() {
    cin >> N >> Ms;
    REP(i, Ms) {
        cin >> a >> b;
        a--;
        b--;
        startG[a].PB(b);
        startG[b].PB(a);
        neigh[a].insert(b);
        neigh[b].insert(a);
    }

    cin >> Mt;
    REP(i, Mt) {
        cin >> a >> b;
        a--;
        b--;
        targG[a].PB(b);
        targG[b].PB(a);
        targNeigh[a].insert(b);
        targNeigh[b].insert(a);
    }
}

bool existsEdge(int a, int b, set<int> s[]) {
    return s[a].find(b) != s[a].end();
}

void addEdge(int a, int b) {
    if (existsEdge(a, b, neigh))
        return;
    neigh[a].insert(b);
    neigh[b].insert(a);
    moves.emplace_back('+', a, b);
}

void removeEdge(int a, int b) {
    if (!existsEdge(a, b, neigh))
        return;
    neigh[a].erase(b);
    neigh[b].erase(a);
    moves.emplace_back('-', a, b);
}

void bfs(int start) {
    queue<int> q;
    vector<bool> visited(N + 1);
    VI distance(N + 1);

    distance[start] = 0;
    visited[start] = true;
    q.push(start);

    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (int i = 0; i < startG[v].size(); i++) {
            int cur = startG[v][i];
            if (!visited[cur]) {
                visited[cur] = true;
                distance[cur] = distance[v] + 1;
                q.push(cur);
                addEdge(start, cur);
            }
        }
    }
}

void createNecessaryEdges() {
    for (int i = 0; i < N; i++) {
        for (auto v: targG[i]) {
            if (!existsEdge(i, v, neigh)) {
                addEdge(i, v);
            }
        }
    }
}

void removeUnnecessaryEdges() {
    set<PII> toDelete;

    for (int i = 1; i < N; i++) {
        for (auto v: neigh[i]) {
            if (v != 0 && !existsEdge(i, v, targNeigh)) {
                toDelete.insert({min(i, v), max(i, v)});
            }
        }
    }

    for (auto p: toDelete) {
        removeEdge(p.ST, p.ND);
    }
}

void printAnswer() {
    cout << moves.size() << "\n";

    for (auto move : moves) {
        cout << get<0>(move) << " " << get<1>(move) + 1 << " " << get<2>(move) + 1 << "\n";
    }
}


void rollback(int start) {
    queue<int> q;
    vector<bool> visited(N + 1);
    VI distance(N + 1);

    distance[start] = 0;
    visited[start] = true;
    q.push(start);

    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (int i = 0; i < targG[v].size(); i++) {
            int cur = targG[v][i];
            if (!visited[cur]) {
                visited[cur] = true;
                distance[cur] = distance[v] + 1;
                q.push(cur);
            }
        }
    }

    VPII v;
    for (int i = 1; i < N; i++) {
        v.PB({distance[i], i});
    }

    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());

    for (auto p: v) {
        int nr = p.ND;
        if (!existsEdge(start, nr, targNeigh)) {
            removeEdge(start, nr);
        }
    }
}

void solve() {
    // 1. Connect all to 1
    bfs(0);

    // 2. Create all edges from targG that should exist but they do not
    createNecessaryEdges();

    // 3. Remove all edges that exist but are not in targG
    removeUnnecessaryEdges();

    // 4. Unroll edges to 1.
    rollback(0);

    printAnswer();
}

int main() {
    ios_base::sync_with_stdio(0);
#ifdef MULTI_TASK
    int test;
    cin >> test;
    while (test--) {
#endif
        zeruj();
        readIn();
        solve();
#ifdef MULTI_TASK
    }
#endif
    return 0;
}