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
#include <iostream>
#include <unordered_set>
#include <sstream>
#include <vector>

using namespace std;

uint32_t check_intersection(uint32_t left, uint32_t right, vector<unordered_set<uint32_t>> &adj) {
    for (auto n: adj[left]) {
        if (adj[right].contains(n)) {
            return -1;
        }
    }

    return *adj[right].begin();
}

void add_edge(uint32_t left, uint32_t right, vector<unordered_set<uint32_t>> &adj, vector<pair<uint32_t, uint32_t>> &rm,
              vector<unordered_set<uint32_t>> &adj_d, stringstream& ss, uint32_t &count) {
    if (adj[left].contains(right)) {
        return;
    }
    uint32_t how_many = 0;
    uint32_t new_neigh;
    while ((new_neigh = check_intersection(left, right, adj)) != -1)  {
        rm.emplace_back(left, right);
        adj[left].insert(right);
        adj[right].insert(left);
        right = new_neigh;
        how_many++;
    }
    rm.emplace_back(left, right);
    adj[left].insert(right);
    adj[right].insert(left);
    how_many++;


    for (int i = 0; i < how_many; ++i) {
        auto [l, r] = rm[rm.size() - 1 - i];
//        ss << "dupcia";
        ss << "+ " << l + 1 << ' ' << r + 1 << '\n';
        count++;
    }
}


void remove_edge(uint32_t left, uint32_t right, vector<unordered_set<uint32_t>> &adj, vector<pair<uint32_t, uint32_t>> &rm,
              vector<unordered_set<uint32_t>> &adj_d, stringstream& ss, uint32_t &count) {
    if (adj_d[left].contains(right)) {
        return;
    }


    uint32_t how_many = 0;
    uint32_t new_neigh = right;
    while ((new_neigh = check_intersection(left, new_neigh, adj)) != -1)  {
        rm.emplace_back(left, new_neigh);
        adj[left].insert(new_neigh);
        adj[new_neigh].insert(left);
        how_many++;
    }
    rm.emplace_back(left, right);


    for (int i = 0; i < how_many; ++i) {
        auto [l, r] = rm[rm.size() - 2 - i];
//        ss << "niedupcia";
        ss << "+ " << l + 1 << ' ' << r + 1 << '\n';
        count++;
    }
}

void reme_edge(uint32_t left, uint32_t right, vector<unordered_set<uint32_t>> &adj, vector<pair<uint32_t, uint32_t>> &rm,
              vector<unordered_set<uint32_t>> &adj_d, stringstream& ss, uint32_t &count) {
    if (!adj_d[left].contains(right)) {
        return;
    }
    uint32_t how_many = 0;
    uint32_t new_neigh;
    while ((new_neigh = check_intersection(left, right, adj)) != -1)  {
        rm.emplace_back(left, new_neigh);
        adj[left].insert(new_neigh);
        adj[new_neigh].insert(left);
        how_many++;
    }

    for (int i = 0; i < how_many; ++i) {
        auto [l, r] = rm[rm.size() - 1 - i];
//        cout << 'here';
        ss << "+ " << l + 1 << ' ' << r + 1 << '\n';
        count++;
    }
    rm.emplace_back(left, right);
}


int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    uint32_t n;
    cin >> n;
    uint32_t m;
    vector<unordered_set<uint32_t>> adj_b(n);
    vector<unordered_set<uint32_t>> adj_d(n);
    cin >> m;
    uint32_t left, right;
    for (int i = 0; i < m; ++i) {
        cin >> left >> right;
        left--;
        right--;
        adj_b[left].insert(right);
        adj_b[right].insert(left);
    }
    cin >> m;
    for (int i = 0; i < m; ++i) {
        cin >> left >> right;
        left--;
        right--;
        adj_d[left].insert(right);
        adj_d[right].insert(left);
    }
    vector<pair<uint32_t, uint32_t>> to_rm;
    stringstream ss;
    uint32_t count = 0;
//    cout << "starting" << endl;
    vector<pair<uint32_t, uint32_t>> for_adding;
    vector<pair<uint32_t, uint32_t>> for_removal;
    for (int i = 0; i < n; ++i) {
        for (auto neigh: adj_d[i]) {
            if (!adj_b[i].contains(neigh)) {
                for_adding.emplace_back(i, neigh);
            }
        }
    }

    for (int i = 0; i < n; ++i) {
        for (auto neigh: adj_b[i]) {
            if (!adj_d[i].contains(neigh)) {
                for_removal.emplace_back(i, neigh);
            }
        }
    }

    for (auto [le, ri]: for_adding) {
        if (!adj_b[le].contains(ri)){
            add_edge(le, ri, adj_b, to_rm, adj_d, ss, count);
        }
    }
    for (auto [le, ri]: for_removal) {
        remove_edge(le, ri, adj_b, to_rm, adj_d, ss, count);
    }



//    cout << "here" << endl;
    for (int i = to_rm.size() - 1; i >= 0; --i) {
//        cout <<"lol";
        auto [l, r] = to_rm[i];
        if (!adj_d[l].contains(r) && adj_b[l].contains(r)) {
            ss << "- " << l + 1 << ' ' << r + 1 << '\n';
            adj_b[l].erase(r);
            adj_b[r].erase(l);
            count++;
        }
    }


    cout << count << '\n' << ss.str();


    return 0;
}