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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Mateusz Lambert
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <climits>
#include <algorithm>
#include <string>
#define ll long long
#define int128 __uint128_t
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define vi vector <int>
#define si set <int>
#define sl set <ll>
#define mins(x) *x.begin()
#define maxs(x) *x.rbegin()
#define vii vector <pair<int, int>>
#define vl vector <ll>
#define vll vector<pair<ll, ll>>
#define vb vector <bool>
#define vs vector <string>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sec second
#define fir first
using namespace std;

// td fix rozroznialnosc par

const int MAXN = 3e4+1;
vi graf[MAXN];
vi grafv[MAXN];
set<pii> added, fini, ad;
int odl[MAXN];
int odw[MAXN];

// bfs - dostaje 2 wierzcholki chce zwrocic najkrotsza sciezke miedzy nimi
vi bfs(int a, int b){
	unordered_map <int, int> parent;
	vi path;
	queue <int> q;
	
	q.push(a);
	parent[a] = -1;
	parent[-1] = -2;
	while (!q.empty()){
		int curr = q.front(); q.pop();
		
		if (curr==b){ // koniec
			int v = b;
			while (parent[v]!=parent[-1]){ // != a
				path.pb(v);
				v = parent[v];
			}
			reverse(all(path));
			return path;
		}
		
		// lecimy dalej
		for (int x : graf[curr]){
			if (parent[x]==parent[b]){ //unvis
				q.push(x);
				parent[x] = curr;
			}
		}
	}
	return path; // empty
}

void removeEdge(int u, int v) {
	added.erase(added.find({min(u, v), max(u, v)}));
    auto it = find(grafv[u].begin(), grafv[u].end(), v);
    if (it != grafv[u].end()) {
        grafv[u].erase(it);
    }

    it = find(grafv[v].begin(), grafv[v].end(), u);
    if (it != grafv[v].end()) {
        grafv[v].erase(it);
    }
}

vi bfs2(int a, int b){
	removeEdge(a, b);
	unordered_map <int, int> parent;
	vi path;
	queue <int> q;
	
	q.push(a);
	parent[a] = -1;
	parent[-1] = -2;
	while (!q.empty()){
		int curr = q.front(); q.pop();
		
		if (curr==b){ // koniec
			int v = b;
			while (parent[v]!=parent[-1]){ // != a
				path.pb(v);
				v = parent[v];
			}
			reverse(all(path));
			return path;
		}
		
		// lecimy dalej
		for (int x : grafv[curr]){
			if (parent[x]==parent[b]){ //unvis
				q.push(x);
				parent[x] = curr;
			}
		}
	}
	return path; // empty
}

void gen_res(vs& ans, vi& path, int start, int end){
	vii res;
	int l=1, n=path.size();
	while ((1<<l) < n){ // ??
		for (int i=0; i+(1<<l)<n; i+=(1<<l)){
			res.pb({min(path[i], path[i+(1<<l)]), max(path[i], path[i+(1<<l)])});
			if (i+(1<<l)==n-2) res.pb({min(path[i], path[n-1]), max(path[i], path[n-1])});
		}
		l++;
	}
	if (res[res.size()-1]!=make_pair(min(start, end), max(start, end))) res.pb({min(start, end), max(start, end)});
	
	for (int i=0; i<res.size(); i++){
		if (added.find(make_pair(min(res[i].fir, res[i].sec), max(res[i].fir, res[i].sec)))==added.end()){
			string pub = "+ ";
			pub += to_string(min(res[i].fir, res[i].sec)); pub += ' '; pub += to_string(max(res[i].fir, res[i].sec));
			ans.pb(pub);
			added.insert({min(res[i].fir, res[i].sec), max(res[i].fir, res[i].sec)});
			if (fini.find(make_pair(min(res[i].fir, res[i].sec), max(res[i].fir, res[i].sec)))!=fini.end()){
				grafv[res[i].fir].pb(res[i].sec);
				grafv[res[i].sec].pb(res[i].fir);
			}
		} 
	}
	for (int i=res.size()-2; i>=0; i--){
		if (added.find(make_pair(min(res[i].fir, res[i].sec), max(res[i].fir, res[i].sec)))!=added.end() && fini.find(make_pair(min(res[i].fir, res[i].sec), max(res[i].fir, res[i].sec)))==fini.end()){
			string pub = "- ";
			pub += to_string(min(res[i].fir, res[i].sec)); pub += ' '; pub += to_string(max(res[i].fir, res[i].sec));
			ans.pb(pub);
			added.erase(added.find(make_pair(min(res[i].fir, res[i].sec), max(res[i].fir, res[i].sec))));
		}
	}
		grafv[start].pb(end);
		grafv[end].pb(start);
	
	//for (auto x : res) cout << x.fir << " " << x.sec << "\n";
	//cout << "###############################################\n";
}

void gen2(vs& ans, vi& path, int start, int end){
	vii res;
	int l=1, n=path.size();
	while ((1<<l) < n){ // ??
		for (int i=0; i+(1<<l)<n; i+=(1<<l)){
			res.pb({min(path[i], path[i+(1<<l)]), max(path[i], path[i+(1<<l)])});
			if (i+(1<<l)==n-2) res.pb({min(path[i], path[n-1]), max(path[i], path[n-1])});
		}
		l++;
	}
	if (res[res.size()-1]==make_pair(min(start, end), max(start, end))) res.pop_back();

	for (int i=0; i<res.size(); i++){
		//if (added.find(make_pair(min(res[i].fir, res[i].sec), max(res[i].fir, res[i].sec)))==added.end()){
			string pub = "+ ";
			pub += to_string(min(res[i].fir, res[i].sec)); pub += ' '; pub += to_string(max(res[i].fir, res[i].sec));
			ans.pb(pub);
		//} 
	}
	string pub = "- "; pub += to_string(min(start, end)); pub += ' '; pub += to_string(max(start, end));
	ans.pb(pub);
	
	for (int i=res.size()-1; i>=0; i--){
		//if (added.find(make_pair(min(res[i].fir, res[i].sec), max(res[i].fir, res[i].sec)))!=added.end() && fini.find(make_pair(min(res[i].fir, res[i].sec), max(res[i].fir, res[i].sec)))==fini.end()){
			pub = "- ";
			pub += to_string(min(res[i].fir, res[i].sec)); pub += ' '; pub += to_string(max(res[i].fir, res[i].sec));
			ans.pb(pub);
		//}
	}
}

void del_ed(vs& ans, int a, int b){
	// usuwamy krawedz z grafv i szukamy sciezki
	// solved
	vi path = bfs2(min(a,b), max(a, b));
	gen2(ans, path, a, b);
}
void removeRedundantEdges(vector<string>& edges) {
    unordered_set<string> addedEdges;
    vector<string> result;

    for (int i=edges.size()-1; i>=0; i--) {
        if (edges[i][0] == '+') {
            // If it's an addition, add it to the set of added edges
            addedEdges.insert(edges[i].substr(2));
            result.push_back(edges[i]);
        } else if (edges[i][0] == '-') {
            // If it's a removal, check if the corresponding addition exists
            string removedEdge = edges[i].substr(2);
            if (addedEdges.find(removedEdge) == addedEdges.end()) {
                // If the corresponding addition doesn't exist, keep the removal
                result.push_back(edges[i]);
            }
        }
    }

    // Update the original vector with the result
    edges = result;
}
void parse1(vs& ans){
	vs res;
	for (auto x : ans){		
		int a, b;
		string tmp = "";
		int k=2;
		while (x[k]!=' ') {tmp += x[k]; k++;}
		a = stoi(tmp); k++;
		tmp = "";
		while (k<x.size() && x[k]!=' ') {tmp += x[k]; k++;}
		b = stoi(tmp);
		//cout << a << ' ' << b << '\n';
		if (x[0]=='+'){
			if (ad.find({min(a, b), max(a, b)})==ad.end()){
				res.pb(x);
				ad.insert({min(a, b), max(a, b)});
				
			}
		}else{
			res.pb(x);
			ad.erase(ad.find({min(a, b), max(a, b)}));
		}
	}
	ans = res;	
}

int main(){
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	
	int n, ms; cin >> n >> ms;
	vii plus, minus;
	vs ans;
	
	for (int i=0; i<ms; i++){
		int a, b; cin >> a >> b;
		if (a>b) swap(a, b); // musza byc uporzadkowane zeby znalezc +- w tych setach
		grafv[a].pb(b);
		grafv[b].pb(a);
		graf[a].pb(b);
		graf[b].pb(a);
		added.insert({a, b});
		ad.insert({a, b});
	}
	int md; cin >> md;
	for (int i=0; i<md; i++){
		int a, b; cin >> a >> b;
		if (a>b) swap(a, b);
		if (added.find({a, b})==added.end()) plus.pb({a, b}); // nie ma w startowym ale jest w docelowym
		fini.insert({a, b});
	}
	// minusy
	for (auto [a, b] : added) if (fini.find({a, b})==fini.end()) minus.pb({a, b}); // jest w startowym nie ma w docelowym
	// stestowane do tego momentu


	// dodawanie do grafu
	for (auto [a, b] : plus){
		//cout << "A : " <<a <<" B: " << b << '\n';
		vi path = bfs(a, b);
		//for (int x : path) cout << x << ' ';
		//cout << '\n';
		gen_res(ans, path, a, b);
	}
	//for (auto x : ans) cout << x << '\n';
	
	// wykurwianie z grafu
	for (auto [a, b] : minus){
		//cout << "A: " << a << " B: " << b << '\n';
		//vi h = bfs2(a, b);
		//for (int x : h) cout << x << ' ';
		//cout << '\n';
		del_ed(ans, a, b);
	}
	//for (auto x : ans) cout << x << '\n';
	removeRedundantEdges(ans);
	reverse(all(ans));
	parse1(ans);
	cout << (int)ans.size() << '\n';
	for (auto x : ans) cout << x << '\n';
}

/*12 11
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
1 12
/*/