// PA2024 runda 2B - https://sio2.mimuw.edu.pl/c/pa-2024-1/p/alc/ //-std=c++20 #include<iostream> #include <cstddef> #include <algorithm> #include<vector> #include<list> #include<set> using I = u_int64_t; struct Alc { I atoms; // <=30K std::vector<std::set<I>> source; std::vector<std::set<I>> target; std::list<std::string> result; std::vector<I> visits; I counter = 0; void input_graph(std::vector<std::set<I>> &graph) { graph.resize(atoms, std::set<I>()); visits.resize(atoms, 0); I edges; // <=50k std::cin >> edges; for (I i(0); i < edges; i++) { I a, b; std::cin >> a >> b; if (a > b) { std::swap(a, b); } a--; b--; // user indeksuje po liczbach naturalnych graph[a].insert(b); graph[b].insert(a); } } bool is_k2(I i, I m) { for (const auto &k1: source[i]) { for (const auto &k2: source[k1]) { if (k2 == m) { return true; } } } return false; } void print_add(I i, I m) { result.push_back("+ " + std::to_string(i + 1) + " " + std::to_string(m + 1)); } void print_delete(I i, I m) { result.push_back("- " + std::to_string(i + 1) + " " + std::to_string(m + 1)); } void run() { std::cin >> atoms; input_graph(source); input_graph(target); add_missing(); remove_bad(); } void remove_bad() { for (I i(0); i < atoms; i++) { std::list<I> bad; for (auto it = source[i].begin(); it != source[i].end(); it++) { if (!target[i].contains(*it)) { bad.push_back(*it); } } while (!bad.empty()) { auto b = bad.front(); source[i].erase(b); source[b].erase(i); counter++; std::vector<I> path; visits[i] = counter; path.push_back(i); find_path(path, b); I size = path.size(); for (I j(2); j < size - 1; j++) { I t = path[j]; if (!source[i].contains(t)) { print_add(i, t); } } for (I j(size - 1); j > 1; j--) { I t = path[j]; if (!target[i].contains(t)) { print_delete(i, t); } } bad.pop_front(); } } } void add_missing() { for (I i(0); i < atoms; i++) { std::list<I> missing; for (auto it = target[i].begin(); it != target[i].end(); it++) { if (!source[i].contains(*it)) { missing.push_back(*it); } } while (!missing.empty()) { I m = missing.front(); counter++; std::vector<I> path; visits[i] = counter; path.push_back(i); find_path(path, m); I size = path.size(); for (I j(2); j < size; j++) { I t = path[j]; if (!source[i].contains(t)) { print_add(i, t); } } for (I j(2); j < size - 1; j++) { I t = path[j]; if (!target[i].contains(t)) { print_delete(i, t); } } source[i].insert(m); source[m].insert(i); missing.pop_front(); } } } void find_path(std::vector<I> &path, I m) { if (path.back() == m) { return; } for (const auto &next: source[path.back()]) { if (visits[next] != counter) { visits[next] = counter; path.push_back(next); find_path(path, m); if (path.back() == m) { return; } else { path.pop_back(); } }; } } void print() { std::cout << result.size() << '\n'; for (const auto &item: result) { std::cout << item << '\n'; } } }; // 17:35 - 20:55 int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); Alc alc; alc.run(); alc.print(); }
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 | // PA2024 runda 2B - https://sio2.mimuw.edu.pl/c/pa-2024-1/p/alc/ //-std=c++20 #include<iostream> #include <cstddef> #include <algorithm> #include<vector> #include<list> #include<set> using I = u_int64_t; struct Alc { I atoms; // <=30K std::vector<std::set<I>> source; std::vector<std::set<I>> target; std::list<std::string> result; std::vector<I> visits; I counter = 0; void input_graph(std::vector<std::set<I>> &graph) { graph.resize(atoms, std::set<I>()); visits.resize(atoms, 0); I edges; // <=50k std::cin >> edges; for (I i(0); i < edges; i++) { I a, b; std::cin >> a >> b; if (a > b) { std::swap(a, b); } a--; b--; // user indeksuje po liczbach naturalnych graph[a].insert(b); graph[b].insert(a); } } bool is_k2(I i, I m) { for (const auto &k1: source[i]) { for (const auto &k2: source[k1]) { if (k2 == m) { return true; } } } return false; } void print_add(I i, I m) { result.push_back("+ " + std::to_string(i + 1) + " " + std::to_string(m + 1)); } void print_delete(I i, I m) { result.push_back("- " + std::to_string(i + 1) + " " + std::to_string(m + 1)); } void run() { std::cin >> atoms; input_graph(source); input_graph(target); add_missing(); remove_bad(); } void remove_bad() { for (I i(0); i < atoms; i++) { std::list<I> bad; for (auto it = source[i].begin(); it != source[i].end(); it++) { if (!target[i].contains(*it)) { bad.push_back(*it); } } while (!bad.empty()) { auto b = bad.front(); source[i].erase(b); source[b].erase(i); counter++; std::vector<I> path; visits[i] = counter; path.push_back(i); find_path(path, b); I size = path.size(); for (I j(2); j < size - 1; j++) { I t = path[j]; if (!source[i].contains(t)) { print_add(i, t); } } for (I j(size - 1); j > 1; j--) { I t = path[j]; if (!target[i].contains(t)) { print_delete(i, t); } } bad.pop_front(); } } } void add_missing() { for (I i(0); i < atoms; i++) { std::list<I> missing; for (auto it = target[i].begin(); it != target[i].end(); it++) { if (!source[i].contains(*it)) { missing.push_back(*it); } } while (!missing.empty()) { I m = missing.front(); counter++; std::vector<I> path; visits[i] = counter; path.push_back(i); find_path(path, m); I size = path.size(); for (I j(2); j < size; j++) { I t = path[j]; if (!source[i].contains(t)) { print_add(i, t); } } for (I j(2); j < size - 1; j++) { I t = path[j]; if (!target[i].contains(t)) { print_delete(i, t); } } source[i].insert(m); source[m].insert(i); missing.pop_front(); } } } void find_path(std::vector<I> &path, I m) { if (path.back() == m) { return; } for (const auto &next: source[path.back()]) { if (visits[next] != counter) { visits[next] = counter; path.push_back(next); find_path(path, m); if (path.back() == m) { return; } else { path.pop_back(); } }; } } void print() { std::cout << result.size() << '\n'; for (const auto &item: result) { std::cout << item << '\n'; } } }; // 17:35 - 20:55 int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); Alc alc; alc.run(); alc.print(); } |