/* 2024 * Maciej Szeptuch */ #include <algorithm> #include <cstdio> #include <queue> #include <set> #include <vector> const int MAX_ACTIONS = 200001; const int MAX_ATOMS = 30001; const int MAX_BONDS = 50001; using Bond = std::pair<int, int>; using Path = std::vector<int>; int actions; int atoms; int source_bonds; int target_bonds; Bond source_bond[MAX_BONDS]; Bond target_bond[MAX_BONDS]; std::pair<bool, Bond> action[MAX_ACTIONS]; std::set<int> graph[MAX_ATOMS]; void remove_bond(const Bond &bond); void add_bond(const Bond &bond); Path find_path(const int s, const int e); void connect_path(const Path &path, int s = 0, int e = -1); void disconnect_path(const Path &path, int s = 0, int e = -1); void add_edge(const int s, const int e); void remove_edge(const int s, const int e); int main(void) { scanf("%d %d", &atoms, &source_bonds); for(int b = 0; b < source_bonds; ++b) { scanf("%d %d", &source_bond[b].first, &source_bond[b].second); if(source_bond[b].second < source_bond[b].first) std::swap(source_bond[b].first, source_bond[b].second); graph[source_bond[b].first].insert(source_bond[b].second); graph[source_bond[b].second].insert(source_bond[b].first); } scanf("%d", &target_bonds); for(int b = 0; b < target_bonds; ++b) { scanf("%d %d", &target_bond[b].first, &target_bond[b].second); if(target_bond[b].second < target_bond[b].first) std::swap(target_bond[b].first, target_bond[b].second); } std::sort(source_bond, source_bond + source_bonds); std::sort(target_bond, target_bond + target_bonds); int sb = 0; int tb = 0; while(sb < source_bonds && tb < target_bonds) { if(source_bond[sb] == target_bond[tb]) { ++sb; ++tb; continue; } if(source_bond[sb] < target_bond[tb]) { ++sb; continue; } add_bond(target_bond[tb]); ++tb; } while(tb < target_bonds) { add_bond(target_bond[tb]); ++tb; } sb = 0; tb = 0; while(sb < source_bonds && tb < target_bonds) { if(source_bond[sb] == target_bond[tb]) { ++sb; ++tb; continue; } if(source_bond[sb] < target_bond[tb]) { remove_bond(source_bond[sb]); ++sb; continue; } ++tb; } while(sb < source_bonds) { remove_bond(source_bond[sb]); ++sb; } printf("%d\n", actions); for(int a = 0; a < actions; ++a) printf("%c %d %d\n", action[a].first ? '+' : '-', action[a].second.first, action[a].second.second); return 0; } inline void remove_bond(const Bond &bond) { Path path = find_path(bond.first, bond.second); connect_path(path); remove_edge(bond.first, bond.second); disconnect_path(path); } inline void add_bond(const Bond &bond) { Path path = find_path(bond.first, bond.second); connect_path(path); add_edge(bond.first, bond.second); disconnect_path(path); } inline Path find_path(const int s, const int e) { std::vector<int> prev(atoms, 0); std::queue<int> que; que.push(s); while(!que.empty()) { int v = que.front(); que.pop(); for(auto n: graph[v]) { if(prev[n]) continue; if(v == s && n == e) continue; if(n == e) { Path path{e}; for(int p = v; p != s; p = prev[p]) path.push_back(p); path.push_back(s); std::reverse(std::begin(path), std::end(path)); return path; } prev[n] = v; que.push(n); } } return {}; } inline void connect_path(const Path &path, int s, int e) { if(e == -1) e = path.size() - 1; if(e - s <= 2) return; if(e - s > 3) { connect_path(path, s, (s + e) / 2); add_edge(path[s], path[(s + e) / 2]); } connect_path(path, (s + e) / 2, e); add_edge(path[(s + e) / 2], path[e]); } inline void disconnect_path(const Path &path, int s, int e) { if(e == -1) e = path.size() - 1; if(e - s <= 2) return; remove_edge(path[(s + e) / 2], path[e]); disconnect_path(path, (s + e) / 2, e); if(e - s > 3) { remove_edge(path[s], path[(s + e) / 2]); disconnect_path(path, s, (s + e) / 2); } } inline void add_edge(const int s, const int e) { graph[s].insert(e); graph[e].insert(s); action[actions++] = {true, {s, e}}; } inline void remove_edge(const int s, const int e) { graph[s].erase(e); graph[e].erase(s); action[actions++] = {false, {s, e}}; }
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 | /* 2024 * Maciej Szeptuch */ #include <algorithm> #include <cstdio> #include <queue> #include <set> #include <vector> const int MAX_ACTIONS = 200001; const int MAX_ATOMS = 30001; const int MAX_BONDS = 50001; using Bond = std::pair<int, int>; using Path = std::vector<int>; int actions; int atoms; int source_bonds; int target_bonds; Bond source_bond[MAX_BONDS]; Bond target_bond[MAX_BONDS]; std::pair<bool, Bond> action[MAX_ACTIONS]; std::set<int> graph[MAX_ATOMS]; void remove_bond(const Bond &bond); void add_bond(const Bond &bond); Path find_path(const int s, const int e); void connect_path(const Path &path, int s = 0, int e = -1); void disconnect_path(const Path &path, int s = 0, int e = -1); void add_edge(const int s, const int e); void remove_edge(const int s, const int e); int main(void) { scanf("%d %d", &atoms, &source_bonds); for(int b = 0; b < source_bonds; ++b) { scanf("%d %d", &source_bond[b].first, &source_bond[b].second); if(source_bond[b].second < source_bond[b].first) std::swap(source_bond[b].first, source_bond[b].second); graph[source_bond[b].first].insert(source_bond[b].second); graph[source_bond[b].second].insert(source_bond[b].first); } scanf("%d", &target_bonds); for(int b = 0; b < target_bonds; ++b) { scanf("%d %d", &target_bond[b].first, &target_bond[b].second); if(target_bond[b].second < target_bond[b].first) std::swap(target_bond[b].first, target_bond[b].second); } std::sort(source_bond, source_bond + source_bonds); std::sort(target_bond, target_bond + target_bonds); int sb = 0; int tb = 0; while(sb < source_bonds && tb < target_bonds) { if(source_bond[sb] == target_bond[tb]) { ++sb; ++tb; continue; } if(source_bond[sb] < target_bond[tb]) { ++sb; continue; } add_bond(target_bond[tb]); ++tb; } while(tb < target_bonds) { add_bond(target_bond[tb]); ++tb; } sb = 0; tb = 0; while(sb < source_bonds && tb < target_bonds) { if(source_bond[sb] == target_bond[tb]) { ++sb; ++tb; continue; } if(source_bond[sb] < target_bond[tb]) { remove_bond(source_bond[sb]); ++sb; continue; } ++tb; } while(sb < source_bonds) { remove_bond(source_bond[sb]); ++sb; } printf("%d\n", actions); for(int a = 0; a < actions; ++a) printf("%c %d %d\n", action[a].first ? '+' : '-', action[a].second.first, action[a].second.second); return 0; } inline void remove_bond(const Bond &bond) { Path path = find_path(bond.first, bond.second); connect_path(path); remove_edge(bond.first, bond.second); disconnect_path(path); } inline void add_bond(const Bond &bond) { Path path = find_path(bond.first, bond.second); connect_path(path); add_edge(bond.first, bond.second); disconnect_path(path); } inline Path find_path(const int s, const int e) { std::vector<int> prev(atoms, 0); std::queue<int> que; que.push(s); while(!que.empty()) { int v = que.front(); que.pop(); for(auto n: graph[v]) { if(prev[n]) continue; if(v == s && n == e) continue; if(n == e) { Path path{e}; for(int p = v; p != s; p = prev[p]) path.push_back(p); path.push_back(s); std::reverse(std::begin(path), std::end(path)); return path; } prev[n] = v; que.push(n); } } return {}; } inline void connect_path(const Path &path, int s, int e) { if(e == -1) e = path.size() - 1; if(e - s <= 2) return; if(e - s > 3) { connect_path(path, s, (s + e) / 2); add_edge(path[s], path[(s + e) / 2]); } connect_path(path, (s + e) / 2, e); add_edge(path[(s + e) / 2], path[e]); } inline void disconnect_path(const Path &path, int s, int e) { if(e == -1) e = path.size() - 1; if(e - s <= 2) return; remove_edge(path[(s + e) / 2], path[e]); disconnect_path(path, (s + e) / 2, e); if(e - s > 3) { remove_edge(path[s], path[(s + e) / 2]); disconnect_path(path, s, (s + e) / 2); } } inline void add_edge(const int s, const int e) { graph[s].insert(e); graph[e].insert(s); action[actions++] = {true, {s, e}}; } inline void remove_edge(const int s, const int e) { graph[s].erase(e); graph[e].erase(s); action[actions++] = {false, {s, e}}; } |