#include<bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define pii pair<int, int> #define st first #define nd second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() const int MAXN = 30'009; vector<pair<int, pii>> ans; vector<int> G[MAXN]; vector<int> G2[MAXN]; bool is1[MAXN]; bool is2[MAXN]; set<pii> S1, S2; bool vis[MAXN]; bool vis2[MAXN]; void dfs1(int x) { vis[x] = true; if(x!=1&&!is1[x]) { ans.pb({1, {x, 1}}); } for(int y:G[x]) { if(!vis[y]) { dfs1(y); } } } void dfs2(int x) { vis2[x] = true; for(int y:G2[x]) { if(!vis2[y]) { dfs2(y); } } if(x!=1&&!is2[x]) { ans.pb({0, {x, 1}}); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int m1; cin >> m1; for(int i=0;i<m1;i++) { int a, b; cin >> a >> b; if(a>b) swap(a, b); if(a==1) { is1[b] = true; } else { S1.insert({a, b}); } G[a].pb(b); G[b].pb(a); } int m2; cin >> m2; for(int i=0;i<m2;i++) { int a, b; cin >> a >> b; if(a>b) swap(a, b); if(a==1) { is2[b] = true; } else { S2.insert({a, b}); } G2[a].pb(b); G2[b].pb(a); } dfs1(1); for(pii x:S1) { if(!S2.count(x)) { ans.pb({0, x}); } } for(pii x:S2) { if(!S1.count(x)) { ans.pb({1, x}); } } dfs2(1); cout << sz(ans) << "\n"; for(auto [type, x]:ans) { if(type==0) cout << "- "; else cout << "+ "; cout << x.st << " " << x.nd << "\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 int int64_t #define pb push_back #define pii pair<int, int> #define st first #define nd second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() const int MAXN = 30'009; vector<pair<int, pii>> ans; vector<int> G[MAXN]; vector<int> G2[MAXN]; bool is1[MAXN]; bool is2[MAXN]; set<pii> S1, S2; bool vis[MAXN]; bool vis2[MAXN]; void dfs1(int x) { vis[x] = true; if(x!=1&&!is1[x]) { ans.pb({1, {x, 1}}); } for(int y:G[x]) { if(!vis[y]) { dfs1(y); } } } void dfs2(int x) { vis2[x] = true; for(int y:G2[x]) { if(!vis2[y]) { dfs2(y); } } if(x!=1&&!is2[x]) { ans.pb({0, {x, 1}}); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int m1; cin >> m1; for(int i=0;i<m1;i++) { int a, b; cin >> a >> b; if(a>b) swap(a, b); if(a==1) { is1[b] = true; } else { S1.insert({a, b}); } G[a].pb(b); G[b].pb(a); } int m2; cin >> m2; for(int i=0;i<m2;i++) { int a, b; cin >> a >> b; if(a>b) swap(a, b); if(a==1) { is2[b] = true; } else { S2.insert({a, b}); } G2[a].pb(b); G2[b].pb(a); } dfs1(1); for(pii x:S1) { if(!S2.count(x)) { ans.pb({0, x}); } } for(pii x:S2) { if(!S1.count(x)) { ans.pb({1, x}); } } dfs2(1); cout << sz(ans) << "\n"; for(auto [type, x]:ans) { if(type==0) cout << "- "; else cout << "+ "; cout << x.st << " " << x.nd << "\n"; } } |