#include <bits/stdc++.h> #include <bits/extc++.h> using namespace std; using namespace __gnu_pbds; #define fwd(i,a,b) for(int i=(a); i<(b);i++) #define rep(i,n) for(int i=0; i<(n);i++) #define ford(i,a,b) for(int i=(a); i>(b);i--) #define all(X) (X).begin(), (X).end() #define sz(X)((int)(X).size()) #define st first #define nd second #define DEBUG #ifdef DEBUG template<typename T1, typename T2> auto& operator<<(ostream& out, const pair<T1, T2>& a) { return out << "(" << a.first << ", " << a.second << ")"; } template<typename T, typename N> auto& operator<<(N& out, const T& a) { out << "{"; for (const auto& b : a) out << b << ", "; return out << "}"; } template<typename... Args> void print(Args&&... args) { (cerr << ... << args) << "\n"; } #define debug(x...) cerr << "[" #x "]: ", print(x) #else #define debug(...) ; #endif template<typename T> using Tree = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; vector<ll> order; vector<bool> visited; vector<vector<ll>> adj; void dfs(ll v) { visited[v] = true; if(v!=1) order.emplace_back(v); for(auto u : adj[v]) { if(!visited[u]) dfs(u); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n, m, a, b; cin >> n; visited = vector<bool>(n+1,false); adj = vector<vector<ll>>(n+1, vector<ll>()); set<pair<ll, ll>> s1, s2; cin >> m; rep(_,m) { cin >> a >> b; if(a>b) swap(a,b); s1.insert(make_pair(a,b)); adj[a].emplace_back(b); adj[b].emplace_back(a); } cin >> m; rep(_,m) { cin >> a >> b; if(a>b) swap(a,b); s2.insert(make_pair(a,b)); } dfs(1); vector<string> ans; for(auto v : order) { if(s1.count({1,v})) continue; ans.emplace_back("+ 1 " + to_string(v)); } for(auto [p,q] : s2) { if(p==1 || s1.count({p,q})) continue; ans.emplace_back("+ " + to_string(p) + " " + to_string(q)); } for(auto [p,q] : s1) { if(p==1 || s2.count({p,q})) continue; ans.emplace_back("- " + to_string(p) + " " + to_string(q)); } reverse(all(order)); for(auto v : order) { if(s2.count({1,v})) continue; ans.emplace_back("- 1 " + to_string(v)); } cout << ans.size() << '\n'; rep(i,sz(ans)) cout << ans[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 | #include <bits/stdc++.h> #include <bits/extc++.h> using namespace std; using namespace __gnu_pbds; #define fwd(i,a,b) for(int i=(a); i<(b);i++) #define rep(i,n) for(int i=0; i<(n);i++) #define ford(i,a,b) for(int i=(a); i>(b);i--) #define all(X) (X).begin(), (X).end() #define sz(X)((int)(X).size()) #define st first #define nd second #define DEBUG #ifdef DEBUG template<typename T1, typename T2> auto& operator<<(ostream& out, const pair<T1, T2>& a) { return out << "(" << a.first << ", " << a.second << ")"; } template<typename T, typename N> auto& operator<<(N& out, const T& a) { out << "{"; for (const auto& b : a) out << b << ", "; return out << "}"; } template<typename... Args> void print(Args&&... args) { (cerr << ... << args) << "\n"; } #define debug(x...) cerr << "[" #x "]: ", print(x) #else #define debug(...) ; #endif template<typename T> using Tree = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; vector<ll> order; vector<bool> visited; vector<vector<ll>> adj; void dfs(ll v) { visited[v] = true; if(v!=1) order.emplace_back(v); for(auto u : adj[v]) { if(!visited[u]) dfs(u); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n, m, a, b; cin >> n; visited = vector<bool>(n+1,false); adj = vector<vector<ll>>(n+1, vector<ll>()); set<pair<ll, ll>> s1, s2; cin >> m; rep(_,m) { cin >> a >> b; if(a>b) swap(a,b); s1.insert(make_pair(a,b)); adj[a].emplace_back(b); adj[b].emplace_back(a); } cin >> m; rep(_,m) { cin >> a >> b; if(a>b) swap(a,b); s2.insert(make_pair(a,b)); } dfs(1); vector<string> ans; for(auto v : order) { if(s1.count({1,v})) continue; ans.emplace_back("+ 1 " + to_string(v)); } for(auto [p,q] : s2) { if(p==1 || s1.count({p,q})) continue; ans.emplace_back("+ " + to_string(p) + " " + to_string(q)); } for(auto [p,q] : s1) { if(p==1 || s2.count({p,q})) continue; ans.emplace_back("- " + to_string(p) + " " + to_string(q)); } reverse(all(order)); for(auto v : order) { if(s2.count({1,v})) continue; ans.emplace_back("- 1 " + to_string(v)); } cout << ans.size() << '\n'; rep(i,sz(ans)) cout << ans[i] << '\n'; return 0; } |