#include<bits/stdc++.h> using namespace std; constexpr int MAXN = 3e4+30; int n; map<string, bool> perf; vector<int> G[MAXN]; vector<int> G2[MAXN]; bool visited[MAXN]; vector<int> track; bool count_dfs(int a, int b, int k){ visited[a] = true; if(k == 0 && a == b) { track.push_back(a); return true; } for(const auto v : G[a]) { if(!visited[v]){ if(count_dfs(v, b, k - 1)){ track.push_back(a); return true; } } } return false; } bool does_edge_exist(int a, int b){ return find(G[a].begin(), G[a].end(), b) != G[a].end(); } bool does_edge_exist2(int a, int b){ return find(G2[a].begin(), G2[a].end(), b) != G2[a].end(); } pair<vector<string>, int> add_edge(int a, int b) { fill(visited, visited + MAXN, false); track.clear(); if(count_dfs(a, b, 2)){ vector<string> tmp; tmp.push_back("+ " + to_string(a) + " " + to_string(b)); G[a].push_back(b); G[b].push_back(a); return make_pair(tmp, 1); } fill(visited, visited + MAXN, false); track.clear(); if(count_dfs(a, b, 3)){ vector<string> tmp; int c = track[2]; int d = track[1]; if(!does_edge_exist(a,d)){ tmp.push_back("+ " + to_string(a) + " " + to_string(d)); tmp.push_back("+ " + to_string(a) + " " + to_string(b)); tmp.push_back("- " + to_string(a) + " " + to_string(d)); } else { tmp.push_back("+ " + to_string(b) + " " + to_string(c)); tmp.push_back("+ " + to_string(a) + " " + to_string(b)); tmp.push_back("- " + to_string(b) + " " + to_string(c)); } G[a].push_back(b); G[b].push_back(a); return make_pair(tmp, 3); } } pair<vector<string>, int> remove_edge(int a, int b) { G[a].erase(find(G[a].begin(), G[a].end(), b)); G[b].erase(find(G[b].begin(), G[b].end(), a)); fill(visited, visited + n + 10, false); track.clear(); if(count_dfs(a, b, 2)){ vector<string> tmp; tmp.push_back("- " + to_string(a) + " " + to_string(b)); return make_pair(tmp, 1); } fill(visited, visited + n + 10, false); track.clear(); if(count_dfs(a, b, 3)){ vector<string> tmp; int c = track[2]; int d = track[1]; if(!does_edge_exist(a,d)){ tmp.push_back("+ " + to_string(a) + " " + to_string(d)); tmp.push_back("- " + to_string(a) + " " + to_string(b)); tmp.push_back("- " + to_string(a) + " " + to_string(d)); } else { tmp.push_back("+ " + to_string(b) + " " + to_string(c)); tmp.push_back("- " + to_string(a) + " " + to_string(b)); tmp.push_back("- " + to_string(b) + " " + to_string(c)); } return make_pair(tmp, 3); } } bool fast_check(string a) { string with_plus = a; with_plus[0] = '+'; string with_minus = a; with_minus[0] = '-'; if(perf.find(with_plus) != perf.end() && perf.find(with_minus) != perf.end()){ return true; } return false; } int main() { int m1, m2; cin >> n >> m1; for(int i = 0; i < m1; i++) { int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } cin >> m2; int res = 0; vector<string> results; vector<string> final_result1; vector<string> final_result2; for(int i = 0; i < m2; i++) { int a, b; cin >> a >> b; G2[a].push_back(b); G2[b].push_back(a); if(find(G[a].begin(), G[a].end(), b) == G[a].end()){ auto info = add_edge(a, b); res += info.second; for(const string& txtt : info.first){ results.push_back(txtt); } } } for(int i = 1; i <= n; i++){ for(const auto v : G[i]){ if(!does_edge_exist2(i, v)){ auto info = remove_edge(i , v); res += info.second; for(const string& txtt : info.first){ results.push_back(txtt); } } } } for(int i = 0; i <= results.size()/2; i++) { string tmp = results[i]; string tmp2 = results[results.size() - i - 1]; if(!fast_check(tmp)){ final_result1.push_back(tmp); } if((results.size() - i - 1) != i && !fast_check(tmp2)){ final_result2.push_back(tmp2); } perf[tmp] = true; perf[tmp2] = true; } cout << final_result1.size() + final_result2.size() << '\n'; for(const auto v : final_result1) { cout << v << '\n'; } for(int i = final_result2.size() - 1; i >= 0 ; i--) { cout << final_result2[i] << '\n'; } 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 | #include<bits/stdc++.h> using namespace std; constexpr int MAXN = 3e4+30; int n; map<string, bool> perf; vector<int> G[MAXN]; vector<int> G2[MAXN]; bool visited[MAXN]; vector<int> track; bool count_dfs(int a, int b, int k){ visited[a] = true; if(k == 0 && a == b) { track.push_back(a); return true; } for(const auto v : G[a]) { if(!visited[v]){ if(count_dfs(v, b, k - 1)){ track.push_back(a); return true; } } } return false; } bool does_edge_exist(int a, int b){ return find(G[a].begin(), G[a].end(), b) != G[a].end(); } bool does_edge_exist2(int a, int b){ return find(G2[a].begin(), G2[a].end(), b) != G2[a].end(); } pair<vector<string>, int> add_edge(int a, int b) { fill(visited, visited + MAXN, false); track.clear(); if(count_dfs(a, b, 2)){ vector<string> tmp; tmp.push_back("+ " + to_string(a) + " " + to_string(b)); G[a].push_back(b); G[b].push_back(a); return make_pair(tmp, 1); } fill(visited, visited + MAXN, false); track.clear(); if(count_dfs(a, b, 3)){ vector<string> tmp; int c = track[2]; int d = track[1]; if(!does_edge_exist(a,d)){ tmp.push_back("+ " + to_string(a) + " " + to_string(d)); tmp.push_back("+ " + to_string(a) + " " + to_string(b)); tmp.push_back("- " + to_string(a) + " " + to_string(d)); } else { tmp.push_back("+ " + to_string(b) + " " + to_string(c)); tmp.push_back("+ " + to_string(a) + " " + to_string(b)); tmp.push_back("- " + to_string(b) + " " + to_string(c)); } G[a].push_back(b); G[b].push_back(a); return make_pair(tmp, 3); } } pair<vector<string>, int> remove_edge(int a, int b) { G[a].erase(find(G[a].begin(), G[a].end(), b)); G[b].erase(find(G[b].begin(), G[b].end(), a)); fill(visited, visited + n + 10, false); track.clear(); if(count_dfs(a, b, 2)){ vector<string> tmp; tmp.push_back("- " + to_string(a) + " " + to_string(b)); return make_pair(tmp, 1); } fill(visited, visited + n + 10, false); track.clear(); if(count_dfs(a, b, 3)){ vector<string> tmp; int c = track[2]; int d = track[1]; if(!does_edge_exist(a,d)){ tmp.push_back("+ " + to_string(a) + " " + to_string(d)); tmp.push_back("- " + to_string(a) + " " + to_string(b)); tmp.push_back("- " + to_string(a) + " " + to_string(d)); } else { tmp.push_back("+ " + to_string(b) + " " + to_string(c)); tmp.push_back("- " + to_string(a) + " " + to_string(b)); tmp.push_back("- " + to_string(b) + " " + to_string(c)); } return make_pair(tmp, 3); } } bool fast_check(string a) { string with_plus = a; with_plus[0] = '+'; string with_minus = a; with_minus[0] = '-'; if(perf.find(with_plus) != perf.end() && perf.find(with_minus) != perf.end()){ return true; } return false; } int main() { int m1, m2; cin >> n >> m1; for(int i = 0; i < m1; i++) { int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } cin >> m2; int res = 0; vector<string> results; vector<string> final_result1; vector<string> final_result2; for(int i = 0; i < m2; i++) { int a, b; cin >> a >> b; G2[a].push_back(b); G2[b].push_back(a); if(find(G[a].begin(), G[a].end(), b) == G[a].end()){ auto info = add_edge(a, b); res += info.second; for(const string& txtt : info.first){ results.push_back(txtt); } } } for(int i = 1; i <= n; i++){ for(const auto v : G[i]){ if(!does_edge_exist2(i, v)){ auto info = remove_edge(i , v); res += info.second; for(const string& txtt : info.first){ results.push_back(txtt); } } } } for(int i = 0; i <= results.size()/2; i++) { string tmp = results[i]; string tmp2 = results[results.size() - i - 1]; if(!fast_check(tmp)){ final_result1.push_back(tmp); } if((results.size() - i - 1) != i && !fast_check(tmp2)){ final_result2.push_back(tmp2); } perf[tmp] = true; perf[tmp2] = true; } cout << final_result1.size() + final_result2.size() << '\n'; for(const auto v : final_result1) { cout << v << '\n'; } for(int i = final_result2.size() - 1; i >= 0 ; i--) { cout << final_result2[i] << '\n'; } return 0; } |