#include <cstdio> #include <vector> #include <set> const int N = 30007; std::set<int> graph[N]; std::set<std::pair<int, int>> actual_edges; std::set<std::pair<int, int>> target_edges; bool visited[N]; std::vector<std::pair<int, int>> added_edges; std::vector<std::pair<int, int>> deleted_edges; void dfs_add(int x) { visited[x] = true; if (x > 1) { std::pair<int, int> p = std::make_pair(1, x); if (actual_edges.find(p) == actual_edges.end()) { actual_edges.insert(p); added_edges.push_back(p); } } for (int y : graph[x]) if (!visited[y]) dfs_add(y); } void dfs_delete(int x) { visited[x] = true; for (int y : graph[x]) if (!visited[y]) dfs_delete(y); std::pair<int, int> p = std::make_pair(1, x); if (actual_edges.find(p) != actual_edges.end() && target_edges.find(p) == target_edges.end()) { deleted_edges.push_back(p); } } int main() { int n; scanf("%d", &n); int ms; scanf("%d", &ms); for (int i = 0; i < ms; i++) { int a, b; scanf("%d%d", &a, &b); int x = std::min(a, b); int y = std::max(a, b); graph[x].insert(y); graph[y].insert(x); actual_edges.insert(std::make_pair(x, y)); } int md; scanf("%d", &md); for (int i = 0; i < md; i++) { int a, b; scanf("%d%d", &a, &b); int x = std::min(a, b); int y = std::max(a, b); target_edges.insert(std::make_pair(x, y)); } dfs_add(1); for (std::pair<int, int> p : added_edges) { graph[p.first].insert(p.second); graph[p.second].insert(p.first); } for (std::pair<int, int> p : target_edges) { if (actual_edges.find(p) == actual_edges.end()) { graph[p.first].insert(p.second); graph[p.second].insert(p.first); actual_edges.insert(p); added_edges.push_back(p); } } for (std::pair<int, int> p : actual_edges) { if (p.first > 1 && target_edges.find(p) == target_edges.end()) { graph[p.first].erase(p.second); graph[p.second].erase(p.first); deleted_edges.push_back(p); } } for (int i = 1; i <= n; i++) visited[i] = false; visited[1] = true; for (std::pair<int, int> p : target_edges) { if (p.first == 1 && target_edges.find(p) != target_edges.end() && !visited[p.second]) { dfs_delete(p.second); } } printf("%lu\n", added_edges.size() + deleted_edges.size()); for (int i = 0; i < added_edges.size(); i++) printf("+ %d %d\n", added_edges[i].first, added_edges[i].second); for (int i = 0; i < deleted_edges.size(); i++) printf("- %d %d\n", deleted_edges[i].first, deleted_edges[i].second); 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 | #include <cstdio> #include <vector> #include <set> const int N = 30007; std::set<int> graph[N]; std::set<std::pair<int, int>> actual_edges; std::set<std::pair<int, int>> target_edges; bool visited[N]; std::vector<std::pair<int, int>> added_edges; std::vector<std::pair<int, int>> deleted_edges; void dfs_add(int x) { visited[x] = true; if (x > 1) { std::pair<int, int> p = std::make_pair(1, x); if (actual_edges.find(p) == actual_edges.end()) { actual_edges.insert(p); added_edges.push_back(p); } } for (int y : graph[x]) if (!visited[y]) dfs_add(y); } void dfs_delete(int x) { visited[x] = true; for (int y : graph[x]) if (!visited[y]) dfs_delete(y); std::pair<int, int> p = std::make_pair(1, x); if (actual_edges.find(p) != actual_edges.end() && target_edges.find(p) == target_edges.end()) { deleted_edges.push_back(p); } } int main() { int n; scanf("%d", &n); int ms; scanf("%d", &ms); for (int i = 0; i < ms; i++) { int a, b; scanf("%d%d", &a, &b); int x = std::min(a, b); int y = std::max(a, b); graph[x].insert(y); graph[y].insert(x); actual_edges.insert(std::make_pair(x, y)); } int md; scanf("%d", &md); for (int i = 0; i < md; i++) { int a, b; scanf("%d%d", &a, &b); int x = std::min(a, b); int y = std::max(a, b); target_edges.insert(std::make_pair(x, y)); } dfs_add(1); for (std::pair<int, int> p : added_edges) { graph[p.first].insert(p.second); graph[p.second].insert(p.first); } for (std::pair<int, int> p : target_edges) { if (actual_edges.find(p) == actual_edges.end()) { graph[p.first].insert(p.second); graph[p.second].insert(p.first); actual_edges.insert(p); added_edges.push_back(p); } } for (std::pair<int, int> p : actual_edges) { if (p.first > 1 && target_edges.find(p) == target_edges.end()) { graph[p.first].erase(p.second); graph[p.second].erase(p.first); deleted_edges.push_back(p); } } for (int i = 1; i <= n; i++) visited[i] = false; visited[1] = true; for (std::pair<int, int> p : target_edges) { if (p.first == 1 && target_edges.find(p) != target_edges.end() && !visited[p.second]) { dfs_delete(p.second); } } printf("%lu\n", added_edges.size() + deleted_edges.size()); for (int i = 0; i < added_edges.size(); i++) printf("+ %d %d\n", added_edges[i].first, added_edges[i].second); for (int i = 0; i < deleted_edges.size(); i++) printf("- %d %d\n", deleted_edges[i].first, deleted_edges[i].second); return 0; } |