#include <algorithm> #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <queue> std::unordered_map<int, std::vector<int>> parse() { int m; std::cin >> m; std::unordered_map<int, std::vector<int>> ret; for (int i = 0; i < m; ++i) { int a, b; std::cin >> a >> b; ret[a].emplace_back(b); ret[b].emplace_back(a); } for (auto &[_, v]: ret) { (void)_; std::sort(v.begin(), v.end()); } return ret; } std::vector<int> resolve_ones(const std::unordered_map<int, std::vector<int>> &g) { std::unordered_set<int> seen = {1}; std::queue<int> next; for (auto i: g.at(1)) { seen.insert(i); next.emplace(i); } std::vector<int> ret; while (!next.empty()) { for (auto i: g.at(next.front())) { if (!seen.count(i)) { seen.insert(i); next.emplace(i); ret.emplace_back(i); } } next.pop(); } return ret; } int main() { int n; std::cin >> n; std::unordered_map<int, std::vector<int>> orig = parse(), dest = parse(); std::vector<int> setup_add = resolve_ones(orig), teardown_del = resolve_ones(dest); std::reverse(teardown_del.begin(), teardown_del.end()); std::vector<std::pair<int, int>> change_del, change_add; for (auto i = 2; i <= n; ++i) { auto &o = orig.at(i); auto &d = dest.at(i); auto o_it = o.begin(), d_it = d.begin(); while (o_it != o.end() && d_it != d.end()) { if (*o_it == *d_it) { ++o_it; ++d_it; } else if (*o_it < *d_it) { if (*o_it > i) { change_del.emplace_back(i, *o_it); } ++o_it; } else { if (*d_it > i) { change_add.emplace_back(i, *d_it); } ++d_it; } } while (o_it != o.end()) { if (*o_it > i) { change_del.emplace_back(i, *o_it); } ++o_it; } while (d_it != d.end()) { if (*d_it > i) { change_add.emplace_back(i, *d_it); } ++d_it; } } std::cout << setup_add.size() + teardown_del.size() + change_add.size() + change_del.size() << "\n"; for (auto b: setup_add) { std::cout << "+ 1" << " " << b << "\n"; } for (auto [a, b]: change_add) { std::cout << "+ " << a << " " << b << "\n"; } for (auto [a, b]: change_del) { std::cout << "- " << a << " " << b << "\n"; } for (auto b: teardown_del) { std::cout << "- 1" << " " << b << "\n"; } }
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 | #include <algorithm> #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #include <queue> std::unordered_map<int, std::vector<int>> parse() { int m; std::cin >> m; std::unordered_map<int, std::vector<int>> ret; for (int i = 0; i < m; ++i) { int a, b; std::cin >> a >> b; ret[a].emplace_back(b); ret[b].emplace_back(a); } for (auto &[_, v]: ret) { (void)_; std::sort(v.begin(), v.end()); } return ret; } std::vector<int> resolve_ones(const std::unordered_map<int, std::vector<int>> &g) { std::unordered_set<int> seen = {1}; std::queue<int> next; for (auto i: g.at(1)) { seen.insert(i); next.emplace(i); } std::vector<int> ret; while (!next.empty()) { for (auto i: g.at(next.front())) { if (!seen.count(i)) { seen.insert(i); next.emplace(i); ret.emplace_back(i); } } next.pop(); } return ret; } int main() { int n; std::cin >> n; std::unordered_map<int, std::vector<int>> orig = parse(), dest = parse(); std::vector<int> setup_add = resolve_ones(orig), teardown_del = resolve_ones(dest); std::reverse(teardown_del.begin(), teardown_del.end()); std::vector<std::pair<int, int>> change_del, change_add; for (auto i = 2; i <= n; ++i) { auto &o = orig.at(i); auto &d = dest.at(i); auto o_it = o.begin(), d_it = d.begin(); while (o_it != o.end() && d_it != d.end()) { if (*o_it == *d_it) { ++o_it; ++d_it; } else if (*o_it < *d_it) { if (*o_it > i) { change_del.emplace_back(i, *o_it); } ++o_it; } else { if (*d_it > i) { change_add.emplace_back(i, *d_it); } ++d_it; } } while (o_it != o.end()) { if (*o_it > i) { change_del.emplace_back(i, *o_it); } ++o_it; } while (d_it != d.end()) { if (*d_it > i) { change_add.emplace_back(i, *d_it); } ++d_it; } } std::cout << setup_add.size() + teardown_del.size() + change_add.size() + change_del.size() << "\n"; for (auto b: setup_add) { std::cout << "+ 1" << " " << b << "\n"; } for (auto [a, b]: change_add) { std::cout << "+ " << a << " " << b << "\n"; } for (auto [a, b]: change_del) { std::cout << "- " << a << " " << b << "\n"; } for (auto b: teardown_del) { std::cout << "- 1" << " " << b << "\n"; } } |