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
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define st first
#define nd second

const int N = 30005;

bitset<N> con1[N];
bitset<N> con2[N];

vector<int> G1[N];
vector<int> G2[N];

bool visited1[N];
bool visited2[N];

struct Op {
  char op;
  int a, b;
};

vector<Op> ops;
set<int> added_to;

set<pair<int, int>> cur_edges;
set<pair<int, int>> goal_edges;

void dfs_add(int x) {
  visited1[x] = true;
  for (int el : G1[x]) {
    if (visited1[el]) {
      continue;
    }
    if (!con1[1][el]) {
      ops.emplace_back('+', 1, el);
      added_to.insert(el);
      cur_edges.insert({1, el});
    }
    dfs_add(el);
  }
}

void dfs_remove(int x) {
  visited2[x] = true;
  for (int el : G2[x]) {
    if (visited2[el]) {
      continue;
    }
    dfs_remove(el);
  }
  if (x != 1) {
    if (!con2[1][x]) {
      ops.emplace_back('-', 1, x);
      assert(cur_edges.contains({1, x}));
      cur_edges.erase({1, x});
    }
  }
}

void TEST([[maybe_unused]] int testcase_i) {
  int n;
  cin >> n;

  int m1;
  cin >> m1;
  rep(i, m1) {
    int a, b;
    cin >> a >> b;
    G1[a].push_back(b);
    G1[b].push_back(a);
    con1[a][b] = true;
    con1[b][a] = true;
    cur_edges.insert({min(a, b), max(a, b)});
  }

  int m2;
  cin >> m2;
  rep(i, m2) {
    int a, b;
    cin >> a >> b;
    G2[a].push_back(b);
    G2[b].push_back(a);
    con2[a][b] = true;
    con2[b][a] = true;
    goal_edges.insert({min(a, b), max(a, b)});
  }

  dfs_add(1);
  for (auto edge : goal_edges) {
    if (!cur_edges.contains(edge)) {
      ops.emplace_back('+', edge.first, edge.second);
      cur_edges.insert(edge);
    }
  }

  vector<pair<int, int>> to_remove;
  for (auto edge : cur_edges) {
    if (!goal_edges.contains(edge) and edge.first != 1) {
      ops.emplace_back('-', edge.first, edge.second);
      to_remove.push_back(edge);
    }
  }

  for (auto edge : to_remove) {
    cur_edges.erase(edge);
  }

  dfs_remove(1);

  assert(cur_edges == goal_edges);
  cout << ops.size() << "\n";
  for (auto el : ops) {
    cout << el.op << " " << el.a << " " << el.b << "\n";
  }
}

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

  cout << setprecision(20) << fixed;

  // srand(time(NULL));

  int t = 1;
  // cin >> t;
  rep(i, t) {
    TEST(i);
  }

  return 0;
}