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
#include <iostream>
#include <utility>
#include <set>
#include <algorithm>
#include <iterator>
#include <list>
#include <sstream> 

using namespace std;

bool mod_edge(int a, int b, bool** g, int n, set<pair<int, int>> &sett, char op, list<string> &result) {
    //cout << op <<" run for " << a << ' ' << b << '\n'; 
    if (a == 0 || b == 0) {
        return false;
    }

    if (op == '+' && g[a][b]) {
        return false;
    }

    if (op == '-' && !g[a][b]) {
        return false;
    }
    
    int i {};
    int c {};
    bool end {};

    int tmp {};
    stringstream strs {};

    if (a > b) {
        tmp = a;
        a = b;
        b = tmp;
    }

    i = 1;
    c = 0;
    while(i <= n && c == 0) {
        if (i != a && i != b) {
            if (g[a][i] && g[b][i]) {
                c = i;
            }
        }
        i++;
    }

    //cout << "c = " << c << '\n';

    if (c > 0) {
        if (op == '+') {
            g[a][b] = true;
            g[b][a] = true;
        } else {
            g[a][b] = false;
            g[b][a] = false;
        }
        strs << op << ' ' << a << ' ' << b;
        result.push_back(strs.str());
        //cout << op << ' ' << a << ' ' << b << '\n';
        return true;
    } else {
        i = 1;
        end = false;
        while(!end && i <= n) {
            if (i != a && i != b) {
                if(!g[a][i] && g[b][i]) {
                    end = mod_edge(a, i, g, n, sett, op, result);
                    if (end) {
                        if (a < i) {
                            sett.insert(make_pair(a, i));
                        } else {
                            sett.insert(make_pair(i, a));
                        }
                    }
                } else if(g[a][i] && !g[b][i]) {
                    end = mod_edge(b, i, g, n, sett, op, result);
                    if (end) {
                        if (b < i) {
                            sett.insert(make_pair(b, i));
                        } else {
                            sett.insert(make_pair(i, b));
                        }
                    }
                }
            }
            i++;
        }
        if (end) {
            return mod_edge(a, b, g, n, sett, op, result);
        } else {
            return false;
        }
    }

}

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

    set<pair<int, int>> src;
    set<pair<int, int>> dst;
    set<pair<int, int>> to_add;
    set<pair<int, int>> to_remove;

    int n {}, ms {}, md {}, x {}, y {};
    list<string> result {};
    set<pair<int, int>>::iterator it;

    cin >> n;
    cin >> ms;

    bool** connections {new bool*[n+1]};

    for (int i = 0; i <= n; i++) {
        connections[i] = new bool[n+1];
        for (int j = 0; j <= n; j++) {
            connections[i][j] = false;
        }
    }
    
    for (int i = 0; i < ms; i++) {
        cin >> x;
        cin >> y;
        connections[x][y] = true;
        connections[y][x] = true;
        if (x < y) {
            src.insert(make_pair(x, y));
        } else {
            src.insert(make_pair(y, x));
        }
    }

    cin >> md;

    for (int i = 0; i < md; i++) {
        cin >> x;
        cin >> y;
        if (x < y) {
            dst.insert(make_pair(x, y));
        } else {
            dst.insert(make_pair(y, x));
        }
    }

    set_difference(dst.begin(), dst.end(), src.begin(), src.end(),
                        inserter(to_add, to_add.begin()));

    set_difference(src.begin(), src.end(), dst.begin(), dst.end(),
                        inserter(to_remove, to_remove.begin()));

    
    while(!to_add.empty() || !to_remove.empty()) {
    
        it = to_add.begin();
        while(it != to_add.end()) {
            if(mod_edge(it->first, it->second, connections, n, to_remove, '+', result)) {
                it = to_add.erase(it);
            } else {
                it++;
            }
            
        }

        it = to_remove.begin();
        while(it != to_remove.end()) {
            if(mod_edge(it->first, it->second, connections, n, to_add, '-', result)) {
                it = to_remove.erase(it);
            }
            else {
                it++;
            }       
        }
    }

    cout << result.size() << '\n';

    for (string s : result) {
        cout << s << '\n';
    }
    
    return 0;
}