#include <cstdio> #include <algorithm> #include <set> #include <vector> #include <queue> using namespace std; bool temp[31000]; void bfs(set<int>* g, int n, int* path) { for (int i = 1; i <= n; i++) { temp[i] = false; path[i] = -1; } queue<int> q; q.push(1); int s = 0; temp[1] = true; while (!q.empty()) { int c = q.front(); q.pop(); path[s++] = c; set<int>::iterator it; for (it = g[c].begin(); it != g[c].end(); ++it) if (!temp[*it]) { temp[*it] = true; q.push(*it); } } } struct Operation { char op; int a, b; }; int main() { int n, m1, m2; scanf("%d", &n); set<int>* g1 = new set<int>[n + 1]; set<int>* g2 = new set<int>[n + 1]; scanf("%d", &m1); for (int i = 0; i < m1; i++) { int a, b; scanf("%d %d", &a, &b); g1[a].insert(b); g1[b].insert(a); } scanf("%d", &m2); for (int i = 0; i < m2; i++) { int a, b; scanf("%d %d", &a, &b); g2[a].insert(b); g2[b].insert(a); } int* path1 = new int[n], * path2 = new int[n]; bfs(g1, n, path1); bfs(g2, n, path2); vector<Operation> results; for (int i = 1; i < n; i++) { if (g1[1].find(path1[i]) == g1[1].end()) { Operation o; o.a = 1; o.b = path1[i]; o.op = '+'; results.push_back(o); } } for (int i = 2; i < n; i++) { set<int>::iterator it; for (it = g1[i].begin(); it != g1[i].end(); ++it) { if (i < *it && g2[i].find(*it) == g2[i].end()) { Operation o; o.a = i; o.b = *it; o.op = '-'; results.push_back(o); } } for (it = g2[i].begin(); it != g2[i].end(); ++it) { if (i < *it && g1[i].find(*it) == g1[i].end()) { Operation o; o.a = i; o.b = *it; o.op = '+'; results.push_back(o); } } } for (int i = n - 1; i >= 1; i--) { if (g2[1].find(path2[i]) == g2[1].end()) { Operation o; o.a = 1; o.b = path2[i]; o.op = '-'; results.push_back(o); } } printf("%d\n", results.size()); for (int i = 0; i < results.size(); i++) { printf("%c %d %d\n", results[i].op, results[i].a, results[i].b); } 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 132 133 134 135 136 | #include <cstdio> #include <algorithm> #include <set> #include <vector> #include <queue> using namespace std; bool temp[31000]; void bfs(set<int>* g, int n, int* path) { for (int i = 1; i <= n; i++) { temp[i] = false; path[i] = -1; } queue<int> q; q.push(1); int s = 0; temp[1] = true; while (!q.empty()) { int c = q.front(); q.pop(); path[s++] = c; set<int>::iterator it; for (it = g[c].begin(); it != g[c].end(); ++it) if (!temp[*it]) { temp[*it] = true; q.push(*it); } } } struct Operation { char op; int a, b; }; int main() { int n, m1, m2; scanf("%d", &n); set<int>* g1 = new set<int>[n + 1]; set<int>* g2 = new set<int>[n + 1]; scanf("%d", &m1); for (int i = 0; i < m1; i++) { int a, b; scanf("%d %d", &a, &b); g1[a].insert(b); g1[b].insert(a); } scanf("%d", &m2); for (int i = 0; i < m2; i++) { int a, b; scanf("%d %d", &a, &b); g2[a].insert(b); g2[b].insert(a); } int* path1 = new int[n], * path2 = new int[n]; bfs(g1, n, path1); bfs(g2, n, path2); vector<Operation> results; for (int i = 1; i < n; i++) { if (g1[1].find(path1[i]) == g1[1].end()) { Operation o; o.a = 1; o.b = path1[i]; o.op = '+'; results.push_back(o); } } for (int i = 2; i < n; i++) { set<int>::iterator it; for (it = g1[i].begin(); it != g1[i].end(); ++it) { if (i < *it && g2[i].find(*it) == g2[i].end()) { Operation o; o.a = i; o.b = *it; o.op = '-'; results.push_back(o); } } for (it = g2[i].begin(); it != g2[i].end(); ++it) { if (i < *it && g1[i].find(*it) == g1[i].end()) { Operation o; o.a = i; o.b = *it; o.op = '+'; results.push_back(o); } } } for (int i = n - 1; i >= 1; i--) { if (g2[1].find(path2[i]) == g2[1].end()) { Operation o; o.a = 1; o.b = path2[i]; o.op = '-'; results.push_back(o); } } printf("%d\n", results.size()); for (int i = 0; i < results.size(); i++) { printf("%c %d %d\n", results[i].op, results[i].a, results[i].b); } return 0; } |