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
// PA 2024 2B - Alchemik Bajtazar
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;

struct Result {
	char type;
	int a;
	int b;
	
	Result(char type, int a, int b) {
		this->type = type;
		this->a = a;
		this->b = b;
	}
};

void dfs(int node, vector<vector<int>> *edges, vector<bool> *marked, vector<int> *order)
{
	(*marked)[node] = true;
	order->push_back(node);
	
	for (unsigned i = 0; i < (*edges)[node].size(); ++i) {
		if (not (*marked)[(*edges)[node][i]]) {
			dfs((*edges)[node][i], edges, marked, order);
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	int n, m, a, b;
	vector<vector<int>> edgesA;
	vector<vector<int>> edgesB;
	vector<bool> marked;
	vector<int> orderA;
	vector<int> orderB;
	vector<Result> operations;
	
	cin >> n;
	for (int i = 0; i < n; ++i) {
		edgesA.push_back(vector<int>());
		edgesB.push_back(vector<int>());
		marked.push_back(false);
	}
	
	cin >> m;
	for (int i = 0; i < m; ++i) {
		cin >> a;
		cin >> b;
		--a;
		--b;
		edgesA[a].push_back(b);
		edgesA[b].push_back(a);
	}
	
	cin >> m;
	for (int i = 0; i < m; ++i) {
		cin >> a;
		cin >> b;
		--a;
		--b;
		edgesB[a].push_back(b);
		edgesB[b].push_back(a);
	}
	
	for (int i = 0; i < n; ++i) {
		sort(edgesA[i].begin(), edgesA[i].end());
		sort(edgesB[i].begin(), edgesB[i].end());
	}
	
	// etap 1 - dodanie polaczenia z 0 wszystkim tym, ktorzy jeszcze nie maja (w kolejnosci DFS-a)
	dfs(0, &edgesA, &marked, &orderA);
	for (int i = 1; i < n; ++i) {
		if (edgesA[orderA[i]][0] != 0) {
			operations.push_back(Result('+', 1, orderA[i] + 1));
		}
	}
	
	// etap 2 - usuniecie wszystkich polaczen niezwiazanych z 0
	for (int i = 1; i < n; ++i) {
		for (unsigned j = 0; j < edgesA[i].size(); ++j) {
			if (i < edgesA[i][j]) {
				operations.push_back(Result('-', i + 1, edgesA[i][j] + 1));
			}
		}
	}
	
	// etap 3 - dodanie tych polaczen niezwiazanych z 0, ktore maja powstac
	for (int i = 1; i < n; ++i) {
		for (unsigned j = 0; j < edgesB[i].size(); ++j) {
			if (i < edgesB[i][j]) {
				operations.push_back(Result('+', i + 1, edgesB[i][j] + 1));
			}
		}
	}
	
	// etap 4 - usuniecie polaczenia z 0 wszystkim tym, ktorzy nie maja go miec (w kolejnosci odwrotnego DFS-a)
	for (int i = 0; i < n; ++i) {
		marked[i] = false;
	}
	dfs(0, &edgesB, &marked, &orderB);
	reverse(orderB.begin(), orderB.end());
	for (int i = 0; i < n - 1; ++i) {
		if (edgesB[orderB[i]][0] != 0) {
			operations.push_back(Result('-', 1, orderB[i] + 1));
		}
	}
	
	// wyniki
	cout << operations.size() << endl;
	for (unsigned i = 0; i < operations.size(); ++i) {
		cout << operations[i].type << " " << operations[i].a << " " << operations[i].b << endl;
	}		
	
	return 0;
}