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

#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define REP(i, n) for(int i = 0; i < (n); ++i)

template<class T> int ssize(T && a) {
	return (int)(a.size());
}
ostream& operator << (ostream &os, string str) {
    for(char c : str) os << c;
    return os;
}
template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) {
	return os << '(' << p.first << "," << p.second << ')';
}
template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) {
	os << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		os << *it << (it == prev(x.end()) ? "" : " ");
	return os << '}';
}
template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) {
    for(auto x : vec)
    	os << "\n  " << x;
    return os;
}
void dump() {}
template <class T, class... Args> void dump(T &&x, Args... args) {
    cerr << x << "; ";
    dump(args...);
}
#ifdef DEBUG
# define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n'
#else 
# define debug(...) 0
#endif

//--------------------------------------------------


set<pair<int, int>> start_edges, target_edges;
set<pair<int, int>> created_edges;

#ifdef LOCAL
	set<pair<int, int>> current_edges;
	constexpr int MAX_N = int(3e4);
	constexpr int MAX_CALLS = int(2e5);
	int calls = 0;

	bool can_be_added(int a, int b) {
		debug(a, b, current_edges);
		if (a > b) swap(a, b);
		if (current_edges.contains(pair(a, b)))
			return false;

		REP (c, MAX_N) {
			if (c == a or c == b) continue;

			pair<int, int> e1 = pair(min(a, c), max(a, c));
			pair<int, int> e2 = pair(min(b, c), max(b, c));
			if (current_edges.contains(e1)
					and current_edges.contains(e2)) {
				return true;
			}
		}

		return false;
	}

	bool can_be_removed(int a, int b) {
		if (a > b) swap(a, b);
		if (!current_edges.contains(pair(a, b)))
			return false;

		REP (c, MAX_N) {
			if (c == a or c == b) continue;

			pair<int, int> e1 = pair(min(a, c), max(a, c));
			pair<int, int> e2 = pair(min(b, c), max(b, c));

			if (current_edges.contains(e1) 
					and current_edges.contains(e2)) {
				return true;
			}
		}

		return false;
	}

#endif

vector<array<int, 3>> answer;
bool add_edge(int a, int b) {
#ifdef LOCAL
	++calls;
#endif

	if (a > b) swap(a, b);
	if (created_edges.contains(pair(a, b))) {
		return false;
	}

	created_edges.emplace(a, b);
	if (start_edges.contains(pair(a, b))) {
		return false;
	}

#ifdef LOCAL
	debug("+ ", a + 1, b + 1);
	assert(can_be_added(a, b));
	current_edges.emplace(a, b);
#endif

	answer.push_back({1, a + 1, b + 1});
	return true;
}

bool remove_edge(int a, int b) {
#ifdef LOCAL
	++calls;
#endif

	if (a > b) swap(a, b);
	if (target_edges.contains(pair(a, b)))
		return false;

#ifdef LOCAL
	debug("- ", a + 1, b + 1);
	assert(can_be_removed(a, b));
	current_edges.erase(pair(a, b));
#endif

	answer.push_back({0, a + 1, b + 1});
	return true;
}

vector<int> pre_order, post_order;
vector<bool> visited;
void dfs(vector<vector<int>> &graph, int u) {
	pre_order.emplace_back(u);
	visited[u] = true;
	for (int v : graph[u]) 
		if (!visited[v]) dfs(graph, v);
	post_order.emplace_back(u);
}

vector<int> get_pre_order(vector<vector<int>> &graph, int source = 0) {
	int n = ssize(graph);
	pre_order = post_order = {};
	visited = vector<bool>(n, false);

	dfs(graph, source);
	return pre_order;
}

vector<int> get_post_order(vector<vector<int>> &graph, int source = 0) {
	int n = ssize(graph);
	pre_order = post_order = {};
	visited = vector<bool>(n, false);

	dfs(graph, source);
	return post_order;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n; cin >> n;

	int m_s; cin >> m_s;
	vector<vector<int>> start_graph(n);
	REP (i, m_s) {
		int a, b; 
		cin >> a >> b;
		--a, --b;

		if (a > b) swap(a, b);
		start_edges.emplace(a, b);

		start_graph[a].emplace_back(b);
		start_graph[b].emplace_back(a);
	}

#ifdef LOCAL
	current_edges = start_edges;
#endif

	int m_d; cin >> m_d;
	vector<vector<int>> target_graph(n);
	REP (i, m_d) {
		int a, b;
		cin >> a >> b;
		--a, --b;
		
		if (a > b) swap(a, b);
		target_edges.emplace(a, b);

		target_graph[a].emplace_back(b);
		target_graph[b].emplace_back(a);
	}


	vector<int> pre = get_pre_order(start_graph);
	vector<int> post = get_post_order(target_graph);
	debug(pre);
	debug(post);

	constexpr int source = 0;
	for (int u : pre) if (u != source) {
		add_edge(u, source);
	}

	for (auto [a, b] : start_edges) {
		if (a != source and b != source and !created_edges.contains(pair(a, b))) {
			remove_edge(a, b);
		}
	}

	for (auto [a, b] : target_edges) {
		add_edge(a, b);
	}

	for (int u : post) if (u != source) {
		remove_edge(u, source);
	}

#ifdef LOCAL
	assert(current_edges == target_edges);
	assert(calls < MAX_CALLS);
	cout << "OK\n";
#else
	cout << ssize(answer) << '\n';
	for (auto [add, a, b] : answer) {
		cout << (add ? '+' : '-') << ' ' << a << ' ' << b << '\n';
	}
#endif
}