#include<bits/stdc++.h> using namespace std; /* */ # define int int64_t const int64_t mod = 1e9+7; const int maxn = 1e5+10; set<int> graph[maxn]; set<int> dgraph[maxn]; vector<pair<char, pair<int, int>>> ans; int n; set<pair<int, int>> cons_to_add; set<int> targets; void fill_targets(int v) { //cout << "fill_targets v=" << v << endl; targets.clear(); for (auto it = cons_to_add.upper_bound({v, -1}); it != cons_to_add.end() && it->first == v; it++) { int u = it->second; targets.insert(u); //cout << "target " << u << endl; } } queue<pair<int, pair<int, int>>> q; set<int> was_here; int parent[maxn]; void build_bfs_tree(int s) { //cout << "build_bfs_tree s=" << s << endl; fill_targets(s); while (!q.empty()) q.pop(); was_here.clear(); q.push({s, {0, -1}}); while (!q.empty() && targets.size() != 0) { int cn = q.front().first; int d = q.front().second.first; int p = q.front().second.second; q.pop(); if (was_here.find(cn) != was_here.end()) { continue; } was_here.insert(cn); parent[cn] = p; //cout << cn << " is child of " << p << endl; targets.erase(cn); if (targets.size() == 0) { break; } for (auto it = graph[cn].begin(); it != graph[cn].end(); it++) { int cnn = *it; q.push({cnn, {d+1, cn}}); } } } list<int> path; vector<pair<int, int>> help_cons; void add_conn(int u, int v) { //cout << "add_conn " << u << " " << v << endl; ans.push_back({'+', {u, v}}); graph[u].insert(v); graph[v].insert(u); cons_to_add.erase({u, v}); cons_to_add.erase({v, u}); } void add_path(int s, int e) { //cout << "add_path " << s << " " << e << endl; // tworzenie ścieżki path.clear(); int cn = e; while (cn != s) { path.push_front(cn); cn = parent[cn]; } path.push_front(cn); while (path.size() > 2) { auto it = path.begin(); while (it != path.end()) { auto lit = it; it++; if (it == path.end()) { break; } auto mit = it; it++; if (it == path.end()) { break; } auto rit = it; int u = *lit; int v = *rit; add_conn(u, v); help_cons.push_back({u, v}); path.erase(mit); parent[v] = u; } } } void add_cons_for(int s) { //cout << "add_cons_for s=" << s << endl; build_bfs_tree(s); fill_targets(s); help_cons.clear(); for (auto it = targets.begin(); it != targets.end(); it++) { int cn = *it; add_path(s, cn); } } bool conn_exists(int u, int v) { return graph[u].find(v) != graph[u].end(); } int kol[maxn]; void add_all_cons() { //cout << "add_all_cons" << endl; // tworzymy listę połączeń które trzeba dodać (a, b) cons_to_add.clear(); for (int cn = 1; cn <= n; cn++) { for (auto it = dgraph[cn].upper_bound(cn); it != dgraph[cn].end(); it++) { int cnn = *it; if (conn_exists(cn, cnn)) continue; cons_to_add.insert({cn, cnn}); cons_to_add.insert({cnn, cn}); //cout << "must add " << cn << " " << cnn << endl; } } // losujemy kolejność for (int i = 1; i <= n; i++) { kol[i] = i; } random_shuffle(kol+1, kol+1+n); // dodajemy połączenia dla pojedynczego wierzchołka for (int i = 1; i <= n; i++) { add_cons_for(kol[i]); } } bool is_conn_good(int u, int v) { return dgraph[u].find(v) != dgraph[u].end(); } bool is_conn_bad(int u, int v) { return !is_conn_good(u, v); } void delete_conn(int u, int v) { //cout << "delete conn " << u << " " << v << endl; ans.push_back({'-', {u, v}}); graph[u].erase(v); graph[v].erase(u); } void delete_bad_help_cons() { //cout << "delete_bad_help_cons" << endl; for (int i = help_cons.size()-1; i >= 0; i--) { int u = help_cons[i].first; int v = help_cons[i].second; if (is_conn_good(u, v)) continue; delete_conn(u, v); } } deque<int> cq; bool is_cleared[maxn]; int find_unrem_unpr_cnn(int s) { for (auto it = graph[s].begin(); it != graph[s].end(); it++) { int cnn = *it; if (!is_cleared[cnn]) return cnn; } return -1; } bool can_delete(int u, int v) { for (auto it = graph[u].begin(); it != graph[u].end(); it++) { int w = *it; if (graph[v].find(w) != graph[v].end()) { return true; } } return false; } vector<int> to_del; void delete_bad_from(int s) { //cout << "delete_bad_from s=" << s << endl; to_del.clear(); for (auto it = graph[s].begin(); it != graph[s].end(); it++) { int v = *it; if (is_conn_bad(s, v)) { to_del.push_back(v); //cout << "must delete " << v << endl; } } //cout << "e1" << endl; int wp = find_unrem_unpr_cnn(s); //cout << "wp=" << wp << endl; for (int i = 0; i < to_del.size(); i++) { int v = to_del[i]; if (can_delete(s, v)) { delete_conn(s, v); continue; } add_conn(v, wp); delete_conn(s, v); } //cout << "e2" << endl; } void delete_rest_bad_cons() { //cout << "delete_rest_bad_cons" << endl; while (!cq.empty()) cq.pop_front(); for (int cn = 1; cn <= n; cn++) { is_cleared[cn] = false; if (dgraph[cn].size() == 1) { cq.push_back(cn); } } if (cq.empty()) { cq.push_back(1); } while (!cq.empty()) { int cn = cq.front(); cq.pop_front(); //cout << "cn=" << cn << endl; if (is_cleared[cn]) continue; delete_bad_from(cn); is_cleared[cn] = true; for (auto it = graph[cn].begin(); it != graph[cn].end(); it++) { int cnn = *it; if (is_cleared[cnn]) continue; cq.push_back(cnn); } } //cout << "out_loop" << endl; } void delete_bad_cons() { //cout << "delete_bad_cons" << endl; delete_bad_help_cons(); delete_rest_bad_cons(); } void tc() { // wczytujemy dane cin >> n; int m; cin >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; graph[u].insert(v); graph[v].insert(u); } cin >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; dgraph[u].insert(v); dgraph[v].insert(u); } add_all_cons(); delete_bad_cons(); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i].first << " " << ans[i].second.first << " " << ans[i].second.second << endl; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); srand(52753); tc(); return 0; int T; cin >> T; while (T--) tc(); 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | #include<bits/stdc++.h> using namespace std; /* */ # define int int64_t const int64_t mod = 1e9+7; const int maxn = 1e5+10; set<int> graph[maxn]; set<int> dgraph[maxn]; vector<pair<char, pair<int, int>>> ans; int n; set<pair<int, int>> cons_to_add; set<int> targets; void fill_targets(int v) { //cout << "fill_targets v=" << v << endl; targets.clear(); for (auto it = cons_to_add.upper_bound({v, -1}); it != cons_to_add.end() && it->first == v; it++) { int u = it->second; targets.insert(u); //cout << "target " << u << endl; } } queue<pair<int, pair<int, int>>> q; set<int> was_here; int parent[maxn]; void build_bfs_tree(int s) { //cout << "build_bfs_tree s=" << s << endl; fill_targets(s); while (!q.empty()) q.pop(); was_here.clear(); q.push({s, {0, -1}}); while (!q.empty() && targets.size() != 0) { int cn = q.front().first; int d = q.front().second.first; int p = q.front().second.second; q.pop(); if (was_here.find(cn) != was_here.end()) { continue; } was_here.insert(cn); parent[cn] = p; //cout << cn << " is child of " << p << endl; targets.erase(cn); if (targets.size() == 0) { break; } for (auto it = graph[cn].begin(); it != graph[cn].end(); it++) { int cnn = *it; q.push({cnn, {d+1, cn}}); } } } list<int> path; vector<pair<int, int>> help_cons; void add_conn(int u, int v) { //cout << "add_conn " << u << " " << v << endl; ans.push_back({'+', {u, v}}); graph[u].insert(v); graph[v].insert(u); cons_to_add.erase({u, v}); cons_to_add.erase({v, u}); } void add_path(int s, int e) { //cout << "add_path " << s << " " << e << endl; // tworzenie ścieżki path.clear(); int cn = e; while (cn != s) { path.push_front(cn); cn = parent[cn]; } path.push_front(cn); while (path.size() > 2) { auto it = path.begin(); while (it != path.end()) { auto lit = it; it++; if (it == path.end()) { break; } auto mit = it; it++; if (it == path.end()) { break; } auto rit = it; int u = *lit; int v = *rit; add_conn(u, v); help_cons.push_back({u, v}); path.erase(mit); parent[v] = u; } } } void add_cons_for(int s) { //cout << "add_cons_for s=" << s << endl; build_bfs_tree(s); fill_targets(s); help_cons.clear(); for (auto it = targets.begin(); it != targets.end(); it++) { int cn = *it; add_path(s, cn); } } bool conn_exists(int u, int v) { return graph[u].find(v) != graph[u].end(); } int kol[maxn]; void add_all_cons() { //cout << "add_all_cons" << endl; // tworzymy listę połączeń które trzeba dodać (a, b) cons_to_add.clear(); for (int cn = 1; cn <= n; cn++) { for (auto it = dgraph[cn].upper_bound(cn); it != dgraph[cn].end(); it++) { int cnn = *it; if (conn_exists(cn, cnn)) continue; cons_to_add.insert({cn, cnn}); cons_to_add.insert({cnn, cn}); //cout << "must add " << cn << " " << cnn << endl; } } // losujemy kolejność for (int i = 1; i <= n; i++) { kol[i] = i; } random_shuffle(kol+1, kol+1+n); // dodajemy połączenia dla pojedynczego wierzchołka for (int i = 1; i <= n; i++) { add_cons_for(kol[i]); } } bool is_conn_good(int u, int v) { return dgraph[u].find(v) != dgraph[u].end(); } bool is_conn_bad(int u, int v) { return !is_conn_good(u, v); } void delete_conn(int u, int v) { //cout << "delete conn " << u << " " << v << endl; ans.push_back({'-', {u, v}}); graph[u].erase(v); graph[v].erase(u); } void delete_bad_help_cons() { //cout << "delete_bad_help_cons" << endl; for (int i = help_cons.size()-1; i >= 0; i--) { int u = help_cons[i].first; int v = help_cons[i].second; if (is_conn_good(u, v)) continue; delete_conn(u, v); } } deque<int> cq; bool is_cleared[maxn]; int find_unrem_unpr_cnn(int s) { for (auto it = graph[s].begin(); it != graph[s].end(); it++) { int cnn = *it; if (!is_cleared[cnn]) return cnn; } return -1; } bool can_delete(int u, int v) { for (auto it = graph[u].begin(); it != graph[u].end(); it++) { int w = *it; if (graph[v].find(w) != graph[v].end()) { return true; } } return false; } vector<int> to_del; void delete_bad_from(int s) { //cout << "delete_bad_from s=" << s << endl; to_del.clear(); for (auto it = graph[s].begin(); it != graph[s].end(); it++) { int v = *it; if (is_conn_bad(s, v)) { to_del.push_back(v); //cout << "must delete " << v << endl; } } //cout << "e1" << endl; int wp = find_unrem_unpr_cnn(s); //cout << "wp=" << wp << endl; for (int i = 0; i < to_del.size(); i++) { int v = to_del[i]; if (can_delete(s, v)) { delete_conn(s, v); continue; } add_conn(v, wp); delete_conn(s, v); } //cout << "e2" << endl; } void delete_rest_bad_cons() { //cout << "delete_rest_bad_cons" << endl; while (!cq.empty()) cq.pop_front(); for (int cn = 1; cn <= n; cn++) { is_cleared[cn] = false; if (dgraph[cn].size() == 1) { cq.push_back(cn); } } if (cq.empty()) { cq.push_back(1); } while (!cq.empty()) { int cn = cq.front(); cq.pop_front(); //cout << "cn=" << cn << endl; if (is_cleared[cn]) continue; delete_bad_from(cn); is_cleared[cn] = true; for (auto it = graph[cn].begin(); it != graph[cn].end(); it++) { int cnn = *it; if (is_cleared[cnn]) continue; cq.push_back(cnn); } } //cout << "out_loop" << endl; } void delete_bad_cons() { //cout << "delete_bad_cons" << endl; delete_bad_help_cons(); delete_rest_bad_cons(); } void tc() { // wczytujemy dane cin >> n; int m; cin >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; graph[u].insert(v); graph[v].insert(u); } cin >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; dgraph[u].insert(v); dgraph[v].insert(u); } add_all_cons(); delete_bad_cons(); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i].first << " " << ans[i].second.first << " " << ans[i].second.second << endl; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); srand(52753); tc(); return 0; int T; cin >> T; while (T--) tc(); return 0; } |