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

struct Node {
  vector<int> adj;
  int dist = -1;
};

vector<Node> readGraph(int n) {
  vector<Node> g(n);
  int m;
  cin >> m;

  for(int i=0;i<m;i++) {
    int a,b;
    cin >> a >> b, a--, b--;
    g[a].adj.push_back(b);
    g[b].adj.push_back(a);
  }

  return g;
}

vector<tuple<char,int,int>> normalize(vector<Node> g) {
  vector<tuple<char,int,int>> result;

  {
    queue<int> q;
    q.push(0);
    g[0].dist = 0;

    while(!q.empty()) {
      int v = q.front();
      q.pop();

      if(g[v].dist > 1)
        result.push_back({'+', 0, v});

      for(int u : g[v].adj)
        if(g[u].dist == -1) {
          g[u].dist = g[v].dist+1;
          q.push(u);
        }
    }

    for(auto [_, u, v] : result) {
      g[v].adj.push_back(v);
      g[u].adj.push_back(u);
    }
  }

  {
    for(int u=1;u<g.size();u++)
      for(int v : g[u].adj)
        if(v > u)
          result.push_back({'-', u, v});
  }

  return result;
}

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

  int n;
  cin >> n;

  vector<tuple<char,int,int>> result;
  {
    auto g = readGraph(n);
    auto t = normalize(g);
    result.insert(result.end(), t.begin(), t.end());
  }
  {
    auto g = readGraph(n);
    auto t = normalize(g);
    for(auto& [c, u, v] : t)
      c = (c == '+' ? '-' : '+');
    result.insert(result.end(), t.rbegin(), t.rend());
  }

  cout << result.size() << "\n";
  for(auto [c, u, v] : result)
    cout << c << " " << u+1 << " " << v+1 << "\n";

  return 0;
}