#include <ranges> #include <vector> #include <iostream> #include <numeric> #include <algorithm> #include <unordered_map> #include <set> #include <stack> using namespace std; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; int no_atom_edges; cin >> no_atom_edges; set<pair<int, int>> atom_edges; vector<vector<int>> atom(n + 1); vector<vector<int>> target(n + 1); int a, b; for (int i = 0; i < no_atom_edges; i++) { cin >> a >> b; atom_edges.insert(make_pair(min(a, b), max(a, b))); atom[a].push_back(b); atom[b].push_back(a); } int no_target_edges; cin >> no_target_edges; set<pair<int, int>> target_edges; for (int i = 0; i < no_target_edges; i++) { cin >> a >> b; target_edges.insert(make_pair(min(a, b), max(a, b))); target[a].push_back(b); target[b].push_back(a); } vector<pair<int, int>> add_instructions; vector<pair<int, int>> remove_instructions; set<int> seen; stack<int> stack; // run DFS for atom from vertex 1 stack.push(1); seen.insert(1); vector<int> atom_dfs_order; while (!stack.empty()) { auto curr = stack.top(); stack.pop(); atom_dfs_order.push_back(curr); for (const auto &nei: atom[curr]) { if (seen.find(nei) == seen.end()) { stack.push(nei); seen.insert(nei); } } } // add edge 1--x for all vertices x in G in DFS order for (const auto &item: atom_dfs_order) { auto edge = make_pair(1, item); if (atom_edges.find(edge) == atom_edges.end() && edge.second != 1) { add_instructions.push_back(edge); } } // add missing (present in target, missing in atom) for (const auto &target_edge: target_edges) { if (atom_edges.find(target_edge) == atom_edges.end() && target_edge.first != 1) { add_instructions.push_back(target_edge); } } // remove missing (present in atom, missing in target) for (const auto &atom_edge: atom_edges) { if (target_edges.find(atom_edge) == target_edges.end() && atom_edge.first != 1) { remove_instructions.push_back(atom_edge); } } // run DFS for target from vertex 1 seen.clear(); stack.push(1); seen.insert(1); vector<int> target_dfs_order; while (!stack.empty()) { auto curr = stack.top(); stack.pop(); target_dfs_order.push_back(curr); for (const auto &nei: target[curr]) { if (seen.find(nei) == seen.end()) { stack.push(nei); seen.insert(nei); } } } // remove edge 1--x for all x in G in reverse DFS order (if not present in target) for (int &it: std::ranges::reverse_view(target_dfs_order)) { auto edge = make_pair(1, it); if (target_edges.find(edge) == target_edges.end() && edge.second != 1) { remove_instructions.push_back(edge); } } cout << add_instructions.size() + remove_instructions.size() << endl; for (const auto &instruction: add_instructions) { cout << "+ " << instruction.first << " " << instruction.second << endl; } for (const auto &instruction: remove_instructions) { cout << "- " << instruction.first << " " << instruction.second << 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 | #include <ranges> #include <vector> #include <iostream> #include <numeric> #include <algorithm> #include <unordered_map> #include <set> #include <stack> using namespace std; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; int no_atom_edges; cin >> no_atom_edges; set<pair<int, int>> atom_edges; vector<vector<int>> atom(n + 1); vector<vector<int>> target(n + 1); int a, b; for (int i = 0; i < no_atom_edges; i++) { cin >> a >> b; atom_edges.insert(make_pair(min(a, b), max(a, b))); atom[a].push_back(b); atom[b].push_back(a); } int no_target_edges; cin >> no_target_edges; set<pair<int, int>> target_edges; for (int i = 0; i < no_target_edges; i++) { cin >> a >> b; target_edges.insert(make_pair(min(a, b), max(a, b))); target[a].push_back(b); target[b].push_back(a); } vector<pair<int, int>> add_instructions; vector<pair<int, int>> remove_instructions; set<int> seen; stack<int> stack; // run DFS for atom from vertex 1 stack.push(1); seen.insert(1); vector<int> atom_dfs_order; while (!stack.empty()) { auto curr = stack.top(); stack.pop(); atom_dfs_order.push_back(curr); for (const auto &nei: atom[curr]) { if (seen.find(nei) == seen.end()) { stack.push(nei); seen.insert(nei); } } } // add edge 1--x for all vertices x in G in DFS order for (const auto &item: atom_dfs_order) { auto edge = make_pair(1, item); if (atom_edges.find(edge) == atom_edges.end() && edge.second != 1) { add_instructions.push_back(edge); } } // add missing (present in target, missing in atom) for (const auto &target_edge: target_edges) { if (atom_edges.find(target_edge) == atom_edges.end() && target_edge.first != 1) { add_instructions.push_back(target_edge); } } // remove missing (present in atom, missing in target) for (const auto &atom_edge: atom_edges) { if (target_edges.find(atom_edge) == target_edges.end() && atom_edge.first != 1) { remove_instructions.push_back(atom_edge); } } // run DFS for target from vertex 1 seen.clear(); stack.push(1); seen.insert(1); vector<int> target_dfs_order; while (!stack.empty()) { auto curr = stack.top(); stack.pop(); target_dfs_order.push_back(curr); for (const auto &nei: target[curr]) { if (seen.find(nei) == seen.end()) { stack.push(nei); seen.insert(nei); } } } // remove edge 1--x for all x in G in reverse DFS order (if not present in target) for (int &it: std::ranges::reverse_view(target_dfs_order)) { auto edge = make_pair(1, it); if (target_edges.find(edge) == target_edges.end() && edge.second != 1) { remove_instructions.push_back(edge); } } cout << add_instructions.size() + remove_instructions.size() << endl; for (const auto &instruction: add_instructions) { cout << "+ " << instruction.first << " " << instruction.second << endl; } for (const auto &instruction: remove_instructions) { cout << "- " << instruction.first << " " << instruction.second << endl; } return 0; } |