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

using namespace std;

using llu = unsigned long long;
using ll = long long;

#define int ll

const int MAX_N = 3e5 + 5;
vector<int> G1[MAX_N], G2[MAX_N];

auto edge(int u, int v) {
    return make_pair(min(u, v), max(u, v));
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, m1, m2;
    cin >> n >> m1;
    std::set<pair<int,int>> edges1;
    std::set<pair<int,int>> edges2;
    for (int i = 0; i < m1; i++) {
        int u, v;
        cin >> u >> v;
        G1[u].push_back(v);
        G1[v].push_back(u);
        edges1.insert(edge(u, v));
    }
    cin >> m2;
    for (int i = 0; i < m2; i++) {
        int u, v;
        cin >> u >> v;
        G2[u].push_back(v);
        G2[v].push_back(u);
        edges2.insert(edge(u, v));
    }
    vector<bool> vis(n+5, false);
    vector<tuple<bool, int, int>> ops;
    vector<tuple<bool, int, int>> inv_ops;
    function<void(int)> dfs = [&](int u) {
        vis[u] = true;
        for (int v : G1[u]) {
            if (!vis[v]) {
                if (edges1.count(edge(1, v)) == 0) {
                    ops.push_back({true, 1, v}); 
                }
                dfs(v);
            }
        }
    };
    dfs(1);
    for (auto [u, v] : edges1) {
        if (u != 1) {
            ops.push_back({false, u, v});
        }
    }
    vis = vector<bool>(n+5, false);
    function<void(int)> dfs2 = [&](int u) {
        vis[u] = true;
        for (int v : G2[u]) {
            if (!vis[v]) {
                if (edges2.count(edge(1, v)) == 0) {
                    inv_ops.push_back({false, 1, v}); 
                }
                dfs2(v);
            }
        }
    };
    dfs2(1);
    for (auto [u, v] : edges2) {
        if (u != 1) {
            ops.push_back({true, u, v});
        }
    }
    auto print_op = [] (auto op) {
        if (get<0>(op)) {
            cout << "+ " << get<1>(op) << " " << get<2>(op) << '\n';
        } else {
            cout << "- " << get<1>(op) << " " << get<2>(op) << '\n';
        }
    };
    cout << ops.size() + inv_ops.size() << '\n';
    for (auto op : ops) {
        print_op(op);
    }
    for (auto it = inv_ops.rbegin(); it != inv_ops.rend(); it++) {
        print_op(*it);
    }

    return 0;
}