#include <cstdio> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> template <typename T> T min_of(T a, T b) { return a < b ? a : b; } template <typename T> T max_of(T a, T b) { return a > b ? a : b; } const char PLUS = '+'; const char MINUS = '-'; typedef std::pair<int, int> _edge; struct Op { char op; _edge e; Op(char o, _edge _e) : op(o), e(_e) {} }; struct Graph { std::vector<std::set<int>> nodes; std::set<_edge> edges; std::set<_edge> addons; std::set<_edge> rst; std::unordered_map<int, int> prevs; std::unordered_set<int> visited; std::map<_edge, int> removes; inline std::pair<int, int> mk_edge(int a, int b) { return std::make_pair(min_of(a, b), max_of(a, b)); } void save_edge(_edge &edge) { edges.insert(edge); nodes[edge.first].insert(edge.second); nodes[edge.second].insert(edge.first); } void forget_edge(_edge &ee) { edges.erase(ee); nodes[ee.first].erase(ee.second); nodes[ee.second].erase(ee.first); } void mutate(Graph &tgt, std::vector<Op> &resp) { std::vector<Op> uncompressed; for (auto &t_edge : tgt.edges) { if (!edges.contains(t_edge)) { add_edge(t_edge, uncompressed); } } std::vector<std::pair<int, int>> to_remove; for (auto &edge : edges) { if (!tgt.edges.contains(edge)) { to_remove.push_back(edge); } } for (auto &edge : to_remove) { remove_edge(edge, uncompressed); } compress(uncompressed, resp); } void lossless(std::vector<Op> &uncompressed, std::vector<Op> &resp) { for (Op &op : uncompressed) { resp.push_back(op); } } void compress(std::vector<Op> &uncompressed, std::vector<Op> &resp) { for (Op op : uncompressed) { if (op.op == PLUS) { if (!addons.contains(op.e) && !rst.contains(op.e)) { resp.push_back(op); addons.insert(op.e); } } else { if (removes[op.e] == 1) { resp.push_back(op); } else { removes[op.e] -= 1; if (!addons.contains(op.e)) { rst.insert(op.e); } } } } } void _bsf(int start, int end) { std::queue<int> next; prevs.clear(); visited.clear(); next.push(start); visited.insert(start); while (!next.empty()) { int current = next.front(); next.pop(); for (int tg : nodes[current]) { if (visited.contains(tg)) { continue; } prevs[tg] = current; if (tg == end) { return; } next.push(tg); visited.insert(tg); } } } void r_increase(_edge &edge) { if (removes.contains(edge)) { removes[edge] += 1; } else { removes[edge] = 1; } } void save_ops(_edge edge, char action, std::vector<Op> &resp) { _bsf(edge.first, edge.second); std::vector<int> path; for (int current = prevs[edge.second]; current != edge.first; current = prevs[current]) { path.push_back(current); } int focal = edge.second; auto it = path.begin(); for (++it; it != path.end(); ++it) { _edge e = mk_edge(focal, *it); resp.push_back(Op(PLUS, e)); if (action == PLUS) { save_edge(e); } } resp.push_back(Op(action, edge)); if (action == MINUS) { r_increase(edge); for (--it; it != path.begin(); --it) { _edge e = mk_edge(focal, *it); r_increase(e); resp.push_back(Op(MINUS, e)); forget_edge(e); } } } void add_edge(std::pair<int, int> edge, std::vector<Op> &resp) { save_ops(edge, PLUS, resp); save_edge(edge); } void remove_edge(std::pair<int, int> edge, std::vector<Op> &resp) { forget_edge(edge); save_ops(edge, MINUS, resp); } void load(int N) { for (int n = 0; n < N; ++n) { nodes.push_back(std::set<int>()); } int E, a, b; scanf("%d", &E); for (int e = 0; e < E; ++e) { scanf("%d%d", &a, &b); --a, --b; nodes[a].insert(b); nodes[b].insert(a); edges.insert(std::make_pair(min_of(a, b), max_of(a, b))); } } void print() { int id = 1; for (auto edges : nodes) { printf("%3d: ", id); ++id; for (int e : edges) { printf("%d ", e + 1); } puts(""); } puts(""); } }; int main() { int n; scanf("%d", &n); Graph a, b; a.load(n); b.load(n); std::vector<Op> ops; a.mutate(b, ops); printf("%d\n", (int)ops.size()); for (Op &op : ops) { printf("%c %d %d\n", op.op, op.e.first + 1, op.e.second + 1); } return 0; }
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | #include <cstdio> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> template <typename T> T min_of(T a, T b) { return a < b ? a : b; } template <typename T> T max_of(T a, T b) { return a > b ? a : b; } const char PLUS = '+'; const char MINUS = '-'; typedef std::pair<int, int> _edge; struct Op { char op; _edge e; Op(char o, _edge _e) : op(o), e(_e) {} }; struct Graph { std::vector<std::set<int>> nodes; std::set<_edge> edges; std::set<_edge> addons; std::set<_edge> rst; std::unordered_map<int, int> prevs; std::unordered_set<int> visited; std::map<_edge, int> removes; inline std::pair<int, int> mk_edge(int a, int b) { return std::make_pair(min_of(a, b), max_of(a, b)); } void save_edge(_edge &edge) { edges.insert(edge); nodes[edge.first].insert(edge.second); nodes[edge.second].insert(edge.first); } void forget_edge(_edge &ee) { edges.erase(ee); nodes[ee.first].erase(ee.second); nodes[ee.second].erase(ee.first); } void mutate(Graph &tgt, std::vector<Op> &resp) { std::vector<Op> uncompressed; for (auto &t_edge : tgt.edges) { if (!edges.contains(t_edge)) { add_edge(t_edge, uncompressed); } } std::vector<std::pair<int, int>> to_remove; for (auto &edge : edges) { if (!tgt.edges.contains(edge)) { to_remove.push_back(edge); } } for (auto &edge : to_remove) { remove_edge(edge, uncompressed); } compress(uncompressed, resp); } void lossless(std::vector<Op> &uncompressed, std::vector<Op> &resp) { for (Op &op : uncompressed) { resp.push_back(op); } } void compress(std::vector<Op> &uncompressed, std::vector<Op> &resp) { for (Op op : uncompressed) { if (op.op == PLUS) { if (!addons.contains(op.e) && !rst.contains(op.e)) { resp.push_back(op); addons.insert(op.e); } } else { if (removes[op.e] == 1) { resp.push_back(op); } else { removes[op.e] -= 1; if (!addons.contains(op.e)) { rst.insert(op.e); } } } } } void _bsf(int start, int end) { std::queue<int> next; prevs.clear(); visited.clear(); next.push(start); visited.insert(start); while (!next.empty()) { int current = next.front(); next.pop(); for (int tg : nodes[current]) { if (visited.contains(tg)) { continue; } prevs[tg] = current; if (tg == end) { return; } next.push(tg); visited.insert(tg); } } } void r_increase(_edge &edge) { if (removes.contains(edge)) { removes[edge] += 1; } else { removes[edge] = 1; } } void save_ops(_edge edge, char action, std::vector<Op> &resp) { _bsf(edge.first, edge.second); std::vector<int> path; for (int current = prevs[edge.second]; current != edge.first; current = prevs[current]) { path.push_back(current); } int focal = edge.second; auto it = path.begin(); for (++it; it != path.end(); ++it) { _edge e = mk_edge(focal, *it); resp.push_back(Op(PLUS, e)); if (action == PLUS) { save_edge(e); } } resp.push_back(Op(action, edge)); if (action == MINUS) { r_increase(edge); for (--it; it != path.begin(); --it) { _edge e = mk_edge(focal, *it); r_increase(e); resp.push_back(Op(MINUS, e)); forget_edge(e); } } } void add_edge(std::pair<int, int> edge, std::vector<Op> &resp) { save_ops(edge, PLUS, resp); save_edge(edge); } void remove_edge(std::pair<int, int> edge, std::vector<Op> &resp) { forget_edge(edge); save_ops(edge, MINUS, resp); } void load(int N) { for (int n = 0; n < N; ++n) { nodes.push_back(std::set<int>()); } int E, a, b; scanf("%d", &E); for (int e = 0; e < E; ++e) { scanf("%d%d", &a, &b); --a, --b; nodes[a].insert(b); nodes[b].insert(a); edges.insert(std::make_pair(min_of(a, b), max_of(a, b))); } } void print() { int id = 1; for (auto edges : nodes) { printf("%3d: ", id); ++id; for (int e : edges) { printf("%d ", e + 1); } puts(""); } puts(""); } }; int main() { int n; scanf("%d", &n); Graph a, b; a.load(n); b.load(n); std::vector<Op> ops; a.mutate(b, ops); printf("%d\n", (int)ops.size()); for (Op &op : ops) { printf("%c %d %d\n", op.op, op.e.first + 1, op.e.second + 1); } return 0; } |