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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <unordered_set>
#include <vector>

struct Graph {
    struct Operation {
        bool is_add;
        int from;
        int to;

        Operation(bool is_add, int from, int to) : is_add(is_add), from(from), to(to) {}
    };

    std::vector<std::unordered_set<int>> edges;
    std::vector<Operation> history;

    Graph(int n) : edges(n) {}

    void AddEdge(int a, int b, bool record = true, bool check = false) {
        /*if (check && (!edges[1].contains(a) || !edges[1].contains(b))) {
            std::cerr << "Invalid operation: " << a << " or " << b << " does not connect to 1" << std::endl;
        }
        if (edges[a].contains(b)) {
            std::cerr << "Edge from " << a << " to " << b << " already exists" << std::endl;
        }
        if (edges[b].contains(a)) {
            std::cerr << "Edge from " << b << " to " << a << " already exists" << std::endl;
        }*/

        if (record) {
            history.emplace_back(true, a, b);
        }

        edges[a].insert(b);
        edges[b].insert(a);
    }

    bool HasEdge(int a, int b) const {
        return edges[a].contains(b);
    }

    void RemoveEdge(int a, int b, bool record = true, bool check = false) {
        /*if (check && (!edges[1].contains(a) || !edges[1].contains(b))) {
            std::cerr << "Invalid operation: " << a << " or " << b << " does not connect to 1" << std::endl;
        }
        if (!edges[a].contains(b)) {
            std::cerr << "Edge from " << a << " to " << b << " does not exists" << std::endl;
        }
        if (!edges[b].contains(a)) {
            std::cerr << "Edge from " << b << " to " << a << " does not exists" << std::endl;
        }*/

        if (record) {
            history.emplace_back(false, a, b);
        }

        edges[a].erase(b);
        edges[b].erase(a);
    }

    bool Compare(const Graph& other) const {
        if (this->edges.size() != other.edges.size()) return false;
        for (int i = 0; i < this->edges.size(); ++i) {
            if (this->edges[i].size() != other.edges[i].size()) return false;
            for (int to : this->edges[i]) {
                if (!other.edges[i].contains(to)) return false;
            }
        }
        return true;
    }
};

std::ostream& operator<<(std::ostream& os, const Graph::Operation& op) {
    os << (op.is_add ? "+ " : "- ") << op.from << " " << op.to << std::endl;
    return os;
}

std::ostream& operator<<(std::ostream& os, const Graph& g) {
    os << g.history.size() << std::endl;
    for (const auto& op : g.history) {
        os << op;
    }
    return os;
}

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

    int n;
    std::cin >> n;
    Graph base(n+1), target(n + 1), curr(n + 1);

    int k;
    std::cin >> k;
    while (k--) {
        int a, b;
        std::cin >> a >> b;
        base.AddEdge(a, b);
        curr.AddEdge(a, b, false, false);
    }

    std::cin >> k;
    while (k--) {
        int a, b;
        std::cin >> a >> b;
        target.AddEdge(a, b);
    }

    std::vector<bool> visited(n+1, false);
    visited[1] = true;
    std::vector<int> travel;
    travel.push_back(1);
    while (!travel.empty()) {
        int from = *travel.rbegin();
        travel.pop_back();
        for (int to : base.edges[from]) {
            if (!visited[to]) {
                visited[to] = true;
                travel.push_back(to);
                if (!curr.HasEdge(1, to)) {
                    curr.AddEdge(1, to, true, false);
                }
            }
        }
    }

    for (int i = 1; i <= n; ++i) {
        const auto& target_edges = target.edges[i];
        const auto& base_edges = base.edges[i];
        for (int to : target_edges) {
            if (!curr.HasEdge(i, to)) {
                curr.AddEdge(i, to, true, true);
            }
        }
        for (int to : base_edges) {
            if (i == 1 || to == 1) continue;
            if (!target.HasEdge(i, to) && curr.HasEdge(i, to)) {
                curr.RemoveEdge(i, to, true, true);
            }
        }
    }

    visited.clear();
    visited.resize(n + 1, false);
    visited[1] = true;
    int t_ind = 0;
    travel.push_back(1);

    while (t_ind < travel.size()) {
        int from = travel[t_ind++];
        for (int to : target.edges[from]) {
            if (!visited[to]) {
                visited[to] = true;
                travel.push_back(to);
            }
        }
    }

    for (int i = travel.size() - 1; i > 0; --i) {
        if (!target.HasEdge(1, travel[i])) {
            curr.RemoveEdge(1, travel[i], true, false);
        }
    }

    /*if (!curr.Compare(target)) {
        std::cerr << "Target graph is not the current graph. Investigate." << std::endl;
    }*/

    std::cout << curr;

    return 0;
}