#include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> struct Node { size_t a; size_t b; bool operator<(const Node &other) const { return a < other.a || (a == other.a && b < other.b); } Node(size_t _a, size_t _b) : a(std::min(_a, _b)), b(std::max(_a, _b)) {} }; struct ResultItem { size_t a; size_t b; bool add; ResultItem(size_t _a, size_t _b, bool _add) : a(_a), b(_b), add(_add) {} }; std::vector<int> bfs(const std::vector<std::set<size_t>> &graph, int start, int goal) { std::queue<int> queue; std::map<int, int> predecessors; std::vector<bool> visited(graph.size(), false); visited[start] = true; queue.push(start); bool pathFound = false; while (!queue.empty()) { int current = queue.front(); queue.pop(); if (current == goal) { pathFound = true; break; } for (const int &neighbor : graph[current]) { if (!visited[neighbor]) { visited[neighbor] = true; predecessors[neighbor] = current; queue.push(neighbor); } } } std::vector<int> path; if (pathFound) { for (int at = goal; at != start; at = predecessors[at]) { path.push_back(at); } } return path; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::set<Node> toAdd, toRemove; std::vector<std::set<size_t>> graph; std::vector<ResultItem> result; int n; std::cin >> n; graph.resize(n + 1); std::cin >> n; for (int i = 0; i < n; i++) { int a, b; std::cin >> a >> b; toRemove.insert(Node(a, b)); graph[a].insert(b); graph[b].insert(a); } std::cin >> n; for (int i = 0; i < n; i++) { int a, b; std::cin >> a >> b; if (toRemove.find(Node(a, b)) != toRemove.end()) { toRemove.erase(Node(a, b)); } else { toAdd.insert(Node(a, b)); } } while (!toAdd.empty()) { Node node = *toAdd.begin(); auto path = bfs(graph, node.a, node.b); for (auto it = path.rbegin() + 1; it != path.rend(); it++) { auto nodeId = *it; Node edge(node.a, nodeId); if (toAdd.find(edge) != toAdd.end()) { toAdd.erase(edge); } else { toRemove.insert(edge); } graph[node.a].insert(nodeId); graph[nodeId].insert(node.a); result.push_back(ResultItem(node.a, nodeId, true)); } } for (auto &node : toRemove) { graph[node.a].erase(node.b); graph[node.b].erase(node.a); auto path = bfs(graph, node.a, node.b); for (auto it = path.rbegin() + 1; it != path.rend() - 1; it++) { result.push_back(ResultItem(node.a, *it, true)); } result.push_back(ResultItem(node.a, node.b, false)); for (auto it = path.begin() + 1; it != path.end() - 1; it++) { result.push_back(ResultItem(node.a, *it, false)); } } std::cout << result.size() << "\n"; for (auto &item : result) { std::cout << (item.add ? "+" : "-") << " " << item.a << " " << item.b << "\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 | #include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> struct Node { size_t a; size_t b; bool operator<(const Node &other) const { return a < other.a || (a == other.a && b < other.b); } Node(size_t _a, size_t _b) : a(std::min(_a, _b)), b(std::max(_a, _b)) {} }; struct ResultItem { size_t a; size_t b; bool add; ResultItem(size_t _a, size_t _b, bool _add) : a(_a), b(_b), add(_add) {} }; std::vector<int> bfs(const std::vector<std::set<size_t>> &graph, int start, int goal) { std::queue<int> queue; std::map<int, int> predecessors; std::vector<bool> visited(graph.size(), false); visited[start] = true; queue.push(start); bool pathFound = false; while (!queue.empty()) { int current = queue.front(); queue.pop(); if (current == goal) { pathFound = true; break; } for (const int &neighbor : graph[current]) { if (!visited[neighbor]) { visited[neighbor] = true; predecessors[neighbor] = current; queue.push(neighbor); } } } std::vector<int> path; if (pathFound) { for (int at = goal; at != start; at = predecessors[at]) { path.push_back(at); } } return path; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::set<Node> toAdd, toRemove; std::vector<std::set<size_t>> graph; std::vector<ResultItem> result; int n; std::cin >> n; graph.resize(n + 1); std::cin >> n; for (int i = 0; i < n; i++) { int a, b; std::cin >> a >> b; toRemove.insert(Node(a, b)); graph[a].insert(b); graph[b].insert(a); } std::cin >> n; for (int i = 0; i < n; i++) { int a, b; std::cin >> a >> b; if (toRemove.find(Node(a, b)) != toRemove.end()) { toRemove.erase(Node(a, b)); } else { toAdd.insert(Node(a, b)); } } while (!toAdd.empty()) { Node node = *toAdd.begin(); auto path = bfs(graph, node.a, node.b); for (auto it = path.rbegin() + 1; it != path.rend(); it++) { auto nodeId = *it; Node edge(node.a, nodeId); if (toAdd.find(edge) != toAdd.end()) { toAdd.erase(edge); } else { toRemove.insert(edge); } graph[node.a].insert(nodeId); graph[nodeId].insert(node.a); result.push_back(ResultItem(node.a, nodeId, true)); } } for (auto &node : toRemove) { graph[node.a].erase(node.b); graph[node.b].erase(node.a); auto path = bfs(graph, node.a, node.b); for (auto it = path.rbegin() + 1; it != path.rend() - 1; it++) { result.push_back(ResultItem(node.a, *it, true)); } result.push_back(ResultItem(node.a, node.b, false)); for (auto it = path.begin() + 1; it != path.end() - 1; it++) { result.push_back(ResultItem(node.a, *it, false)); } } std::cout << result.size() << "\n"; for (auto &item : result) { std::cout << (item.add ? "+" : "-") << " " << item.a << " " << item.b << "\n"; } return 0; } |