#include <bits/stdc++.h> using namespace std; namespace std { template<class Fun> class y_combinator_result { Fun fun_; public: template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {} template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); } }; template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); } } // namespace std int main(){ ios_base::sync_with_stdio(false), cin.tie(nullptr); int N; cin >> N; vector<vector<vector<int> >> g(2); for(int s = 0; s < 2; s++){ g[s].resize(N); int M; cin >> M; for(int i = 0; i < M; i++){ int u, v; cin >> u >> v; u--; v--; g[s][u].push_back(v); g[s][v].push_back(u); } } vector<tuple<char, int, int> > ops; for(int s = 0; s < 2; s++){ vector<pair<int, int> > add_edges; vector<int> conn0(N, 0); conn0[0] = 1; for(int v : g[s][0]) conn0[v] = 1; vector<int> vis(N, 0); vector<vector<int> > new_adj = g[s]; y_combinator( [&](auto self, int v) -> void { if(!conn0[v]){ conn0[v] = 1; add_edges.push_back({0, v}); new_adj[0].push_back(v); new_adj[v].push_back(0); } vis[v] = 1; for(int w : g[s][v]){ if(!vis[w]) self(w); } } )(0); vector<vector<int> > other = g[s^1]; vector<int> tmp(N, 0); for(int v = 0; v < N; v++){ for(int x : new_adj[v]) tmp[x] = 1; for(int x : other[v]){ if(!tmp[x]){ add_edges.push_back({v, x}); new_adj[v].push_back(x); new_adj[x].push_back(v); tmp[x] = 1; } } for(int x : new_adj[v]) tmp[x] = 0; for(int x : other[v]) tmp[x] = 0; } if(s == 0){ for(auto [u, v] : add_edges){ ops.push_back({'+', u, v}); } } else { reverse(add_edges.begin(), add_edges.end()); for(auto [u, v] : add_edges){ ops.push_back({'-', u, v}); } } } cout << ops.size() << '\n'; for(auto [c, u, v] : ops){ cout << c << ' ' << (u+1) << ' ' << (v+1) << '\n'; } }
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 | #include <bits/stdc++.h> using namespace std; namespace std { template<class Fun> class y_combinator_result { Fun fun_; public: template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {} template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); } }; template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); } } // namespace std int main(){ ios_base::sync_with_stdio(false), cin.tie(nullptr); int N; cin >> N; vector<vector<vector<int> >> g(2); for(int s = 0; s < 2; s++){ g[s].resize(N); int M; cin >> M; for(int i = 0; i < M; i++){ int u, v; cin >> u >> v; u--; v--; g[s][u].push_back(v); g[s][v].push_back(u); } } vector<tuple<char, int, int> > ops; for(int s = 0; s < 2; s++){ vector<pair<int, int> > add_edges; vector<int> conn0(N, 0); conn0[0] = 1; for(int v : g[s][0]) conn0[v] = 1; vector<int> vis(N, 0); vector<vector<int> > new_adj = g[s]; y_combinator( [&](auto self, int v) -> void { if(!conn0[v]){ conn0[v] = 1; add_edges.push_back({0, v}); new_adj[0].push_back(v); new_adj[v].push_back(0); } vis[v] = 1; for(int w : g[s][v]){ if(!vis[w]) self(w); } } )(0); vector<vector<int> > other = g[s^1]; vector<int> tmp(N, 0); for(int v = 0; v < N; v++){ for(int x : new_adj[v]) tmp[x] = 1; for(int x : other[v]){ if(!tmp[x]){ add_edges.push_back({v, x}); new_adj[v].push_back(x); new_adj[x].push_back(v); tmp[x] = 1; } } for(int x : new_adj[v]) tmp[x] = 0; for(int x : other[v]) tmp[x] = 0; } if(s == 0){ for(auto [u, v] : add_edges){ ops.push_back({'+', u, v}); } } else { reverse(add_edges.begin(), add_edges.end()); for(auto [u, v] : add_edges){ ops.push_back({'-', u, v}); } } } cout << ops.size() << '\n'; for(auto [c, u, v] : ops){ cout << c << ' ' << (u+1) << ' ' << (v+1) << '\n'; } } |