#include <cstdio> #include <set> #include <vector> #include <algorithm> #include <queue> #pragma warning ( disable : 4996 ) using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; vector<vi> readGraph(int n) { vector<vi> res(n+1, vi()); int m; scanf("%d", &m); for (int k = 0; k < m; k++) { int u, v; scanf("%d %d", &u, &v); res[u].push_back(v); res[v].push_back(u); } return res; } inline pii makePairMinMax(int a, int b) { if (a <= b) return pii(a, b); else return pii(b, a); } void buildDiffLine(int v, vi &list1, vi &list2, set<pii> &diff1, set<pii> &diff2, set<int> &usedVertices) { sort(list1.begin(), list1.end()); sort(list2.begin(), list2.end()); size_t i = 0, j = 0; while (i < list1.size() && j < list2.size()) { if (list1[i] == list2[j]) { i++; j++; // do nothing } else if (list1[i] < list2[j]) { usedVertices.insert(v); usedVertices.insert(list1[i]); diff2.insert(makePairMinMax(v, list1[i])); i++; } else { // list1[i] > list2[j] usedVertices.insert(v); usedVertices.insert(list2[j]); diff1.insert(makePairMinMax(v, list2[j])); j++; } } while (i < list1.size()) { usedVertices.insert(v); usedVertices.insert(list1[i]); diff2.insert(makePairMinMax(v, list1[i])); i++; } while (j < list2.size()) { usedVertices.insert(v); usedVertices.insert(list2[j]); diff1.insert(makePairMinMax(v, list2[j])); j++; } } // set<int> requiredFinishes should be passed BY COPY, _NOT_ BY ALIASE vector<pii> bfs(const vector<vi> &graph, int start, set<int> requiredFinishes, vi &dist) { vector<pii> res; queue<int> qqq; qqq.push(start); vi st(graph.size(), 0); dist.assign(graph.size(), 0x7FFF7FFF); st[start] = 1; dist[start] = 0; while (!qqq.empty() && !(requiredFinishes.empty())) { int curr = qqq.front(); qqq.pop(); requiredFinishes.erase(curr); for (int next : graph[curr]) { if (st[next] == 0) { st[next] = 1; dist[next] = dist[curr] + 1; qqq.push(next); if (dist[next] > 1) res.push_back(pii(start, next)); requiredFinishes.erase(next); } } } return res; } void buildDiffAll(vector<vi> &g1, vector<vi> &g2, set<pii> &diff1main, set<pii> &diff2main, vector<pii> &diff1vertex1, vector<pii> &diff2vertex1) { set<int> usedVertices; diff1main.clear(); diff2main.clear(); for (size_t v = 1; v < g1.size(); v++) { buildDiffLine(v, g1[v], g2[v], diff1main, diff2main, usedVertices); } vi dist; diff1vertex1 = bfs(g1, 1, usedVertices, dist); diff2vertex1.clear(); for (size_t i = 0; i < diff1vertex1.size(); i++) { if (/*diff2main.find(diff1vertex1[i]) == diff2main.end() && */diff1main.find(diff1vertex1[i]) == diff1main.end()) diff2vertex1.push_back(diff1vertex1[i]); } for (pii p : diff1vertex1) diff1main.erase(p); /* size_t i = 0; while (i < diff1vertex1.size() && dist[diff1vertex1[i].second] <= 1) i++; if (i > 0) diff1vertex1.erase(diff1vertex1.begin(), diff1vertex1.begin() + i); */ for (pii p : diff2vertex1) diff2main.erase(p); } int main(int argc, char* argv[]) { #ifdef _DEBUG freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif int n; scanf("%d", &n); vector<vi> graph1 = readGraph(n); vector<vi> graph2 = readGraph(n); set<pii> diff1main; set<pii> diff2main; vector<pii> diff1vertex1; vector<pii> diff2vertex1; buildDiffAll(graph1, graph2, diff1main, diff2main, diff1vertex1, diff2vertex1); printf("%d\n", diff1main.size() + diff2main.size() + diff1vertex1.size() + diff2vertex1.size()); for (vector<pii>::iterator it = diff1vertex1.begin(); it != diff1vertex1.end(); it++) { printf("+ %d %d\n", it->first, it->second); } for (set<pii>::iterator it = diff1main.begin(); it != diff1main.end(); it++) { printf("+ %d %d\n", it->first, it->second); } for (set<pii>::reverse_iterator it = diff2main.rbegin(); it != diff2main.rend(); it++) { printf("- %d %d\n", it->first, it->second); } for (vector<pii>::reverse_iterator it = diff2vertex1.rbegin(); it != diff2vertex1.rend(); it++) { printf("- %d %d\n", it->first, it->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 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 | #include <cstdio> #include <set> #include <vector> #include <algorithm> #include <queue> #pragma warning ( disable : 4996 ) using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; vector<vi> readGraph(int n) { vector<vi> res(n+1, vi()); int m; scanf("%d", &m); for (int k = 0; k < m; k++) { int u, v; scanf("%d %d", &u, &v); res[u].push_back(v); res[v].push_back(u); } return res; } inline pii makePairMinMax(int a, int b) { if (a <= b) return pii(a, b); else return pii(b, a); } void buildDiffLine(int v, vi &list1, vi &list2, set<pii> &diff1, set<pii> &diff2, set<int> &usedVertices) { sort(list1.begin(), list1.end()); sort(list2.begin(), list2.end()); size_t i = 0, j = 0; while (i < list1.size() && j < list2.size()) { if (list1[i] == list2[j]) { i++; j++; // do nothing } else if (list1[i] < list2[j]) { usedVertices.insert(v); usedVertices.insert(list1[i]); diff2.insert(makePairMinMax(v, list1[i])); i++; } else { // list1[i] > list2[j] usedVertices.insert(v); usedVertices.insert(list2[j]); diff1.insert(makePairMinMax(v, list2[j])); j++; } } while (i < list1.size()) { usedVertices.insert(v); usedVertices.insert(list1[i]); diff2.insert(makePairMinMax(v, list1[i])); i++; } while (j < list2.size()) { usedVertices.insert(v); usedVertices.insert(list2[j]); diff1.insert(makePairMinMax(v, list2[j])); j++; } } // set<int> requiredFinishes should be passed BY COPY, _NOT_ BY ALIASE vector<pii> bfs(const vector<vi> &graph, int start, set<int> requiredFinishes, vi &dist) { vector<pii> res; queue<int> qqq; qqq.push(start); vi st(graph.size(), 0); dist.assign(graph.size(), 0x7FFF7FFF); st[start] = 1; dist[start] = 0; while (!qqq.empty() && !(requiredFinishes.empty())) { int curr = qqq.front(); qqq.pop(); requiredFinishes.erase(curr); for (int next : graph[curr]) { if (st[next] == 0) { st[next] = 1; dist[next] = dist[curr] + 1; qqq.push(next); if (dist[next] > 1) res.push_back(pii(start, next)); requiredFinishes.erase(next); } } } return res; } void buildDiffAll(vector<vi> &g1, vector<vi> &g2, set<pii> &diff1main, set<pii> &diff2main, vector<pii> &diff1vertex1, vector<pii> &diff2vertex1) { set<int> usedVertices; diff1main.clear(); diff2main.clear(); for (size_t v = 1; v < g1.size(); v++) { buildDiffLine(v, g1[v], g2[v], diff1main, diff2main, usedVertices); } vi dist; diff1vertex1 = bfs(g1, 1, usedVertices, dist); diff2vertex1.clear(); for (size_t i = 0; i < diff1vertex1.size(); i++) { if (/*diff2main.find(diff1vertex1[i]) == diff2main.end() && */diff1main.find(diff1vertex1[i]) == diff1main.end()) diff2vertex1.push_back(diff1vertex1[i]); } for (pii p : diff1vertex1) diff1main.erase(p); /* size_t i = 0; while (i < diff1vertex1.size() && dist[diff1vertex1[i].second] <= 1) i++; if (i > 0) diff1vertex1.erase(diff1vertex1.begin(), diff1vertex1.begin() + i); */ for (pii p : diff2vertex1) diff2main.erase(p); } int main(int argc, char* argv[]) { #ifdef _DEBUG freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif int n; scanf("%d", &n); vector<vi> graph1 = readGraph(n); vector<vi> graph2 = readGraph(n); set<pii> diff1main; set<pii> diff2main; vector<pii> diff1vertex1; vector<pii> diff2vertex1; buildDiffAll(graph1, graph2, diff1main, diff2main, diff1vertex1, diff2vertex1); printf("%d\n", diff1main.size() + diff2main.size() + diff1vertex1.size() + diff2vertex1.size()); for (vector<pii>::iterator it = diff1vertex1.begin(); it != diff1vertex1.end(); it++) { printf("+ %d %d\n", it->first, it->second); } for (set<pii>::iterator it = diff1main.begin(); it != diff1main.end(); it++) { printf("+ %d %d\n", it->first, it->second); } for (set<pii>::reverse_iterator it = diff2main.rbegin(); it != diff2main.rend(); it++) { printf("- %d %d\n", it->first, it->second); } for (vector<pii>::reverse_iterator it = diff2vertex1.rbegin(); it != diff2vertex1.rend(); it++) { printf("- %d %d\n", it->first, it->second); } return 0; } |