#include <bits/stdc++.h> using namespace std; int n; using Edge = pair<int, int>; vector<Edge> edges_in, edges_out; using Tree = vector<vector<int>>; void Add(bool t, int a, int b); struct TreeMaker { Tree tree; vector<int> visited; TreeMaker(const vector<Edge>& edges) { tree.resize(n); for (auto [i, j] : edges) { tree[i].push_back(j); tree[j].push_back(i); } visited.resize(n, 0); DFS(0); } void DFS(int a) { assert(!visited[a]); visited[a] = true; vector<int> children; for (int i : tree[a]) if (!visited[i]) { children.push_back(i); DFS(i); } tree[a] = move(children); } }; template <bool t> struct Adder { const Tree& tree; vector<int> root_nb; Adder(const Tree& tree, const vector<Edge>& edges) : tree(tree) { root_nb.resize(n, 0); for (auto [i, j] : edges) if (i == 0) root_nb[j] = 1; DFS(0); } void DFS(int a) { for (int i : tree[a]) { if (t && !root_nb[i]) Add(t, 0, i); DFS(i); if (!t && !root_nb[i]) Add(t, 0, i); } } }; vector<Edge> Diff(const vector<Edge>& a, const vector<Edge>& b) { vector<Edge> c; ranges::set_difference(a, b, back_inserter(c)); return c; } Tree Make(const vector<Edge>& edges) { return TreeMaker(edges).tree; } void Solve() { Adder<true> add_to_root(Make(edges_in), edges_in); for (auto [i, j] : Diff(edges_out, edges_in)) if (i != 0) Add(true, i, j); for (auto [i, j] : Diff(edges_in, edges_out)) if (i != 0) Add(false, i, j); Adder<false> del_from_root(Make(edges_out), edges_out); } vector<tuple<bool, int, int>> opers; void Add(bool t, int a, int b) { opers.emplace_back(t, a, b); } void Read(vector<Edge>& edges) { int m; scanf("%d", &m); for (; m; m--) { int a, b; scanf("%d%d", &a, &b); a--; b--; if (a > b) swap(a, b); edges.push_back({a, b}); } ranges::sort(edges); } void Read() { scanf("%d", &n); Read(edges_in); Read(edges_out); } void Write() { printf("%lu\n" , opers.size()); for (auto [t, a, b] : opers) printf("%c %d %d\n", (t ? '+' : '-'), a + 1, b + 1); } int main() { Read(); Solve(); Write(); 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 | #include <bits/stdc++.h> using namespace std; int n; using Edge = pair<int, int>; vector<Edge> edges_in, edges_out; using Tree = vector<vector<int>>; void Add(bool t, int a, int b); struct TreeMaker { Tree tree; vector<int> visited; TreeMaker(const vector<Edge>& edges) { tree.resize(n); for (auto [i, j] : edges) { tree[i].push_back(j); tree[j].push_back(i); } visited.resize(n, 0); DFS(0); } void DFS(int a) { assert(!visited[a]); visited[a] = true; vector<int> children; for (int i : tree[a]) if (!visited[i]) { children.push_back(i); DFS(i); } tree[a] = move(children); } }; template <bool t> struct Adder { const Tree& tree; vector<int> root_nb; Adder(const Tree& tree, const vector<Edge>& edges) : tree(tree) { root_nb.resize(n, 0); for (auto [i, j] : edges) if (i == 0) root_nb[j] = 1; DFS(0); } void DFS(int a) { for (int i : tree[a]) { if (t && !root_nb[i]) Add(t, 0, i); DFS(i); if (!t && !root_nb[i]) Add(t, 0, i); } } }; vector<Edge> Diff(const vector<Edge>& a, const vector<Edge>& b) { vector<Edge> c; ranges::set_difference(a, b, back_inserter(c)); return c; } Tree Make(const vector<Edge>& edges) { return TreeMaker(edges).tree; } void Solve() { Adder<true> add_to_root(Make(edges_in), edges_in); for (auto [i, j] : Diff(edges_out, edges_in)) if (i != 0) Add(true, i, j); for (auto [i, j] : Diff(edges_in, edges_out)) if (i != 0) Add(false, i, j); Adder<false> del_from_root(Make(edges_out), edges_out); } vector<tuple<bool, int, int>> opers; void Add(bool t, int a, int b) { opers.emplace_back(t, a, b); } void Read(vector<Edge>& edges) { int m; scanf("%d", &m); for (; m; m--) { int a, b; scanf("%d%d", &a, &b); a--; b--; if (a > b) swap(a, b); edges.push_back({a, b}); } ranges::sort(edges); } void Read() { scanf("%d", &n); Read(edges_in); Read(edges_out); } void Write() { printf("%lu\n" , opers.size()); for (auto [t, a, b] : opers) printf("%c %d %d\n", (t ? '+' : '-'), a + 1, b + 1); } int main() { Read(); Solve(); Write(); return 0; } |