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
#include<iostream>
#include<list>
#include<iterator>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
using namespace std;
void displayAdjList(map<int, list<int>> &adj_list) {
    for (const auto& pair : adj_list) {
        cout << pair.first << "--->";
        for (int val : pair.second) {
            cout << val << " ";
        }
        cout << endl;
    }
}
void add_edge(map<int, list<int>> &adj_list, int u, int v) {
    adj_list[u].push_back(v);
    adj_list[v].push_back(u);
}

bool can_you_connect(int dest, int source, map<int, list<int>> &graph) {
    int destAndSourceCounter = 0;
    for (auto it = graph[source].begin(); it != graph[source].end(); ++it) {
        for (auto ij = graph[*it].begin(); ij != graph[*it].end(); ++ij) {
            if(*ij == dest) {
                destAndSourceCounter++;
            } else if(*ij == source) {
                destAndSourceCounter++;
            }
            if(destAndSourceCounter == 2) {
                return true;
            }
        }
        destAndSourceCounter = 0;
    }

    return  false;
}

void recursive_search(int dest, map<int, list<int>> &graph, vector<pair<int, int>> &changes, stack<int> &visited) {
    if(visited.empty()) {
        return;
    }
    int source = visited.top();

    if(can_you_connect(dest, source, graph)) {
        pair<int, int> p = {source, dest};
        changes.push_back(p);
        add_edge(graph, source, dest);
        visited.pop();
        if(visited.empty()) {
            return;
        }
        recursive_search(dest, graph, changes, visited);
    } else {
        for (auto it = graph[source].begin(); it != graph[source].end(); ++it) {
            visited.push(*it);
            recursive_search(dest, graph, changes, visited);
            if (!visited.empty()) {
                visited.pop();
            } else {
                return;
            }
        }
    }
}

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

    int n, connections, desiredConnections;
    cin >> n;
    cin >> connections;

    map<int, list<int>> graph;
    map<int, list<int>> desired_graph;
    vector<pair<int, int>> performConns;

    int first, sec;
    for(int i = 0; i < connections; i++) {
        cin >> first >> sec;
        add_edge(graph, first, sec);
    }

    cin >> desiredConnections;
    int j = 0;
    pair<int, int> performConnPair;
    for(int i = 0; i < desiredConnections; i++) {
        cin >> first >> sec;
        add_edge(desired_graph, first, sec);
        auto iter = find(graph[first].begin(), graph[first].end(), sec);
        if (iter == graph[first].end()) {
            performConnPair.first = first;
            performConnPair.second = sec;
            performConns.push_back(performConnPair);
        }
    }

    vector<pair<int, int>> changes;
    vector<pair<int, int>> removeEdgesChanges;
    stack<int> visited;
    for(int i = 0; i < (desiredConnections - connections); i++) {
        visited.push(performConns[i].first);
        recursive_search(performConns[i].second, graph, changes, visited);
    }

    pair<int, int> removeEvent;
    for(int i = 0; i < changes.size(); i++) {
        auto iter = find(desired_graph[changes[i].first].begin(), desired_graph[changes[i].first].end(), changes[i].second);
        if (iter == desired_graph[changes[i].first].end()) {
            removeEvent = {changes[i].first, changes[i].second};
            removeEdgesChanges.push_back(removeEvent);
        }
    }

    cout << (changes.size() + removeEdgesChanges.size()) << "\n";
    for(int i = 0; i < changes.size(); i++) {
        cout << "+ " << changes[i].first << " " << changes[i].second << "\n";
    }

    for(int i = 0; i < removeEdgesChanges.size(); i++) {
        cout << "- " << removeEdgesChanges[i].first << " " << removeEdgesChanges[i].second << "\n";

    }

    return 0;
}