#include <bits/stdc++.h> #define pb push_back #define st first #define nd second using ll = long long; using ld = long double; using namespace std; const int N = 30005; int n, m1, m2; vector <pair <int,int> > kra1, kra2; vector <int> v1[N], v2[N]; unordered_map <ll, int> mp1; unordered_map <ll, int> mp2; unordered_map <ll, int> mp; vector <int> z2; vector <int> z3; vector <int> z22; vector <int> z33; bool visited1[N]; bool visited2[N]; void dfs1(int x) { if(mp1[(ll)min(x, 1) * 100000LL + max(x, 1)] == 0 && x != 1) { z2.pb(1); z3.pb(x); } visited1[x] = 1; for(int i = 0; i < v1[x].size(); i++) { if(!visited1[v1[x][i]]) { dfs1(v1[x][i]); } } } void dfs2(int x) { if(mp2[(ll)min(x, 1) * 100000LL + max(x, 1)] == 0 && x != 1) { z22.pb(1); z33.pb(x); } visited2[x] = 1; for(int i = 0; i < v2[x].size(); i++) { if(!visited2[v2[x][i]]) { dfs2(v2[x][i]); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m1; for(int i = 0; i < m1; i++) { int a, b; cin >> a >> b; v1[a].pb(b); v1[b].pb(a); mp1[(ll)min(a, b) * 100000LL + max(a, b)] = 1; mp[(ll)min(a, b) * 100000LL + max(a, b)] = 1; kra1.pb({a, b}); } cin >> m2; for(int i = 0; i < m2; i++) { int a, b; cin >> a >> b; kra2.pb({a, b}); v2[a].pb(b); v2[b].pb(a); mp2[(ll)min(a, b) * 100000LL + max(a, b)] = 1; mp[(ll)min(a, b) * 100000LL + max(a, b)] = 1; } dfs1(1); for(int i = 0; i < kra2.size(); i++) { if(kra2[i].st != 1 && kra2[i].nd != 1) { if(mp1[(ll)min(kra2[i].st, kra2[i].nd) * 100000LL + max(kra2[i].st, kra2[i].nd)] == 0) { z2.pb(kra2[i].st); z3.pb(kra2[i].nd); } } } dfs2(1); for(int i = 0; i < kra1.size(); i++) { if(kra1[i].st != 1 && kra1[i].nd != 1) { if(mp2[(ll)min(kra1[i].st, kra1[i].nd) * 100000LL + max(kra1[i].st, kra1[i].nd)] == 0) { z22.pb(kra1[i].st); z33.pb(kra1[i].nd); } } } int ans = (int)z2.size() + (int)z22.size(); cout << ans << '\n'; for(int i = 0; i < z2.size(); i++) { cout << "+ " << z2[i] << ' ' << z3[i] << '\n'; } for(int i = z22.size() - 1; i >= 0; i--) { cout << "- " << z22[i] << ' ' << z33[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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | #include <bits/stdc++.h> #define pb push_back #define st first #define nd second using ll = long long; using ld = long double; using namespace std; const int N = 30005; int n, m1, m2; vector <pair <int,int> > kra1, kra2; vector <int> v1[N], v2[N]; unordered_map <ll, int> mp1; unordered_map <ll, int> mp2; unordered_map <ll, int> mp; vector <int> z2; vector <int> z3; vector <int> z22; vector <int> z33; bool visited1[N]; bool visited2[N]; void dfs1(int x) { if(mp1[(ll)min(x, 1) * 100000LL + max(x, 1)] == 0 && x != 1) { z2.pb(1); z3.pb(x); } visited1[x] = 1; for(int i = 0; i < v1[x].size(); i++) { if(!visited1[v1[x][i]]) { dfs1(v1[x][i]); } } } void dfs2(int x) { if(mp2[(ll)min(x, 1) * 100000LL + max(x, 1)] == 0 && x != 1) { z22.pb(1); z33.pb(x); } visited2[x] = 1; for(int i = 0; i < v2[x].size(); i++) { if(!visited2[v2[x][i]]) { dfs2(v2[x][i]); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m1; for(int i = 0; i < m1; i++) { int a, b; cin >> a >> b; v1[a].pb(b); v1[b].pb(a); mp1[(ll)min(a, b) * 100000LL + max(a, b)] = 1; mp[(ll)min(a, b) * 100000LL + max(a, b)] = 1; kra1.pb({a, b}); } cin >> m2; for(int i = 0; i < m2; i++) { int a, b; cin >> a >> b; kra2.pb({a, b}); v2[a].pb(b); v2[b].pb(a); mp2[(ll)min(a, b) * 100000LL + max(a, b)] = 1; mp[(ll)min(a, b) * 100000LL + max(a, b)] = 1; } dfs1(1); for(int i = 0; i < kra2.size(); i++) { if(kra2[i].st != 1 && kra2[i].nd != 1) { if(mp1[(ll)min(kra2[i].st, kra2[i].nd) * 100000LL + max(kra2[i].st, kra2[i].nd)] == 0) { z2.pb(kra2[i].st); z3.pb(kra2[i].nd); } } } dfs2(1); for(int i = 0; i < kra1.size(); i++) { if(kra1[i].st != 1 && kra1[i].nd != 1) { if(mp2[(ll)min(kra1[i].st, kra1[i].nd) * 100000LL + max(kra1[i].st, kra1[i].nd)] == 0) { z22.pb(kra1[i].st); z33.pb(kra1[i].nd); } } } int ans = (int)z2.size() + (int)z22.size(); cout << ans << '\n'; for(int i = 0; i < z2.size(); i++) { cout << "+ " << z2[i] << ' ' << z3[i] << '\n'; } for(int i = z22.size() - 1; i >= 0; i--) { cout << "- " << z22[i] << ' ' << z33[i] << '\n'; } return 0; } |