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
#include <iostream>
#include <set>
#include <unordered_set>
#include <vector>
#include <algorithm>
using namespace std;

const bool DBG = false;


int n;
int ms;
int md;

struct G {
  set<pair<int, int>> edges;
};
G s, d;

G read() {
  G g;
  int cnt;
  cin >> cnt;
  for (int i = 0; i < cnt; ++i) {
    int a, b;
    cin >> a >> b;
    g.edges.insert(pair(a, b));
    g.edges.insert(pair(b, a));
  }
  return g;
}

void printG(G &g) {
  for (auto it : g.edges) {
    cout << it.first << " - " << it.second << endl;
  }
}


void dfs_helper(int node, unordered_set<int> &visited, vector<int> &added, G &g) {
  const bool DBG = false;
  visited.insert(node);
  // Find the range for edges of the node.
  auto b = g.edges.lower_bound(pair(node, 0));
  auto e = g.edges.upper_bound(pair(node+1, 0));
  if (DBG) {
    cout << "+dfs " << node << endl;
  }
  for (auto it = b; it != e; ++it) {
    int to = it->second;
    if (DBG) {
      cout << " dfs " << to << endl;
    }
    if (visited.count(to)) continue;
    if (!g.edges.count(pair(1, to))) {
      if (DBG) {
        cout << " dfs add " << to << endl;
      }
      added.push_back(to);
    }
    dfs_helper(to, visited, added, g);
  }
  if (DBG) {
    cout << "-dfs " << node << endl;
  }
}

// Returns added edges.
vector<int> dfs(G &g) {
  vector<int> added;
  unordered_set<int> visited;
  dfs_helper(1, visited, added, g);
  return added;
}

void print_added(vector<int> &added) {
  for (auto a : added) {
    cout << "+ 1 " << a << '\n';
  }
}

void print_removed(vector<int> added) {
  reverse(added.begin(), added.end());

  for (auto a : added) {
    cout << "- 1 " << a << '\n';
  }
}

void print_added(vector<pair<int, int>> &added) {
  for (auto a : added) {
    cout << "+ " << a.first << ' ' << a.second << '\n';
  }
}

void print_removed(vector<pair<int, int>> &r) {
  for (auto a : r) {
    cout << "- " << a.first << ' ' << a.second << '\n';
  }
}

int main() {
  cin >> n;
  s = read();
  d = read();
  // First, create edges from 1 to everything else.
  auto s_added = dfs(s);
  if (DBG) {
    cout << "s_added:\n";
    print_added(s_added);
  }
  auto d_added = dfs(d);
  if (DBG) {
    cout << "d_added:\n";
    print_added(d_added);
  }
  vector<pair<int, int>> missing;
  for (auto de : d.edges) {
    if (de.second < de.first) continue;
    if (de.first == 1) continue;
    if (s.edges.count(de) == 0) {
      missing.push_back(de);
    }
  }
  vector<pair<int, int>> extra;
  for (auto se : s.edges) {
    if (se.second < se.first) continue;
    if (se.first == 1) continue;
    if (d.edges.count(se) == 0) {
      extra.push_back(se);
    }
  }
  cout << s_added.size() + d_added.size() + missing.size() + extra.size() << endl;
  print_added(s_added);
  print_added(missing);
  print_removed(extra);
  print_removed(d_added);

  return 0;
}