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
#if defined(EMBE_DEBUG) && !defined(NDEBUG)
#include "embe-debug.hpp"
#else
#define LOG_INDENT(...) do {} while (false)
#define LOG(...) do {} while (false)
#define DUMP(...) do {} while (false)
#endif

#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <ranges>
#include <tuple>
#include <vector>
using namespace std;

namespace {

struct Graph {
  int n;
  vector<vector<int>> neigh;

  Graph(int n_): n(n_), neigh(n)
  {
  }

  void add(int i, int j)
  {
    neigh[i].push_back(j);
    neigh[j].push_back(i);
  }

  void sort()
  {
    for (auto& v: neigh) {
      ranges::sort(v);
    }
  }

  bool has(int i, int j) const
  {
    return ranges::binary_search(neigh[i], j);
  }

  vector<int> bfs(int s) const
  {
    vector<bool> done(n);
    vector<int> res;
    res.reserve(n);
    vector<int> cur;
    cur.reserve(n);
    vector<int> next;
    next.reserve(n);
    next.push_back(s);
    done[s] = true;
    while (!next.empty()) {
      assert(cur.empty());
      swap(cur, next);
      for (int u: cur) {
        assert(done[u]);
        res.push_back(u);

        for (int v: neigh[u]) {
          if (done[v]) continue;
          next.push_back(v);
          done[v] = true;
        }
      }
      cur.clear();
    }
    return res;
  }
};

Graph read(int n)
{
  Graph res(n);
  int m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    res.add(a - 1, b - 1);
  }
  res.sort();
  return res;
}

vector<tuple<char, int, int>> convert(Graph const& src, Graph const& dst)
{
  int n = src.n;
  assert(dst.n == n);

  vector<tuple<char, int, int>> res;

  if (src.neigh == dst.neigh) return res;

  for (int v: src.bfs(0)) {
    if (v == 0 || src.has(0, v)) continue;
    res.emplace_back('+', 0, v);
  }

  for (int u = 1; u < n; ++u) {
    {
      vector<int> deletions;
      deletions.reserve(n);
      ranges::set_difference(src.neigh[u], dst.neigh[u], back_inserter(deletions));
      for (int v: deletions) {
        if (u >= v) continue;
        res.emplace_back('-', u, v);
      }
    }
    {
      vector<int> additions;
      additions.reserve(n);
      ranges::set_difference(dst.neigh[u], src.neigh[u], back_inserter(additions));
      for (int v: additions) {
        if (u >= v) continue;
        res.emplace_back('+', u, v);
      }
    }
  }

  for (int v: dst.bfs(0) | views::reverse) {
    if (v == 0 || dst.has(0, v)) continue;
    res.emplace_back('-', 0, v);
  }

  return res;
}

}

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

  int n;
  cin >> n;
  auto src = read(n);
  auto dst = read(n);

  auto res = convert(src, dst);
  cout << res.size() << endl;
  for (auto& [op, x, y]: res) {
    cout << op << ' ' << x + 1 << ' ' << y + 1 << endl;
  }

  return 0;
}