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
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

int n, a, b, ms, md;

const int mxn = 30005;
const int mxm = 50005;

bool is_edge[mxn][mxn];
pair<int,int> old_edges[mxm];
pair<int,int> new_edges[mxm];
vector<int> edges[mxn];
int visited[mxn];
int father[mxn];
vector<int> bfs_order;

vector<pair<int,int>> added_bonus_edges;
vector<pair<int, int>> added_edges;
vector<pair<int, int>> removed_edges;
vector<pair<int, int>> removed_bonus_edges;

struct PairHash {
    template <typename T, typename U>
    size_t operator()(const pair<T, U>& p) const {
        auto hash1 = hash<T>{}(p.first);
        auto hash2 = hash<U>{}(p.second);
        return hash1 ^ hash2;
    }
};

int main(){
    ios::sync_with_stdio(false);

    cin >> n;

    cin >> ms;
    for(int i = 0; i < ms; ++i){
        cin >> a >> b;

        if(a < b)
            old_edges[i] = {a, b};
        else
            old_edges[i] = {b, a};

        edges[a].push_back(b);
        edges[b].push_back(a);

        is_edge[a][b] = true;
        is_edge[b][a] = true;
    }

    cin >> md;
    for(int i = 0; i < md; ++i){
        cin >> a >> b;

        if(a < b)
            new_edges[i] = {a, b};
        else
            new_edges[i] = {b, a};
    }

    bfs_order.reserve(n);
    bfs_order.push_back(1);

    for(int i = 0; i < bfs_order.size(); ++i){
        int actual = bfs_order[i];
        visited[actual] = 1;
        for(int j = 0; j < edges[actual].size(); ++j){
            int child = edges[actual][j];
            if(!visited[child]){
                bfs_order.push_back(child);
                father[child] = actual;
                if(father[child] != 1){
                    added_bonus_edges.push_back({1, child});

                    is_edge[1][child] = true;
                    is_edge[child][1] = true;
                }
            }
        }
    }

    for(int i = 0; i < md; ++i){
        a = new_edges[i].first;
        b = new_edges[i].second;

        if(!is_edge[a][b]){
            added_edges.push_back({a, b});

            is_edge[a][b] = true;
            is_edge[b][a] = true;
        }
    }

    unordered_set<pair<int, int>, PairHash> hashSet;
    for(int i = 0; i < ms; ++i){
        hashSet.insert(old_edges[i]);
    }
    for(int i = 0; i < md; ++i) {
        auto actual_edge = new_edges[i];
        if(hashSet.count(actual_edge) > 0) {
            hashSet.erase(actual_edge);
        }
    }
    for(int i = 0; i < added_bonus_edges.size(); ++i){
        auto actual_edge = added_bonus_edges[i];
        if(hashSet.count(actual_edge) > 0) {
            hashSet.erase(actual_edge);
        }
    }
    for(auto edge : hashSet) {
        removed_edges.push_back(edge);
    }

    unordered_set<pair<int, int>,PairHash> hashSet2;
    for(int i = 0; i < md; ++i){
        hashSet2.insert(new_edges[i]);
    }
    for(int i = added_bonus_edges.size()-1; i >= 0; --i){
        auto actual_edge = added_bonus_edges[i];
        if(hashSet2.count(actual_edge) <= 0){
            removed_bonus_edges.push_back(actual_edge);
        }
    }

    int num_of_operations = added_bonus_edges.size() + added_edges.size() + removed_edges.size() + removed_bonus_edges.size();

    cout << num_of_operations << "\n";

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

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

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

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



    return 0;
}