#include <iostream> #include <map> #include <unordered_map> #include <vector> #include <unordered_set> #include <set> #include <queue> #include <utility> #include <algorithm> #include <cassert> #include <iomanip> #include <tuple> #include <stack> #include <deque> using namespace std; ///// TEMPLATES typedef long long ll; typedef tuple<ll, ll> ti2; typedef tuple<ll, ll, ll> ti3; typedef tuple<ll, ll, ll, ll> ti4; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<string> vs; typedef vector<ll> vi; typedef vector<ti2> vi2; typedef vector<ti3> vi3; typedef vector<vi> vvi; typedef set<ll> si; typedef set<ti2> si2; typedef set<ti3> si3; typedef multiset<ll> msi; typedef multiset<ti2> msi2; typedef multiset<ti3> msi3; typedef deque<ll> dqi; typedef deque<ti2> dqi2; typedef deque<ti3> dqi3; template<typename T> using PQS = priority_queue<T, vector<T>, greater<T> >; template<typename T> using PQG = priority_queue<T>; ///// OUT OPERATORS ostream& operator<<(ostream& os, const ti2& x) { os << "{ "; auto [a, b] = x; os << a << ", " << b; os << " }"; return os; } ostream& operator<<(ostream& os, const ti3& x) { os << "{ "; auto [a, b, c] = x; os << a << ", " << b << ", " << c; os << " }"; return os; } ostream& operator<<(ostream& os, const ti4& x) { os << "{ "; auto [a, b, c, d] = x; os << a << ", " << b << ", " << c << ", " << d; os << " }"; return os; } ostream& operator<<(ostream& os, const vi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vs& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vb& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vvb& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vi2& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vi3& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vvi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const msi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const msi2& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const msi3& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const dqi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const dqi2& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const dqi3& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ///// IN OPERATORS istream& operator>>(istream& is, ti2& x) { ll a, b; is >> a >> b; x = {a, b}; return is; } istream& operator>>(istream& is, ti3& x) { ll a, b, c; is >> a >> b >> c; x = {a, b, c}; return is; } istream& operator>>(istream& is, ti4& x) { ll a, b, c, d; is >> a >> b >> c >> d; x = {a, b, c, d}; return is; } int log_floor(long long x) { return 64 - __builtin_clzll(max(1LL, x)); } // PLUS ONE! int bit_cnt(ll x) { return __builtin_popcountll(x); } void rev(auto& v) { reverse(v.begin(), v.end()); } void sor(auto& v) { sort(v.begin(), v.end()); } // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣠⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣆⠀⢀⣀⣀⣤⣤⣤⣦⣦⣤⣤⣄⣀⣀⠀⢠⣾⣿⣿⣿⣿⣿⣷⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⠛⠿⣿⣿⣿⣿⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⢿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣿⣿⡟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣿⣿⣿⣿⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⣿⠟⠀⠀⠀⠀⠀⣀⣤⣤⣤⡀⠀⠀⠀⠀⠀⢀⣤⣤⣤⣄⡀⠀⠀⠀⠀⠘⣿⡿⠿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡟⠀⠀⠀⠀⣠⣾⣿⣿⣟⣿⡇⠀⠀⠀⠀⠀⢸⣿⣿⣻⣿⣿⣦⠀⠀⠀⠀⠸⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠁⠀⠀⠀⠀⣿⣿⣿⣿⣿⡟⢠⣶⣾⣿⣿⣷⣤⢽⣿⣿⣿⣿⣿⡇⠀⠀⣀⣤⣿⣷⣴⣶⣦⣀⡀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⣤⣠⣇⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⠀⠘⠻⣿⣿⣿⡿⠋⠀⢹⣿⣿⣿⣿⡇⠀⣿⣿⣿⡏⢹⣿⠉⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⢠⣾⣿⣿⣿⣿⣿⣿⣿⣶⣄⠀⠀⠹⣿⣿⠿⠋⠀⢤⣀⢀⣼⡄⠀⣠⠀⠈⠻⣿⣿⠟⠀⢸⣿⣇⣽⣿⠿⠿⠿⣿⣅⣽⣿⡇⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠁⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿⣟⠁⠀⠀⠀⠈⣿⣿⣿⡇⠀⠀⠀⠀⢀ // ⠛⠛⠛⠛⠛⠛⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛ // ⠀⠀⠀⠀⠀⠀⠘⠛⠻⢿⣿⣿⣿⣿⣿⠟⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠈⠉⠀⠀⠀⠀ [ STASZEK ] // // graphs/structures/regular struct Graph{ ll n, m; vvi G; bool directed; void add_edge(ll x, ll y) { G[x].emplace_back(y); if (!directed) G[y].emplace_back(x); } Graph(bool directed, int nn) { this->directed = directed; cin >> m; n = nn; G.resize(n + 1); for (ll i = 0; i < m; i ++) { ll x, y; cin >> x >> y; add_edge(x, y); } } vi2 get_edges(bool double_edges = false) { vi2 res; for (int i = 1; i <= n; i ++) for (auto e : G[i]) if (i <= e || double_edges) res.emplace_back(i, e); return res; } // Graph revGraph() { // Graph G(directed, n); // G.n = n; // G.m = m; // G.G.resize(n + 1); // for (auto [x, y] : get_edges(true)) { // G.add_edge(y, x); // } // return G; // } const vi& operator[](int node) { return G[node]; } void debug() { cout << "-------------------\n"; cout << "Graph " << n << " nodes & " << m << " edges\n"; for (ll i = 1; i <= n; i ++) { cout << i << ": { "; for (auto x : G[i]) { cout << x << " "; } cout << " }\n"; } cout << "-------------------\n"; } }; // graphs/algos/dfs struct DFSorder{ Graph &G; vb visited; vi order; void dfs(int node) { if (node != 1) order.emplace_back(node); visited[node] = true; for (int e : G[node]) { if (!visited[e]) { dfs(e); } } } DFSorder(Graph &G, int node) : G(G) { visited.assign(G.n + 1, 0); dfs(node); } }; // graphs/algos/dfs string action(int from, int to, char c) { string s; s += c; s += " "; s += to_string(from); s += " "; s += to_string(to); return s; } string action(ti2 t, char c) { auto [x, y] = t; return action(x, y, c); } ti2 edge(int x, int y) { return ti2{min(x, y), max(x, y)}; } struct Connect{ si2 edges, edges_want; vi order1, order2; vs actions; void add_1() { for (auto e : order1) { if (edges.count(edge(1, e))) continue; edges.insert(edge(1, e)); actions.emplace_back(action(1, e, '+')); } } void add_2() { for (auto e : edges_want) { if (edges.count(e)) continue; edges.insert(e); actions.emplace_back(action(e, '+')); } } void remove_1() { for (auto [from, to] : edges) { if (from == 1) continue; if (edges_want.count(ti2{from, to})) continue; edges.erase(edge(from, to)); actions.emplace_back(action(from, to, '-')); } } void remove_2() { for (auto e : order2) { if (!edges.count(edge(1, e))) continue; if (edges_want.count(edge(1, e))) continue; edges.erase(edge(1, e)); actions.emplace_back(action(edge(1, e), '-')); } } Connect(Graph &G1, Graph& G2) { for (int i = 1; i <= G1.n; i ++) { for (auto e : G1[i]) { if (i < e) { edges.insert(edge(i, e)); } } } for (int i = 1; i <= G2.n; i ++) { for (auto e : G2[i]) { if (i < e) { edges_want.insert(edge(i, e)); } } } order1 = DFSorder(G1, 1).order; order2 = DFSorder(G2, 1).order; rev(order2); add_1(); add_2(); remove_1(); remove_2(); } }; // inject here void solve(){ ll n; cin >> n; Graph G1(false, n); Graph G2(false, n); Connect C(G1, G2); cout << C.actions.size() << "\n"; for (auto e : C.actions) { cout << e << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(nullptr); cout << setprecision(15) << fixed; int t = 1; // cin >> t; while (t --) solve(); 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 | #include <iostream> #include <map> #include <unordered_map> #include <vector> #include <unordered_set> #include <set> #include <queue> #include <utility> #include <algorithm> #include <cassert> #include <iomanip> #include <tuple> #include <stack> #include <deque> using namespace std; ///// TEMPLATES typedef long long ll; typedef tuple<ll, ll> ti2; typedef tuple<ll, ll, ll> ti3; typedef tuple<ll, ll, ll, ll> ti4; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<string> vs; typedef vector<ll> vi; typedef vector<ti2> vi2; typedef vector<ti3> vi3; typedef vector<vi> vvi; typedef set<ll> si; typedef set<ti2> si2; typedef set<ti3> si3; typedef multiset<ll> msi; typedef multiset<ti2> msi2; typedef multiset<ti3> msi3; typedef deque<ll> dqi; typedef deque<ti2> dqi2; typedef deque<ti3> dqi3; template<typename T> using PQS = priority_queue<T, vector<T>, greater<T> >; template<typename T> using PQG = priority_queue<T>; ///// OUT OPERATORS ostream& operator<<(ostream& os, const ti2& x) { os << "{ "; auto [a, b] = x; os << a << ", " << b; os << " }"; return os; } ostream& operator<<(ostream& os, const ti3& x) { os << "{ "; auto [a, b, c] = x; os << a << ", " << b << ", " << c; os << " }"; return os; } ostream& operator<<(ostream& os, const ti4& x) { os << "{ "; auto [a, b, c, d] = x; os << a << ", " << b << ", " << c << ", " << d; os << " }"; return os; } ostream& operator<<(ostream& os, const vi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vs& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vb& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vvb& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vi2& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vi3& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vvi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const msi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const msi2& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const msi3& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const dqi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const dqi2& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const dqi3& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ///// IN OPERATORS istream& operator>>(istream& is, ti2& x) { ll a, b; is >> a >> b; x = {a, b}; return is; } istream& operator>>(istream& is, ti3& x) { ll a, b, c; is >> a >> b >> c; x = {a, b, c}; return is; } istream& operator>>(istream& is, ti4& x) { ll a, b, c, d; is >> a >> b >> c >> d; x = {a, b, c, d}; return is; } int log_floor(long long x) { return 64 - __builtin_clzll(max(1LL, x)); } // PLUS ONE! int bit_cnt(ll x) { return __builtin_popcountll(x); } void rev(auto& v) { reverse(v.begin(), v.end()); } void sor(auto& v) { sort(v.begin(), v.end()); } // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣠⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣆⠀⢀⣀⣀⣤⣤⣤⣦⣦⣤⣤⣄⣀⣀⠀⢠⣾⣿⣿⣿⣿⣿⣷⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⠛⠿⣿⣿⣿⣿⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⢿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣿⣿⡟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣿⣿⣿⣿⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⣿⠟⠀⠀⠀⠀⠀⣀⣤⣤⣤⡀⠀⠀⠀⠀⠀⢀⣤⣤⣤⣄⡀⠀⠀⠀⠀⠘⣿⡿⠿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡟⠀⠀⠀⠀⣠⣾⣿⣿⣟⣿⡇⠀⠀⠀⠀⠀⢸⣿⣿⣻⣿⣿⣦⠀⠀⠀⠀⠸⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠁⠀⠀⠀⠀⣿⣿⣿⣿⣿⡟⢠⣶⣾⣿⣿⣷⣤⢽⣿⣿⣿⣿⣿⡇⠀⠀⣀⣤⣿⣷⣴⣶⣦⣀⡀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⣤⣠⣇⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⠀⠘⠻⣿⣿⣿⡿⠋⠀⢹⣿⣿⣿⣿⡇⠀⣿⣿⣿⡏⢹⣿⠉⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⢠⣾⣿⣿⣿⣿⣿⣿⣿⣶⣄⠀⠀⠹⣿⣿⠿⠋⠀⢤⣀⢀⣼⡄⠀⣠⠀⠈⠻⣿⣿⠟⠀⢸⣿⣇⣽⣿⠿⠿⠿⣿⣅⣽⣿⡇⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠁⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿⣟⠁⠀⠀⠀⠈⣿⣿⣿⡇⠀⠀⠀⠀⢀ // ⠛⠛⠛⠛⠛⠛⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛ // ⠀⠀⠀⠀⠀⠀⠘⠛⠻⢿⣿⣿⣿⣿⣿⠟⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠈⠉⠀⠀⠀⠀ [ STASZEK ] // // graphs/structures/regular struct Graph{ ll n, m; vvi G; bool directed; void add_edge(ll x, ll y) { G[x].emplace_back(y); if (!directed) G[y].emplace_back(x); } Graph(bool directed, int nn) { this->directed = directed; cin >> m; n = nn; G.resize(n + 1); for (ll i = 0; i < m; i ++) { ll x, y; cin >> x >> y; add_edge(x, y); } } vi2 get_edges(bool double_edges = false) { vi2 res; for (int i = 1; i <= n; i ++) for (auto e : G[i]) if (i <= e || double_edges) res.emplace_back(i, e); return res; } // Graph revGraph() { // Graph G(directed, n); // G.n = n; // G.m = m; // G.G.resize(n + 1); // for (auto [x, y] : get_edges(true)) { // G.add_edge(y, x); // } // return G; // } const vi& operator[](int node) { return G[node]; } void debug() { cout << "-------------------\n"; cout << "Graph " << n << " nodes & " << m << " edges\n"; for (ll i = 1; i <= n; i ++) { cout << i << ": { "; for (auto x : G[i]) { cout << x << " "; } cout << " }\n"; } cout << "-------------------\n"; } }; // graphs/algos/dfs struct DFSorder{ Graph &G; vb visited; vi order; void dfs(int node) { if (node != 1) order.emplace_back(node); visited[node] = true; for (int e : G[node]) { if (!visited[e]) { dfs(e); } } } DFSorder(Graph &G, int node) : G(G) { visited.assign(G.n + 1, 0); dfs(node); } }; // graphs/algos/dfs string action(int from, int to, char c) { string s; s += c; s += " "; s += to_string(from); s += " "; s += to_string(to); return s; } string action(ti2 t, char c) { auto [x, y] = t; return action(x, y, c); } ti2 edge(int x, int y) { return ti2{min(x, y), max(x, y)}; } struct Connect{ si2 edges, edges_want; vi order1, order2; vs actions; void add_1() { for (auto e : order1) { if (edges.count(edge(1, e))) continue; edges.insert(edge(1, e)); actions.emplace_back(action(1, e, '+')); } } void add_2() { for (auto e : edges_want) { if (edges.count(e)) continue; edges.insert(e); actions.emplace_back(action(e, '+')); } } void remove_1() { for (auto [from, to] : edges) { if (from == 1) continue; if (edges_want.count(ti2{from, to})) continue; edges.erase(edge(from, to)); actions.emplace_back(action(from, to, '-')); } } void remove_2() { for (auto e : order2) { if (!edges.count(edge(1, e))) continue; if (edges_want.count(edge(1, e))) continue; edges.erase(edge(1, e)); actions.emplace_back(action(edge(1, e), '-')); } } Connect(Graph &G1, Graph& G2) { for (int i = 1; i <= G1.n; i ++) { for (auto e : G1[i]) { if (i < e) { edges.insert(edge(i, e)); } } } for (int i = 1; i <= G2.n; i ++) { for (auto e : G2[i]) { if (i < e) { edges_want.insert(edge(i, e)); } } } order1 = DFSorder(G1, 1).order; order2 = DFSorder(G2, 1).order; rev(order2); add_1(); add_2(); remove_1(); remove_2(); } }; // inject here void solve(){ ll n; cin >> n; Graph G1(false, n); Graph G2(false, n); Connect C(G1, G2); cout << C.actions.size() << "\n"; for (auto e : C.actions) { cout << e << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(nullptr); cout << setprecision(15) << fixed; int t = 1; // cin >> t; while (t --) solve(); return 0; } |