#include <bits/stdc++.h> using namespace std; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() #define ssize(x) int(x.size()) #define pb push_back #define fi first #define se second #define ld long double typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair <ld, ld> pd; using LL=long long; #ifdef DEBUG auto &operator<<(auto &o, pair<auto, auto> p) { return o << "()" << p.first << ", " << p.second <<")"; } auto operator<<(auto &o, auto x)-> decltype(x.end(), o) { o << "{";int i = 0; for(auto e : x) o << ", "+!i++<<e; return o <<"}"; } #define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x) #else #define debug(...) {} #endif void solve() { int N; cin >> N; vector< vector<int> > G1(N + 1), G2(N + 1); vector< vector<int> > tree1(N + 1), tree2(N + 1); set<pii> edges1, edges2; auto get_graph = [&](vector< vector<int> > &G, set<pii> &edges) { int M; cin >> M; REP(i, M) { int a, b; cin >> a >> b; if (a > b) swap(a, b); edges.insert({a, b}); G[a].pb(b); G[b].pb(a); } return M; }; get_graph(G1, edges1); get_graph(G2, edges2); function<void(int, vector< vector<int> >&, vector< vector<int> >&, vector<bool>&)> dfs = [&](int v, vector< vector<int> > &G, vector< vector<int> > &tree, vector<bool> &visited) { visited[v] = true; for (auto u : G[v]) { if (!visited[u]) { tree[v].pb(u); dfs(u, G, tree, visited); } } }; auto get_rooted_tree = [&](int start, vector<vector<int>> &G) { vector<bool> visited(N + 1); vector<vector<int>> tree(N + 1); dfs(start, G, tree, visited); return tree; }; tree1 = get_rooted_tree(1, G1); tree2 = get_rooted_tree(1, G2); debug("TREES"); for (int i = 1; i <= N; i++) { debug(i); debug(tree1[i]); debug(tree2[i]); debug("================"); } vector<int> depths1(N + 1), depths2(N + 1); function<void(int, int, vector<int>&, vector< vector<int> >&)> find_depths = [&](int v, int p, vector<int> &depths, vector< vector<int> > &tree) { for (auto u : tree[v]) { if (u == p) continue; depths[u] = depths[v] + 1; find_depths(u, v, depths, tree); } }; find_depths(1, 1, depths1, tree1); find_depths(1, 1, depths2, tree2); vector< vector<int> > group_depths1(N + 1), group_depths2(N + 1); for (int i = 1; i <= N; i++) { int depth1 = depths1[i]; group_depths1[depth1].pb(i); int depth2 = depths2[i]; group_depths2[depth2].pb(i); } auto dbg_depths = [&](vector< vector<int> > group_depths) { for (int i = 0; i < N; i++) { if (group_depths[i].empty()) break; debug(i, group_depths[i]); } }; debug("GROUP DEPTHS 1"); dbg_depths(group_depths1); debug("GROUP DEPTHS 2"); dbg_depths(group_depths2); vector<pii> add_start, remove_end; auto not_connected = [&] (vector< vector<int> > group_depths, set<pii> edges) { vector<pii> to_add; for (int i = 1; i < N; i++) { for (auto v : group_depths[i]) { pii p = {1, v}; if (edges.find(p) == edges.end()) { to_add.pb(p); } } } return to_add; }; add_start = not_connected(group_depths1, edges1); remove_end = not_connected(group_depths2, edges2); // Wszystkie operacje mozemy zrobic bo mamy polaczenia {1, edge.fi} oraz {1, edge.se} // Dodajemy te krawedzie co nie ma w startowym i sa w docelowym. for (auto edge : edges2) { if (edge.fi == 1) continue; if (edges1.find(edge) == edges1.end()) add_start.pb(edge); } // Wyrzucamy te krawedzie co nie ma w docelowym i sa w startowym. for (auto edge : edges1) { if (edge.fi == 1) continue; if (edges2.find(edge) == edges2.end()) remove_end.pb(edge); } // Odwracamy, bo chcemy usuwac polaczenia z jedynka od najnizszej warstwy do najwyzszej // Mamy wtedy niezmiennik, ze nasz ojciec jest polaczony z jedynka. reverse(all(remove_end)); cout << add_start.size() + remove_end.size() << '\n'; for (auto edge : add_start) { cout << "+ " << edge.fi << ' ' << edge.se << '\n'; } for (auto edge : remove_end) { cout << "- " << edge.fi << ' ' << edge.se << '\n'; } } signed main() { cin.tie(0)->sync_with_stdio(0); int t = 1; // cin >> t; while(t--) { solve(); } }
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 | #include <bits/stdc++.h> using namespace std; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() #define ssize(x) int(x.size()) #define pb push_back #define fi first #define se second #define ld long double typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair <ld, ld> pd; using LL=long long; #ifdef DEBUG auto &operator<<(auto &o, pair<auto, auto> p) { return o << "()" << p.first << ", " << p.second <<")"; } auto operator<<(auto &o, auto x)-> decltype(x.end(), o) { o << "{";int i = 0; for(auto e : x) o << ", "+!i++<<e; return o <<"}"; } #define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x) #else #define debug(...) {} #endif void solve() { int N; cin >> N; vector< vector<int> > G1(N + 1), G2(N + 1); vector< vector<int> > tree1(N + 1), tree2(N + 1); set<pii> edges1, edges2; auto get_graph = [&](vector< vector<int> > &G, set<pii> &edges) { int M; cin >> M; REP(i, M) { int a, b; cin >> a >> b; if (a > b) swap(a, b); edges.insert({a, b}); G[a].pb(b); G[b].pb(a); } return M; }; get_graph(G1, edges1); get_graph(G2, edges2); function<void(int, vector< vector<int> >&, vector< vector<int> >&, vector<bool>&)> dfs = [&](int v, vector< vector<int> > &G, vector< vector<int> > &tree, vector<bool> &visited) { visited[v] = true; for (auto u : G[v]) { if (!visited[u]) { tree[v].pb(u); dfs(u, G, tree, visited); } } }; auto get_rooted_tree = [&](int start, vector<vector<int>> &G) { vector<bool> visited(N + 1); vector<vector<int>> tree(N + 1); dfs(start, G, tree, visited); return tree; }; tree1 = get_rooted_tree(1, G1); tree2 = get_rooted_tree(1, G2); debug("TREES"); for (int i = 1; i <= N; i++) { debug(i); debug(tree1[i]); debug(tree2[i]); debug("================"); } vector<int> depths1(N + 1), depths2(N + 1); function<void(int, int, vector<int>&, vector< vector<int> >&)> find_depths = [&](int v, int p, vector<int> &depths, vector< vector<int> > &tree) { for (auto u : tree[v]) { if (u == p) continue; depths[u] = depths[v] + 1; find_depths(u, v, depths, tree); } }; find_depths(1, 1, depths1, tree1); find_depths(1, 1, depths2, tree2); vector< vector<int> > group_depths1(N + 1), group_depths2(N + 1); for (int i = 1; i <= N; i++) { int depth1 = depths1[i]; group_depths1[depth1].pb(i); int depth2 = depths2[i]; group_depths2[depth2].pb(i); } auto dbg_depths = [&](vector< vector<int> > group_depths) { for (int i = 0; i < N; i++) { if (group_depths[i].empty()) break; debug(i, group_depths[i]); } }; debug("GROUP DEPTHS 1"); dbg_depths(group_depths1); debug("GROUP DEPTHS 2"); dbg_depths(group_depths2); vector<pii> add_start, remove_end; auto not_connected = [&] (vector< vector<int> > group_depths, set<pii> edges) { vector<pii> to_add; for (int i = 1; i < N; i++) { for (auto v : group_depths[i]) { pii p = {1, v}; if (edges.find(p) == edges.end()) { to_add.pb(p); } } } return to_add; }; add_start = not_connected(group_depths1, edges1); remove_end = not_connected(group_depths2, edges2); // Wszystkie operacje mozemy zrobic bo mamy polaczenia {1, edge.fi} oraz {1, edge.se} // Dodajemy te krawedzie co nie ma w startowym i sa w docelowym. for (auto edge : edges2) { if (edge.fi == 1) continue; if (edges1.find(edge) == edges1.end()) add_start.pb(edge); } // Wyrzucamy te krawedzie co nie ma w docelowym i sa w startowym. for (auto edge : edges1) { if (edge.fi == 1) continue; if (edges2.find(edge) == edges2.end()) remove_end.pb(edge); } // Odwracamy, bo chcemy usuwac polaczenia z jedynka od najnizszej warstwy do najwyzszej // Mamy wtedy niezmiennik, ze nasz ojciec jest polaczony z jedynka. reverse(all(remove_end)); cout << add_start.size() + remove_end.size() << '\n'; for (auto edge : add_start) { cout << "+ " << edge.fi << ' ' << edge.se << '\n'; } for (auto edge : remove_end) { cout << "- " << edge.fi << ' ' << edge.se << '\n'; } } signed main() { cin.tie(0)->sync_with_stdio(0); int t = 1; // cin >> t; while(t--) { solve(); } } |