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
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

void addEdge(vector<vector<int>>& graph, vector<pair<char, pair<int, int>>>& operations, int src, int dest) {
    graph[src][dest] = 1;
    graph[dest][src] = 1;
    operations.push_back({'+', {src, dest}});
}

void removeEdge(vector<vector<int>>& graph, vector<pair<char, pair<int, int>>>& operations, int src, int dest) {
    graph[src][dest] = 0;
    graph[dest][src] = 0;
    operations.push_back({'-', {src, dest}});
}

void BFS(vector<vector<int>>& graph, vector<pair<char, pair<int, int>>>& operations, int src, int dest) {
    queue<int> q;
    vector<int> visited(graph.size(), 0);
    vector<int> parent(graph.size(), -1);
    vector<int> distance(graph.size(), -1);

    q.push(src);
    distance[src] = 0;

    while (!q.empty()) {
        int u = q.front();
        q.pop();
        visited[u] = 1;

        for (int v = 0; v < graph.size(); ++v) {
            if (graph[u][v] == 1 && !visited[v]) {
                q.push(v);
                visited[v] = 1;
                parent[v] = u;
                distance[v] = distance[u] + 1;
            }
        }
    }

    vector<int> path;
    int u = dest;
    while (u != -1) {
        path.push_back(u);
        u = parent[u];
    }
    // skip source and first connector
    for(int i = path.size() - 1 - 2; i >= 0; i--) {
        addEdge(graph, operations, src, path[i]);
    }

}

int main() {
    int n, m, k;
    cin >> n >> m;
    vector<vector<int>> graph(n + 1, vector<int>(n + 1, 0));
    vector<pair<char, pair<int, int>>> operations;
    for (int i = 0; i < m; i++) {
        int src, dest;
        cin >> src >> dest;
        graph[src][dest] = 1;
        graph[dest][src] = 1;
    }
    cin >> k;
    vector<vector<int>> targetGraph(n + 1, vector<int>(n + 1, 0));
    for (int i = 0; i < k; i++) {
        int src, dest;
        cin >> src >> dest;
        targetGraph[src][dest] = 1;
        targetGraph[dest][src] = 1;
    }

    vector<pair<int, int>> edgesToAdd;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (targetGraph[i][j] == 1 && graph[i][j] == 0) {
                edgesToAdd.push_back({i, j});
            }
        }
    }

    for (const auto& edge : edgesToAdd) {
        BFS(graph, operations, edge.first, edge.second);
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (targetGraph[i][j] == 0 && graph[i][j] == 1) {
                removeEdge(graph, operations, i, j);
            }
        }
    }

    cout << operations.size() << endl;
    for (const auto& operation : operations) {
        cout << operation.first << " " << operation.second.first << " " << operation.second.second << endl;
    }
    return 0;
}