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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <cstdio>
#include <set>
#include <queue>

#define DBG(X)
using namespace std;

set<int> S[30001]; 
set<int> D[30001]; 

int vis[30001];

int sasD_root[30001];
int ile_sasD_root = 0;

int dodanych_krawedzi_z_root = 0;
int dodane[50001];

pair<int, int> usuniete_kraw[50001];

int dir_edge_root[50001];
int tymczasowe_kraw[50001];

pair<char, pair<int, int>> ret[200001];


void dfs(int u, int r)
{
  vis[u] = 1;
  for (auto it : S[u])
  {
    if (!vis[it])
    {
      if (S[r].find(it) == S[r].end() && S[it].find(r) == S[it].end())
      {
        dodane[dodanych_krawedzi_z_root] = it;
        dodanych_krawedzi_z_root++;
      }
      dfs(it, r);
    }
  }
}


int vis_bfs[30001];
int p[30001];

int vert_to_clear[30001];
int vert_cleared = 0;

void find_path(int u)
{
  queue<int> q;
  q.push(u);
  vis_bfs[u] = 1;
  p[u] = -1;
  //vert_to_clear[vert_cleared] = u;
  //vert_cleared++;
  
  while (!q.empty())
  {
    int v = q.front();
    q.pop();
    for (auto it : S[v])
    {
      if (!vis_bfs[it])
      {
        vis_bfs[it] = 1;
        q.push(it);
        p[it] = v;
        //vert_to_clear[vert_cleared] = it;
        //vert_cleared++;
      }
    }
  }
}


bool check(int a, int b)
{
  for (auto it : S[a])
  {
    if (it != b && S[b].find(it) != S[b].end())
      return true;
  }
  return false;
}

int main()
{
  int n;
  scanf("%d", &n);
  
  int m1;
  scanf("%d", &m1);
 
  for (int i = 0; i < m1; i++)
  {
    int a, b;
    scanf("%d %d", &a, &b);
    S[a].insert(b);
    S[b].insert(a);
  }

  int m2;
  scanf("%d", &m2);
 
  for (int i = 0; i < m2; i++)
  {
    int a, b;
    scanf("%d %d", &a, &b);
    D[a].insert(b);
    D[b].insert(a);
  }

  int root = 0;
  int mindeg = n;
  for (int i = 1; i <= n; i++)
  {
    if (S[i].size() < mindeg)
    {
      mindeg = S[i].size();
      root = i;
    }
  }

  dfs(root, root);

  DBG(printf("root %d mindeg %d\n", root, mindeg);)
  int ile_ret = 0;
  
  for (auto it : D[root])
  {
    sasD_root[it] = 1;
  }
DBG(printf("faza 1 start\n");)
  for (int i = 0; i < dodanych_krawedzi_z_root; i++)
  {
    int u = root;
    int v = dodane[i];
    DBG(printf("+ %d %d\n", u, v);)
    S[u].insert(v);
    S[v].insert(u);
    ret[ile_ret] = {'+', {u, v}};
    ile_ret++;
  }
DBG(printf("faza 1 end\n");
printf("faza 2 start\n");)
  for (int i = 1; i <= n; i++)
  {
    for (auto it : D[i])
    {
      if (it < i) continue;
      if (S[i].find(it) == S[i].end() && S[it].find(i) == S[it].end())
      {
        S[i].insert(it);
        S[it].insert(i);
        DBG(printf("+ %d %d\n", i, it);)
        ret[ile_ret] = {'+', {i, it}};
        ile_ret++;
      }
    }
  }
DBG(printf("faza 2 end\n");)
  int ile_usunietych_kraw = 0;
  DBG(printf("faza 3 start\n");)
  for (int i = 1; i <= n; i++)
  {
    for (auto it : S[i])
    {
      if (it < i) continue;
      if (D[i].find(it) == D[i].end() && D[it].find(i) == D[it].end())
      {
        if (i != root && it != root)
        {
          usuniete_kraw[ile_usunietych_kraw] = {i, it};
          ile_usunietych_kraw++;
        }
      }
    }
  }
  
  for (int i = 0; i < ile_usunietych_kraw; i++)
  {
    int u = usuniete_kraw[i].first;
    int v = usuniete_kraw[i].second;
    S[u].erase(v);
    S[v].erase(u);
    DBG(printf("- %d %d\n", u, v);)
    ret[ile_ret] = {'-',{u, v}};
    ile_ret++;
  }

DBG(printf("faza 3 end\n");

printf("faza 4 start\n");)
  for (int i = dodanych_krawedzi_z_root - 1; i >= 0; i--)
  {
    int u = root;
    int v = dodane[i];
    if (!sasD_root[v])
    {
      DBG(printf("- %d %d\n", u, v);)
      S[u].erase(v);
      S[v].erase(u);
      ret[ile_ret] = {'-',{u, v}};
      ile_ret++;
    }
  }

  DBG(printf("faza 4 end\n");)
  DBG(printf("faza 5 start\n");)

  int cnt_dir_edge_root = 0;
  for (auto it : S[root])
  {
    if (D[it].find(root) == D[it].end() && D[root].find(it) == D[root].end())
    {
      dir_edge_root[cnt_dir_edge_root] = it;
      cnt_dir_edge_root++;
    }
  }

  for (int i = 0; i < cnt_dir_edge_root; i++)
  {
    int u = root;
    int v = dir_edge_root[i];
    S[u].erase(v);
    S[v].erase(u);
  }
  
  find_path(root);

  for (int i = 0; i < cnt_dir_edge_root; i++)
  {
    int u = root;
    int v = dir_edge_root[i];
    //S[u].insert(v);
    //S[v].insert(u);
  }
  
  DBG(printf("cnt_dir_edge_root = %d\n",cnt_dir_edge_root);)
  
  for (int i = 0; i < cnt_dir_edge_root; i++)
  {
    int u = root;
    int v = dir_edge_root[i];

    for (int j = 0; j < vert_cleared; j++)
    {
      //p[vert_to_clear[j]] = 0;
      //vis_bfs[vert_to_clear[j]] = 0;
    }
    //vert_cleared = 0;

    //find_path(u);
    
    int ile_tymczasowe_kraw = 0;

    pair<int, int> pop_kraw = {v, root};

    DBG(printf("v %d pv %d root %d\n", v,p[v], root);)
    v = p[v];
    DBG(printf("v %d pv %d root %d\n", v,p[v], root);)
    if (p[v] == root)
    {
      DBG(printf("- %d %d\n", pop_kraw.first, pop_kraw.second);)
      ret[ile_ret] = {'-', pop_kraw};
      ile_ret++;
    }
    else
    {
      while (p[v] != root)
      {
        if (S[v].find(root) == S[v].end() && S[root].find(v) == S[root].end())
        {
          DBG(printf("+ %d %d\n", root, v);
          printf("- %d %d\n", pop_kraw.first, pop_kraw.second);)

          ret[ile_ret] = {'+',{root, v}};
          ile_ret++;
          ret[ile_ret] = {'-', pop_kraw};
          ile_ret++;

          pop_kraw = {root, v};
        }
        else
        {
          DBG(printf("x - %d %d\n", pop_kraw.first, pop_kraw.second);)
          ret[ile_ret] = {'-', pop_kraw};
          ile_ret++;
          pop_kraw = {-1, -1};
          break;
        }
        v = p[v];
      }
      if (pop_kraw.first != -1)
      {
        DBG(printf("- %d %d\n", pop_kraw.first, pop_kraw.second);)
        ret[ile_ret] = {'-', pop_kraw};
        ile_ret++;
      }
    }
    u = root;
    v = dir_edge_root[i];

    //S[u].erase(v);
    //S[v].erase(u);

  }
  DBG(printf("faza 5 end\n");)
  
  printf("%d\n", ile_ret);
  
  for (int i = 0; i < ile_ret; i++)
  {
    printf("%c %d %d\n", ret[i].first, ret[i].second.first, ret[i].second.second);
  }
  
  return 0;
}