// Autor: Nicolas Wochnik #include <algorithm> #include <iostream> #include <string> #include <unordered_set> #include <vector> size_t nodes; std::vector<std::unordered_set<size_t>> start_graph; std::vector<std::unordered_set<size_t>> end_graph; void load_graphs() { std::cin >> nodes; start_graph.resize(nodes); end_graph.resize(nodes); size_t edges_count; std::cin >> edges_count; for (size_t i = 0; i < edges_count; ++i) { size_t a, b; std::cin >> a >> b; a--, b--; start_graph[a].insert(b); start_graph[b].insert(a); } std::cin >> edges_count; for (size_t i = 0; i < edges_count; ++i) { size_t a, b; std::cin >> a >> b; a--, b--; end_graph[a].insert(b); end_graph[b].insert(a); } } std::vector<std::string> commands; void execute_command(char cmd, size_t a, size_t b) { if ((cmd == '+' && start_graph[a].count(b)) || (cmd == '-' && !start_graph[a].count(b)) || a == b) { return; } char cmd_msg[20]; sprintf(cmd_msg, "%c %zu %zu", cmd, a + 1, b + 1); commands.push_back(cmd_msg); if (cmd == '+') { start_graph[a].insert(b); start_graph[b].insert(a); } else { start_graph[a].erase(b); start_graph[b].erase(a); } } std::vector<size_t> bfs_order; void bfs(const std::vector<std::unordered_set<size_t>> &graph) { bfs_order = {0}; size_t ptr = 0; std::vector<bool> visited(nodes, false); visited[0] = true; while (ptr != bfs_order.size()) { for (size_t n : graph[bfs_order[ptr]]) { if (visited[n]) { continue; } bfs_order.push_back(n); visited[n] = true; } ptr++; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); load_graphs(); bfs(start_graph); for (size_t i : bfs_order) { execute_command('+', 0, i); } for (size_t i = 1; i < nodes; ++i) { for (size_t j : end_graph[i]) { if (j <= i || start_graph[i].count(j)) { continue; } execute_command('+', i, j); } } for (size_t i = 1; i < nodes; ++i) { auto neightbours = start_graph[i]; for (size_t j : neightbours) { if (j <= i || end_graph[i].count(j)) { continue; } execute_command('-', i, j); } } bfs(end_graph); std::reverse(bfs_order.begin(), bfs_order.end()); for (size_t j : bfs_order) { if (end_graph[0].count(j)) { continue; } execute_command('-', 0, j); } std::cout << commands.size() << "\n"; for (const auto &command : commands) { std::cout << command << "\n"; } 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 | // Autor: Nicolas Wochnik #include <algorithm> #include <iostream> #include <string> #include <unordered_set> #include <vector> size_t nodes; std::vector<std::unordered_set<size_t>> start_graph; std::vector<std::unordered_set<size_t>> end_graph; void load_graphs() { std::cin >> nodes; start_graph.resize(nodes); end_graph.resize(nodes); size_t edges_count; std::cin >> edges_count; for (size_t i = 0; i < edges_count; ++i) { size_t a, b; std::cin >> a >> b; a--, b--; start_graph[a].insert(b); start_graph[b].insert(a); } std::cin >> edges_count; for (size_t i = 0; i < edges_count; ++i) { size_t a, b; std::cin >> a >> b; a--, b--; end_graph[a].insert(b); end_graph[b].insert(a); } } std::vector<std::string> commands; void execute_command(char cmd, size_t a, size_t b) { if ((cmd == '+' && start_graph[a].count(b)) || (cmd == '-' && !start_graph[a].count(b)) || a == b) { return; } char cmd_msg[20]; sprintf(cmd_msg, "%c %zu %zu", cmd, a + 1, b + 1); commands.push_back(cmd_msg); if (cmd == '+') { start_graph[a].insert(b); start_graph[b].insert(a); } else { start_graph[a].erase(b); start_graph[b].erase(a); } } std::vector<size_t> bfs_order; void bfs(const std::vector<std::unordered_set<size_t>> &graph) { bfs_order = {0}; size_t ptr = 0; std::vector<bool> visited(nodes, false); visited[0] = true; while (ptr != bfs_order.size()) { for (size_t n : graph[bfs_order[ptr]]) { if (visited[n]) { continue; } bfs_order.push_back(n); visited[n] = true; } ptr++; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); load_graphs(); bfs(start_graph); for (size_t i : bfs_order) { execute_command('+', 0, i); } for (size_t i = 1; i < nodes; ++i) { for (size_t j : end_graph[i]) { if (j <= i || start_graph[i].count(j)) { continue; } execute_command('+', i, j); } } for (size_t i = 1; i < nodes; ++i) { auto neightbours = start_graph[i]; for (size_t j : neightbours) { if (j <= i || end_graph[i].count(j)) { continue; } execute_command('-', i, j); } } bfs(end_graph); std::reverse(bfs_order.begin(), bfs_order.end()); for (size_t j : bfs_order) { if (end_graph[0].count(j)) { continue; } execute_command('-', 0, j); } std::cout << commands.size() << "\n"; for (const auto &command : commands) { std::cout << command << "\n"; } return 0; } |