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

vector<vector<int>> g, org_g;  // g[0] is unused
vector<tuple<char, int, int>> out;
vector<bool> vis, vis2;
set<int> s1, s2;
void dfs_1(int u) {
    vis[u] = 1;
    for(auto &&v : org_g[u]) {
        if(vis[v]) continue;
        vis[v] = 1;
        if(s1.count(v) == 0) out.push_back({'+', 1, v});
        dfs_1(v);
    }
}
void dfs_2(int u) {
    vis2[u] = 1;
    for(auto &&v : g[u]) {
        if(vis2[v]) continue;
        vis2[v] = 1;
        dfs_2(v);
        if(s2.count(v) == 0) out.push_back({'-', 1, v});
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, m, a, b;
    cin >> n >> m;
    g.resize(n + 1);
    org_g.resize(n + 1);
    for(int i = 0; i < m; ++i) {
        cin >> a >> b;
        org_g[a].push_back(b);
        org_g[b].push_back(a);
    }
    vis.resize(n + 1);
    copy(org_g[1].begin(), org_g[1].end(), std::inserter(s1, s1.end()));
    dfs_1(1);

    for(int u = 2; u <= n; ++u)
        for(auto &&v : org_g[u])
            if(u < v) out.push_back({'-', u, v});

    cin >> m;
    for(int i = 0; i < m; ++i) {
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
        if(min(a, b) != 1) out.push_back({'+', a, b});
    }
    copy(g[1].begin(), g[1].end(), std::inserter(s2, s2.end()));
    vis2.resize(n + 1);
    dfs_2(1);

    cout << out.size() << '\n';
    for(auto &&[c, u, v] : out) cout << c << ' ' << u << ' ' << v << '\n';
}