#include <bits/stdc++.h> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; const int MAX_N = 30001; unordered_set<int> source_adj[MAX_N], target_adj[MAX_N]; bool visited[MAX_N], visited2[MAX_N]; void dfs_to_add(int x, vector<pii> &to_add, vi &rev_topsort) { // cerr << "dfs_to_add x=" << x << "\n"; visited[x] = true; // if (not(x == 1 or source_adj[x].count(1) > 0)) { // cerr << "add edge 1 <=> x=" << x << "\n"; // to_add.emplace_back(1, x); // source_adj[x].emplace(1); // source_adj[1].emplace(x); // } for (const auto &y : source_adj[x]) { // cerr << " y=" << y << " visited=" << visited[y] << "\n"; if (not visited[y]) { dfs_to_add(y, to_add, rev_topsort); } } rev_topsort.emplace_back(x); } void dfs_to_remove(int x, vector<pii> &to_remove) { visited2[x] = true; for (const auto &y : target_adj[x]) { if (not visited2[y]) { dfs_to_remove(y, to_remove); } } vi removed; for (const auto &y : source_adj[x]) { if (target_adj[x].count(y) == 0) { to_remove.emplace_back(x, y); removed.emplace_back(y); } } for (const auto &y : removed) { source_adj[x].erase(y); source_adj[y].erase(x); } } // struct Compare { // vi order; // Compare(const vi &rev_topsort) : order(SZ(rev_topsort) + 1) { // REP(i, SZ(rev_topsort)) { order[rev_topsort[i]] = i; } // } // bool operator()(const int &a, const int &b) const { return order[a] < order[b]; } // }; void inline one() { int n; cin >> n; int source_m; cin >> source_m; REP(i, source_m) { int a, b; cin >> a >> b; source_adj[a].emplace(b); source_adj[b].emplace(a); } int target_m; cin >> target_m; REP(i, target_m) { int a, b; cin >> a >> b; target_adj[a].emplace(b); target_adj[b].emplace(a); } vector<pii> to_add, to_remove; vi rev_topsort; dfs_to_add(1, to_add, rev_topsort); int axis = rev_topsort.back(); // cerr << "axis=" << axis << "\n"; FORD(i, SZ(rev_topsort) - 2, 0) { int x = rev_topsort[i]; // cerr << "topsort x=" << x << "\n"; if (source_adj[axis].count(x) == 0) { to_add.emplace_back(axis, x); source_adj[axis].emplace(x); source_adj[x].emplace(axis); } } FOR(x, 1, n) { for (const auto &y : target_adj[x]) { if (source_adj[x].count(y) == 0) { source_adj[x].emplace(y); source_adj[y].emplace(x); to_add.emplace_back(x, y); } } } FOR(x, 2, n) { vi removed; for (const auto &y : source_adj[x]) { if (y == 1) { continue; } if (target_adj[x].count(y) == 0) { to_remove.emplace_back(x, y); removed.emplace_back(y); } } for (const auto &y : removed) { source_adj[x].erase(y); source_adj[y].erase(x); } } // const auto compare = Compare(rev_topsort); // for (const auto &x : rev_topsort) { // cerr << "rev_topsort: " << x << "\n"; // vi removed; // vi adj(source_adj[x].begin(), source_adj[x].end()); // sort(adj.begin(), adj.end(), compare); // for (const auto &y : adj) { // if (target_adj[x].count(y) == 0) { // removed.emplace_back(y); // to_remove.emplace_back(x, y); // } // } // for (const auto &y : removed) { // source_adj[x].erase(y); // source_adj[y].erase(x); // } // } dfs_to_remove(1, to_remove); int result = SZ(to_add) + SZ(to_remove); cout << result << "\n"; for (const auto &[a, b] : to_add) { cout << "+ " << a << " " << b << "\n"; } for (const auto &[a, b] : to_remove) { cout << "- " << a << " " << b << "\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); }
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | #include <bits/stdc++.h> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; const int MAX_N = 30001; unordered_set<int> source_adj[MAX_N], target_adj[MAX_N]; bool visited[MAX_N], visited2[MAX_N]; void dfs_to_add(int x, vector<pii> &to_add, vi &rev_topsort) { // cerr << "dfs_to_add x=" << x << "\n"; visited[x] = true; // if (not(x == 1 or source_adj[x].count(1) > 0)) { // cerr << "add edge 1 <=> x=" << x << "\n"; // to_add.emplace_back(1, x); // source_adj[x].emplace(1); // source_adj[1].emplace(x); // } for (const auto &y : source_adj[x]) { // cerr << " y=" << y << " visited=" << visited[y] << "\n"; if (not visited[y]) { dfs_to_add(y, to_add, rev_topsort); } } rev_topsort.emplace_back(x); } void dfs_to_remove(int x, vector<pii> &to_remove) { visited2[x] = true; for (const auto &y : target_adj[x]) { if (not visited2[y]) { dfs_to_remove(y, to_remove); } } vi removed; for (const auto &y : source_adj[x]) { if (target_adj[x].count(y) == 0) { to_remove.emplace_back(x, y); removed.emplace_back(y); } } for (const auto &y : removed) { source_adj[x].erase(y); source_adj[y].erase(x); } } // struct Compare { // vi order; // Compare(const vi &rev_topsort) : order(SZ(rev_topsort) + 1) { // REP(i, SZ(rev_topsort)) { order[rev_topsort[i]] = i; } // } // bool operator()(const int &a, const int &b) const { return order[a] < order[b]; } // }; void inline one() { int n; cin >> n; int source_m; cin >> source_m; REP(i, source_m) { int a, b; cin >> a >> b; source_adj[a].emplace(b); source_adj[b].emplace(a); } int target_m; cin >> target_m; REP(i, target_m) { int a, b; cin >> a >> b; target_adj[a].emplace(b); target_adj[b].emplace(a); } vector<pii> to_add, to_remove; vi rev_topsort; dfs_to_add(1, to_add, rev_topsort); int axis = rev_topsort.back(); // cerr << "axis=" << axis << "\n"; FORD(i, SZ(rev_topsort) - 2, 0) { int x = rev_topsort[i]; // cerr << "topsort x=" << x << "\n"; if (source_adj[axis].count(x) == 0) { to_add.emplace_back(axis, x); source_adj[axis].emplace(x); source_adj[x].emplace(axis); } } FOR(x, 1, n) { for (const auto &y : target_adj[x]) { if (source_adj[x].count(y) == 0) { source_adj[x].emplace(y); source_adj[y].emplace(x); to_add.emplace_back(x, y); } } } FOR(x, 2, n) { vi removed; for (const auto &y : source_adj[x]) { if (y == 1) { continue; } if (target_adj[x].count(y) == 0) { to_remove.emplace_back(x, y); removed.emplace_back(y); } } for (const auto &y : removed) { source_adj[x].erase(y); source_adj[y].erase(x); } } // const auto compare = Compare(rev_topsort); // for (const auto &x : rev_topsort) { // cerr << "rev_topsort: " << x << "\n"; // vi removed; // vi adj(source_adj[x].begin(), source_adj[x].end()); // sort(adj.begin(), adj.end(), compare); // for (const auto &y : adj) { // if (target_adj[x].count(y) == 0) { // removed.emplace_back(y); // to_remove.emplace_back(x, y); // } // } // for (const auto &y : removed) { // source_adj[x].erase(y); // source_adj[y].erase(x); // } // } dfs_to_remove(1, to_remove); int result = SZ(to_add) + SZ(to_remove); cout << result << "\n"; for (const auto &[a, b] : to_add) { cout << "+ " << a << " " << b << "\n"; } for (const auto &[a, b] : to_remove) { cout << "- " << a << " " << b << "\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); } |