#include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; vector<vector<pi>> alles; vector<array<int,3>> transformstar(int n) { int m; cin >> m; vector<pi> es(m); for(auto& [u,v] : es) cin >> u >> v,--u,--v; alles.push_back(es); vector<array<int,3>> ans; vector<bool> vis(n); vector<bool> con0(n); const int root = 0; con0[root]=1; vvi adj(n); for(auto [u,v] : es) { adj[u].push_back(v); adj[v].push_back(u); } for(auto u : adj[root]) con0[u]=1; auto dfs = [&](auto&& self, int at) -> void { vis[at]=1; if(!con0[at]) { ans.push_back({1,root,at}); } for(int to : adj[at]) if(!vis[to]) { self(self,to); } }; dfs(dfs,root); for(auto [u,v] : es) { if(u!=root and v!=root) { ans.push_back({-1,u,v}); } } return ans; } // checking correctness! int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; auto a = transformstar(n); auto b = transformstar(n); reverse(all(b)); for(auto& [sgn,a,b] : b) sgn=-sgn; a.insert(a.end(),all(b)); cout << a.size() << '\n'; // check it! #ifdef LOCAL vector<set<int>> adj(n); for(auto& [u,v] : alles[0]) { adj[u].insert(v); adj[v].insert(u); } #endif for(auto [sgn,u,v] : a) { #ifdef LOCAL bool good=0; for(auto to : adj[u]) { if(adj[v].count(to)) { good=1; break; } } assert(good); if(sgn==1) { assert(!adj[u].count(v)); adj[u].insert(v); adj[v].insert(u); } else { assert(adj[u].count(v)); adj[u].erase(v); adj[v].erase(u); } #endif if(sgn==1) cout << "+ "; else cout << "- "; cout << 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 98 99 100 | #include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; vector<vector<pi>> alles; vector<array<int,3>> transformstar(int n) { int m; cin >> m; vector<pi> es(m); for(auto& [u,v] : es) cin >> u >> v,--u,--v; alles.push_back(es); vector<array<int,3>> ans; vector<bool> vis(n); vector<bool> con0(n); const int root = 0; con0[root]=1; vvi adj(n); for(auto [u,v] : es) { adj[u].push_back(v); adj[v].push_back(u); } for(auto u : adj[root]) con0[u]=1; auto dfs = [&](auto&& self, int at) -> void { vis[at]=1; if(!con0[at]) { ans.push_back({1,root,at}); } for(int to : adj[at]) if(!vis[to]) { self(self,to); } }; dfs(dfs,root); for(auto [u,v] : es) { if(u!=root and v!=root) { ans.push_back({-1,u,v}); } } return ans; } // checking correctness! int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; auto a = transformstar(n); auto b = transformstar(n); reverse(all(b)); for(auto& [sgn,a,b] : b) sgn=-sgn; a.insert(a.end(),all(b)); cout << a.size() << '\n'; // check it! #ifdef LOCAL vector<set<int>> adj(n); for(auto& [u,v] : alles[0]) { adj[u].insert(v); adj[v].insert(u); } #endif for(auto [sgn,u,v] : a) { #ifdef LOCAL bool good=0; for(auto to : adj[u]) { if(adj[v].count(to)) { good=1; break; } } assert(good); if(sgn==1) { assert(!adj[u].count(v)); adj[u].insert(v); adj[v].insert(u); } else { assert(adj[u].count(v)); adj[u].erase(v); adj[v].erase(u); } #endif if(sgn==1) cout << "+ "; else cout << "- "; cout << u+1 << ' ' << v+1 << '\n'; } } |