#include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator<<(auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator<<(auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << __LINE__ << ": [" #x "]: ", dump(x) #else #define debug(...) 0 #endif vector<array<int, 3>> simplify(int n, vector<vector<int>>& g){ vector<bool> vis(n+1, false); vis[1] = true; vector<array<int, 3>> ans; queue<int> q; for(auto u : g[1]){ q.push(u); vis[u] = true; } while(!q.empty()){ int v = q.front(); q.pop(); for(auto u : g[v]){ if(!vis[u]){ vis[u] = true; q.push(u); ans.push_back({1, 1, u}); } } } for(int i = 2; i <= n; i++){ for(auto x : g[i]){ if(x > i){ ans.push_back({-1, i, x}); } } } return ans; } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin>>n; vector<vector<int>> g1(n+1); vector<vector<int>> g2(n+1); int m1; cin>>m1; rep(i, m1){ int a, b; cin>>a>>b; g1[a].push_back(b); g1[b].push_back(a); } int m2; cin>>m2; rep(i, m2){ int a, b; cin>>a>>b; g2[a].push_back(b); g2[b].push_back(a); } vector<array<int, 3>> v1 = simplify(n, g1); vector<array<int, 3>> v2 = simplify(n, g2); reverse(all(v2)); cout<<(sz(v1) + sz(v2))<<"\n"; for(auto x : v1){ if(x[0] > 0){ cout<<"+ "<<x[1]<<" "<<x[2]<<"\n"; }else{ cout<<"- "<<x[1]<<" "<<x[2]<<"\n"; } } for(auto x : v2){ if(x[0] < 0){ cout<<"+ "<<x[1]<<" "<<x[2]<<"\n"; }else{ cout<<"- "<<x[1]<<" "<<x[2]<<"\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 95 96 97 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator<<(auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator<<(auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << __LINE__ << ": [" #x "]: ", dump(x) #else #define debug(...) 0 #endif vector<array<int, 3>> simplify(int n, vector<vector<int>>& g){ vector<bool> vis(n+1, false); vis[1] = true; vector<array<int, 3>> ans; queue<int> q; for(auto u : g[1]){ q.push(u); vis[u] = true; } while(!q.empty()){ int v = q.front(); q.pop(); for(auto u : g[v]){ if(!vis[u]){ vis[u] = true; q.push(u); ans.push_back({1, 1, u}); } } } for(int i = 2; i <= n; i++){ for(auto x : g[i]){ if(x > i){ ans.push_back({-1, i, x}); } } } return ans; } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin>>n; vector<vector<int>> g1(n+1); vector<vector<int>> g2(n+1); int m1; cin>>m1; rep(i, m1){ int a, b; cin>>a>>b; g1[a].push_back(b); g1[b].push_back(a); } int m2; cin>>m2; rep(i, m2){ int a, b; cin>>a>>b; g2[a].push_back(b); g2[b].push_back(a); } vector<array<int, 3>> v1 = simplify(n, g1); vector<array<int, 3>> v2 = simplify(n, g2); reverse(all(v2)); cout<<(sz(v1) + sz(v2))<<"\n"; for(auto x : v1){ if(x[0] > 0){ cout<<"+ "<<x[1]<<" "<<x[2]<<"\n"; }else{ cout<<"- "<<x[1]<<" "<<x[2]<<"\n"; } } for(auto x : v2){ if(x[0] < 0){ cout<<"+ "<<x[1]<<" "<<x[2]<<"\n"; }else{ cout<<"- "<<x[1]<<" "<<x[2]<<"\n"; } } return 0; } |