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

using namespace std;

vector<pair<char, pair<int, int> > > result;
map<pair<int, int>, bool> wyjebane;

void BFS_add(int number_of_vertices, vector<vector<int> > &graph, vector<bool> &do_not_remove){
    queue<int> q; q.push(1);;
    
    vector<bool> visited(number_of_vertices + 1, false);
    visited[1] = true;

    while(!q.empty()){
        int actual_vertex = q.front();
        q.pop();

        for(int i = 0; i < (int)graph[actual_vertex].size(); i ++){
            int u = graph[actual_vertex][i];

            if(visited[u]) continue;

            if(actual_vertex != 1){
                graph[1].push_back(u);
                result.push_back(make_pair('+', make_pair(1, u)));
            }

            q.push(u);
            visited[u] = true;
        }
    }
}

void BFS_remove(int number_of_vertices, vector<vector<int> > &graph, vector<bool> &do_not_remove){
    queue<int> q; q.push(1);

    vector<bool> visited(number_of_vertices + 1, false);
    vector<int> to_delete;
    visited[1] = true;

    while(!q.empty()){
        int actual_vertex = q.front();
        q.pop();

        for(int i = 0; i < (int)graph[actual_vertex].size(); i ++){
            int u = graph[actual_vertex][i];

            if(visited[u] || (do_not_remove[u] == 0 && actual_vertex == 1) || wyjebane[make_pair(actual_vertex, u)] == 1) continue;

            q.push(u);
            visited[u] = true;
        }

        if(actual_vertex != 1 && do_not_remove[actual_vertex] == 0) to_delete.push_back(actual_vertex);
    }

    for(int i = (int)to_delete.size() - 1; i >= 0; i --){
        result.push_back(make_pair('-', make_pair(1, to_delete[i])));
    }
}

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

    int number_of_vertices, number_of_edges; cin >> number_of_vertices >> number_of_edges;

    vector<vector<int> > graph(number_of_vertices + 1);
    map<pair<int, int>, bool> edges;
    vector<bool> do_not_remove(number_of_vertices + 1, false);

    for(int i = 0; i < number_of_edges; i ++){
        int u, v; cin >> u >> v;

        if(u == 1) do_not_remove[v] = true;
        if(v == 1) do_not_remove[u] = true;

        graph[u].push_back(v);  graph[v].push_back(u);
        edges[make_pair(u, v)] = false;  edges[make_pair(v, u)] = false;
    }

    int number_of_edges_2; cin >> number_of_edges_2;

    vector<pair<int, int> > add, remove;

    for(int i = 0; i < number_of_edges_2; i ++){
        int u, v; cin >> u >> v;

        auto iter = edges.find(make_pair(u, v));

        if(iter == edges.end()){
            add.push_back(make_pair(u, v));
            
            if(u == 1) do_not_remove[v] = true;
            if(v == 1) do_not_remove[u] = true;
        } else {
            edges[make_pair(u, v)] = true;
            edges[make_pair(v, u)] = true;
        }
    }

    for(auto i : edges){
        if(i.second == 0){
            if(i.first.first > i.first.second) continue;

            remove.push_back(make_pair(i.first.first, i.first.second));

            if(i.first.first == 1) do_not_remove[i.first.second] = false;
            if(i.first.second == 1) do_not_remove[i.first.first] = false;
        }
    }

    BFS_add(number_of_vertices, graph, do_not_remove);

    for(int i = 0; i < (int)add.size(); i ++){
        int u = add[i].first, v = add[i].second;

        if(u == 1 || v == 1) continue;

        result.push_back(make_pair('+', make_pair(u, v)));
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    for(int i = 0; i < (int)remove.size(); i ++){
        int u = remove[i].second, v = remove[i].first;

        if(u == 1 || v == 1) continue;

        result.push_back(make_pair('-', make_pair(u, v)));
        wyjebane[make_pair(u, v)] = true;
        wyjebane[make_pair(v, u)] = true;
    }

    BFS_remove(number_of_vertices, graph, do_not_remove);

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

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