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

vector<unordered_set<int>> edges;
vector<unordered_set<int>> edges_wanted;
int n, m_s, m_d;

enum op_t : bool {
    op_add,
    op_remove
};

struct item {
    op_t op;
    int a,b;
};

vector<item> logs;

void dfs_add(int v, vector<bool> &seen) {
    seen[v] = true;

    if (!edges[v].contains(0) && v != 0)
        logs.push_back((item){.op = op_add, .a = 0, .b = v});

    for (int e : edges[v])
        if (!seen[e])
            dfs_add(e, seen);
}

void dfs_remove(int v, vector<bool> &seen) {
    seen[v] = true;

    for (int e : edges_wanted[v])
        if (!seen[e])
            dfs_remove(e, seen);

    if (edges[v].contains(0) && !edges_wanted[v].contains(0))
        logs.push_back((item){.op = op_remove, .a = 0, .b = v});
}


int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);

    cin >> n;

    edges = vector<unordered_set<int>> (n, unordered_set<int>());
    edges_wanted = vector<unordered_set<int>> (n, unordered_set<int>());

    cin >> m_s;
    for (int i = 0, a, b; i < m_s; i++) {
        cin >> a >> b;
        edges[a - 1].insert(b - 1);
        edges[b - 1].insert(a - 1);
    }

    cin >> m_d;
    for (int i = 0, a, b; i < m_d; i++) {
        cin >> a >> b;
        edges_wanted[a - 1].insert(b - 1);
        edges_wanted[b - 1].insert(a - 1);
    }

    vector<bool> seen(n, false);
    dfs_add(0, seen);

    for (auto e : logs) {
        edges[e.a].insert(e.b);
        edges[e.b].insert(e.a);
    }

    for (int i = 1; i < n; i++) {
        vector<int> edges_copy;

        for (int e : edges[i])
            edges_copy.push_back(e);

        for (int e : edges_copy) {
            if (e != 0 && !edges_wanted[i].contains(e)) {
                logs.push_back((item){.op = op_remove, .a = i, .b = e});
                edges[i].erase(e);
                edges[e].erase(i);
            }
        }
    }

    for (int i = 1; i < n; i++) {
        for (int e : edges_wanted[i]) {
            if (!edges[i].contains(e)) {
                logs.push_back((item){.op = op_add, .a = i, .b = e});
                edges[i].insert(e);
                edges[e].insert(i);
            }
        }
    }

    seen = vector<bool>(n, false);
    dfs_remove(0, seen);

    cout << logs.size() << "\n";
    for (auto e : logs) {
        cout << (e.op == op_add ? '+' : '-') << " " << e.a + 1 << " " << e.b + 1 << "\n";
    }
}