#include <bits/stdc++.h> constexpr int MAX_NODE = 30'004; struct pair_hash { inline std::size_t operator()(const std::pair<int, int> & v) const { return v.first * MAX_NODE + v.second; } }; struct Graph { int V; std::unordered_map<int, std::unordered_set<int>> adj; std::unordered_set<std::pair<int, int>, pair_hash> edgeList; Graph(int V) { this->V = V; for (int i = 1; i <= V; ++i) { this->adj[i] = std::unordered_set<int>(); } this->edgeList = std::unordered_set<std::pair<int, int>, pair_hash>(); } bool isConnected() { std::vector<bool> visited(V, false); selfDFS(1, visited); for (int i = 1; i <= V; ++i) { if (!visited[i]) { return false; } } return true; } void selfDFS(int node, std::vector<bool> & visited) { visited[node] = true; for (auto & x : adj[node]) { if (!visited[x]) { selfDFS(x, visited); } } } int getDegree(int v) { return adj[v].size(); } void addEdge(int v, int w) { adj[v].insert(w); adj[w].insert(v); if (v < w) { edgeList.insert({v, w}); } else { edgeList.insert({w, v}); } } void removeEdge(int v, int w) { adj[v].erase(w); adj[w].erase(v); if (v < w) { edgeList.erase({v, w}); } else { edgeList.erase({w, v}); } } bool edgeExists(int v, int w) { return adj[v].find(w) != adj[v].end(); } bool is_dist_2(int v, int w) { if (v == w) { return false; } if (adj[v].find(w) != adj[v].end()) { return false; } for (auto & x : adj[v]) { if (adj[x].find(w) != adj[x].end()) { return true; } } return false; } }; int n; int m1, m2; Graph g_start(MAX_NODE); Graph g_end(MAX_NODE); std::unordered_set<std::pair<int, int>, pair_hash> edgeListAdd; std::unordered_set<std::pair<int, int>, pair_hash> edgeListRemove; std::unordered_set<std::pair<int, int>, pair_hash> edgeListHelp; std::vector<std::string> output; void insertQuery(std::string op, int u, int v) { std::string tmp = op + " " + std::to_string(u) + " " + std::to_string(v); // std::cout << tmp << std::endl; output.push_back(tmp); } std::vector<int> remeber; bool visited[MAX_NODE]; void DFS(int node) { visited[node] = true; if (node != 1 && !g_start.edgeExists(1, node)) { // std::cout << "INSERT: " << node << '\n'; insertQuery("+", 1, node); remeber.push_back(node); } for (auto & x : g_start.adj[node]) { // std::cout << "CHECK: " << node << " " << x << '\n'; if (!visited[x]) { // std::cout << "DFS: " << node << " " << x << '\n'; DFS(x); } } } void DFS_EXIT(int node) { visited[node] = true; // std::cout << "DFS_EXIT: " << node << '\n'; for (auto & x : g_end.adj[node]) { if (!visited[x]) { DFS_EXIT(x); } } if (node != 1 && g_start.edgeExists(1, node) && !g_end.edgeExists(1, node)) { remeber.push_back(node); } } bool find_cycle(int node, int parent, std::vector<int> & path) { // insertQuery("FIND_CYCLE", node, parent); visited[node] = true; path.push_back(node); bool output = false; for (auto & x: g_start.adj[node]) { if (x == parent && path.size() == 2) { continue; } else if (x == 1) { output = true; break; } else if (!visited[x]) { output |= find_cycle(x, node, path); if (output) { break; } } } if (!output) { path.pop_back(); } return output; } void remove_cycle(std::vector<int> & cycle) { // for (auto & x : cycle) { // std::cout << x << " "; // } // std::cout << '\n'; for (int i = cycle.size() - 2; i > 1; --i) { if (g_start.adj[1].find(cycle[i]) == g_start.adj[1].end()) { insertQuery("+", 1, cycle[i]); g_start.addEdge(1, cycle[i]); } } // for (int i = 1; i <= n; ++i) { // std::cout << i << ": "; // for (auto & x : g_start.adj[i]) { // std::cout << x << " "; // } // std::cout << '\n'; // } // std::cout << "\n\n"; insertQuery("-", cycle[0], cycle[1]); g_start.removeEdge(cycle[0], cycle[1]); for (int i = 2; i < cycle.size() - 1; ++i) { if (g_end.adj[1].find(cycle[i]) == g_end.adj[1].end()) { insertQuery("-", 1, cycle[i]); g_start.removeEdge(1, cycle[i]); } } // std::cout << "REMOVE CYCLE END\n"; // for (int i = 1; i <= n; ++i) { // std::cout << i << ": "; // for (auto & x : g_start.adj[i]) { // std::cout << x << " "; // } // std::cout << '\n'; // } // std::cout << "\n\n"; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); std::cin >> n; std::cin >> m1; for (int i = 0; i < m1; ++i) { int x, y; std::cin >> x >> y; g_start.addEdge(x, y); } std::cin >> m2; for (int i = 0; i < m2; ++i) { int x, y; std::cin >> x >> y; g_end.addEdge(x, y); } DFS(1); for (auto & x : remeber) { g_start.addEdge(1, x); } std::fill(visited, visited + n, false); // std::cout << "END DFS\n"; // std::cout << g_start.edgeExists(1, 3); for (auto & [u, v] : g_end.edgeList) { if (!g_start.edgeExists(u, v)) { edgeListAdd.insert({u, v}); } } for (auto & [u, v] : g_start.edgeList) { if (!g_end.edgeExists(u, v)) { edgeListRemove.insert({u, v}); } } for (auto & [u, v] : edgeListAdd) { insertQuery("+", u, v); g_start.addEdge(u, v); } edgeListAdd.clear(); for (auto & [u, v] : edgeListRemove) { if (u != 1 && v != 1) { insertQuery("-", u, v); g_start.removeEdge(u, v); } } remeber.clear(); std::fill(visited, visited + n + 1, false); DFS_EXIT(1); for (auto & x : remeber) { insertQuery("-", 1, x); } std::cout << output.size() << std::endl; for (int i = 0; i < output.size(); ++i) { std::cout << output[i]; if (i != output.size() - 1) { std::cout << std::endl; } } 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | #include <bits/stdc++.h> constexpr int MAX_NODE = 30'004; struct pair_hash { inline std::size_t operator()(const std::pair<int, int> & v) const { return v.first * MAX_NODE + v.second; } }; struct Graph { int V; std::unordered_map<int, std::unordered_set<int>> adj; std::unordered_set<std::pair<int, int>, pair_hash> edgeList; Graph(int V) { this->V = V; for (int i = 1; i <= V; ++i) { this->adj[i] = std::unordered_set<int>(); } this->edgeList = std::unordered_set<std::pair<int, int>, pair_hash>(); } bool isConnected() { std::vector<bool> visited(V, false); selfDFS(1, visited); for (int i = 1; i <= V; ++i) { if (!visited[i]) { return false; } } return true; } void selfDFS(int node, std::vector<bool> & visited) { visited[node] = true; for (auto & x : adj[node]) { if (!visited[x]) { selfDFS(x, visited); } } } int getDegree(int v) { return adj[v].size(); } void addEdge(int v, int w) { adj[v].insert(w); adj[w].insert(v); if (v < w) { edgeList.insert({v, w}); } else { edgeList.insert({w, v}); } } void removeEdge(int v, int w) { adj[v].erase(w); adj[w].erase(v); if (v < w) { edgeList.erase({v, w}); } else { edgeList.erase({w, v}); } } bool edgeExists(int v, int w) { return adj[v].find(w) != adj[v].end(); } bool is_dist_2(int v, int w) { if (v == w) { return false; } if (adj[v].find(w) != adj[v].end()) { return false; } for (auto & x : adj[v]) { if (adj[x].find(w) != adj[x].end()) { return true; } } return false; } }; int n; int m1, m2; Graph g_start(MAX_NODE); Graph g_end(MAX_NODE); std::unordered_set<std::pair<int, int>, pair_hash> edgeListAdd; std::unordered_set<std::pair<int, int>, pair_hash> edgeListRemove; std::unordered_set<std::pair<int, int>, pair_hash> edgeListHelp; std::vector<std::string> output; void insertQuery(std::string op, int u, int v) { std::string tmp = op + " " + std::to_string(u) + " " + std::to_string(v); // std::cout << tmp << std::endl; output.push_back(tmp); } std::vector<int> remeber; bool visited[MAX_NODE]; void DFS(int node) { visited[node] = true; if (node != 1 && !g_start.edgeExists(1, node)) { // std::cout << "INSERT: " << node << '\n'; insertQuery("+", 1, node); remeber.push_back(node); } for (auto & x : g_start.adj[node]) { // std::cout << "CHECK: " << node << " " << x << '\n'; if (!visited[x]) { // std::cout << "DFS: " << node << " " << x << '\n'; DFS(x); } } } void DFS_EXIT(int node) { visited[node] = true; // std::cout << "DFS_EXIT: " << node << '\n'; for (auto & x : g_end.adj[node]) { if (!visited[x]) { DFS_EXIT(x); } } if (node != 1 && g_start.edgeExists(1, node) && !g_end.edgeExists(1, node)) { remeber.push_back(node); } } bool find_cycle(int node, int parent, std::vector<int> & path) { // insertQuery("FIND_CYCLE", node, parent); visited[node] = true; path.push_back(node); bool output = false; for (auto & x: g_start.adj[node]) { if (x == parent && path.size() == 2) { continue; } else if (x == 1) { output = true; break; } else if (!visited[x]) { output |= find_cycle(x, node, path); if (output) { break; } } } if (!output) { path.pop_back(); } return output; } void remove_cycle(std::vector<int> & cycle) { // for (auto & x : cycle) { // std::cout << x << " "; // } // std::cout << '\n'; for (int i = cycle.size() - 2; i > 1; --i) { if (g_start.adj[1].find(cycle[i]) == g_start.adj[1].end()) { insertQuery("+", 1, cycle[i]); g_start.addEdge(1, cycle[i]); } } // for (int i = 1; i <= n; ++i) { // std::cout << i << ": "; // for (auto & x : g_start.adj[i]) { // std::cout << x << " "; // } // std::cout << '\n'; // } // std::cout << "\n\n"; insertQuery("-", cycle[0], cycle[1]); g_start.removeEdge(cycle[0], cycle[1]); for (int i = 2; i < cycle.size() - 1; ++i) { if (g_end.adj[1].find(cycle[i]) == g_end.adj[1].end()) { insertQuery("-", 1, cycle[i]); g_start.removeEdge(1, cycle[i]); } } // std::cout << "REMOVE CYCLE END\n"; // for (int i = 1; i <= n; ++i) { // std::cout << i << ": "; // for (auto & x : g_start.adj[i]) { // std::cout << x << " "; // } // std::cout << '\n'; // } // std::cout << "\n\n"; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); std::cin >> n; std::cin >> m1; for (int i = 0; i < m1; ++i) { int x, y; std::cin >> x >> y; g_start.addEdge(x, y); } std::cin >> m2; for (int i = 0; i < m2; ++i) { int x, y; std::cin >> x >> y; g_end.addEdge(x, y); } DFS(1); for (auto & x : remeber) { g_start.addEdge(1, x); } std::fill(visited, visited + n, false); // std::cout << "END DFS\n"; // std::cout << g_start.edgeExists(1, 3); for (auto & [u, v] : g_end.edgeList) { if (!g_start.edgeExists(u, v)) { edgeListAdd.insert({u, v}); } } for (auto & [u, v] : g_start.edgeList) { if (!g_end.edgeExists(u, v)) { edgeListRemove.insert({u, v}); } } for (auto & [u, v] : edgeListAdd) { insertQuery("+", u, v); g_start.addEdge(u, v); } edgeListAdd.clear(); for (auto & [u, v] : edgeListRemove) { if (u != 1 && v != 1) { insertQuery("-", u, v); g_start.removeEdge(u, v); } } remeber.clear(); std::fill(visited, visited + n + 1, false); DFS_EXIT(1); for (auto & x : remeber) { insertQuery("-", 1, x); } std::cout << output.size() << std::endl; for (int i = 0; i < output.size(); ++i) { std::cout << output[i]; if (i != output.size() - 1) { std::cout << std::endl; } } return 0; } |