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
#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>

using namespace std;

const int MAX_N = 30001;

// void print(vector<int>& v) {
//     for (const auto &n : v) {
//         cout << n << " ";
//     }
//     cout << endl;
// }

// void print(set<int>& v) {
//     for (const auto &n : v) {
//         cout << n << " ";
//     }
//     cout << endl;
// }

vector<int> bfsuj(set<int> G[], int root) {
    const int PENDING = 0;
    const int DONE = 2;
    const int IN_QUEUE = 1;
    
    vector<int> output;

    int visited[MAX_N];
    fill(visited, visited+MAX_N, 0);
    deque<int> tbd;
    for (const auto &n : G[root])
    {
        output.push_back(n);
        tbd.push_back(n);
        visited[n] = IN_QUEUE;
    }
    visited[root] = DONE;
    
    
    while (!tbd.empty())
    {
        int next = tbd.front();
        tbd.pop_front();

        for (const auto &n : G[next])
        {
            if (visited[n] == PENDING)
            {
                output.push_back(n);
                tbd.push_back(n);
                visited[n] = IN_QUEUE;
            }
        }
    }
    return output;
}

set<int> adj[MAX_N];
set<int> res[MAX_N];

typedef struct
{
    bool isAdd;
    int left;
    int right;
} action;

vector<action> output;

action Add(int left, int right)
{
    action a;
    a.left = left;
    a.right = right;
    a.isAdd = true;
    return a;
}

action Remove(int left, int right)
{
    action a;
    a.left = left;
    a.right = right;
    a.isAdd = false;
    return a;
}

bool contains(const set<int>& s, int e) {
    return s.find(e) != s.end();
}

set<int> except(const set<int>& left, const set<int>& right) {
    set<int> res;
    set_difference(
        left.begin(), left.end(),
        right.begin(), right.end(),
        inserter(res, res.end())
    );
    return res;
}



int main()
{
    int n;
    int inp_m;
    int u, v;
    cin >> n >> inp_m;
    int root = n;

    for (int i = 0; i < inp_m; i++)
    {
        cin >> u >> v;
        if (v != root) adj[u].insert(v);
        if (u != root) adj[v].insert(u);
    }
    int res_m;
    cin >> res_m;

    for (int i = 0; i < res_m; i++)
    {
        cin >> u >> v;
        if (v != root) res[u].insert(v);
        if (u != root) res[v].insert(u);
    }
    vector<int> bfs_adj = bfsuj(adj, root);
    vector<int> bfs_res = bfsuj(res, root);
    
    vector<action> output;

    // dodaj wszystko
    for (const auto &n : bfs_adj) {
        if (!contains(adj[root], n)) {
            output.push_back(Add(root, n));
        }
    }
    
    // zrob graf
    for (int left = 1; left < root; left++) {
        set<int> toAdd = except(res[left], adj[left]);
        set<int> toRemove = except(adj[left], res[left]);
        for (const auto &right : toAdd) {
            if (left < right && right != root) {
                output.push_back(Add(left, right));
            }
        }
        for (const auto &right : toRemove) {
            if (left < right && right != root) {
                output.push_back(Remove(left, right));
            }
        }
    }

    // koniec, sprzatanie
    for (int i = bfs_res.size() - 1; i >= 0; i--) {
        int v = bfs_res[i];
        if (!contains(res[root], v)) {
            output.push_back(Remove(root, v));
        }
    }

    // print
    cout << output.size() << endl;
    for (const auto &act : output)
    {
        cout << (act.isAdd ? "+ " : "- ");
        cout << act.left << " " << act.right << endl;
    }
    return 0;
}