#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair <int, int>; using pll = pair <ll, ll>; const int inf = 1e9+7; const ll inf_ll = 1e18+7; void boost () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int n, ms, md, a, b; bool odw[30007]; vector <int> gs[30007], gd[30007]; set <int> ss[30007], sd[30007]; vector <pii> res_add, res_rem; void dfs_add(int v) { odw[v] = true; for (int u: gs[v]) { if (!odw[u]) { if (ss[u].find(1) == ss[u].end()) { ss[u].insert(1); ss[1].insert(u); res_add.push_back({1, u}); } dfs_add(u); } } } void dfs_rem(int v) { odw[v] = true; for (int u: gd[v]) { if (!odw[u]) { dfs_rem(u); if (sd[u].find(1) == sd[u].end()) { ss[u].erase(1); ss[1].erase(u); res_rem.push_back({1, u}); } } } } int main () { boost(); cin >> n; cin >> ms; for (int i = 1; i <= ms; i++) { cin >> a >> b; gs[a].push_back(b); gs[b].push_back(a); ss[a].insert(b); ss[b].insert(a); } cin >> md; for (int i = 1; i <= md; i++) { cin >> a >> b; gd[a].push_back(b); gd[b].push_back(a); sd[a].insert(b); sd[b].insert(a); } dfs_add(1); for (int i = 1; i <= n; i++) { for (int u: gd[i]) { if (ss[i].find(u) == ss[i].end()) { ss[i].insert(u); ss[u].insert(i); res_add.push_back({i, u}); } } } for (int i = 2; i <= n; i++) { for (int u: gs[i]) { if (u != 1 && sd[i].find(u) == sd[i].end() && ss[i].find(u) != ss[i].end()) { ss[i].erase(u); ss[u].erase(i); res_rem.push_back({i, u}); } } } for (int i = 1; i <= n; i++) odw[i] = false; dfs_rem(1); cout << res_add.size() + res_rem.size() << "\n"; for (pii p: res_add) { cout << "+ " << p.first << " " << p.second << "\n"; } for (pii p: res_rem) { cout << "- " << p.first << " " << p.second << "\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> using namespace std; using ll = long long; using pii = pair <int, int>; using pll = pair <ll, ll>; const int inf = 1e9+7; const ll inf_ll = 1e18+7; void boost () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int n, ms, md, a, b; bool odw[30007]; vector <int> gs[30007], gd[30007]; set <int> ss[30007], sd[30007]; vector <pii> res_add, res_rem; void dfs_add(int v) { odw[v] = true; for (int u: gs[v]) { if (!odw[u]) { if (ss[u].find(1) == ss[u].end()) { ss[u].insert(1); ss[1].insert(u); res_add.push_back({1, u}); } dfs_add(u); } } } void dfs_rem(int v) { odw[v] = true; for (int u: gd[v]) { if (!odw[u]) { dfs_rem(u); if (sd[u].find(1) == sd[u].end()) { ss[u].erase(1); ss[1].erase(u); res_rem.push_back({1, u}); } } } } int main () { boost(); cin >> n; cin >> ms; for (int i = 1; i <= ms; i++) { cin >> a >> b; gs[a].push_back(b); gs[b].push_back(a); ss[a].insert(b); ss[b].insert(a); } cin >> md; for (int i = 1; i <= md; i++) { cin >> a >> b; gd[a].push_back(b); gd[b].push_back(a); sd[a].insert(b); sd[b].insert(a); } dfs_add(1); for (int i = 1; i <= n; i++) { for (int u: gd[i]) { if (ss[i].find(u) == ss[i].end()) { ss[i].insert(u); ss[u].insert(i); res_add.push_back({i, u}); } } } for (int i = 2; i <= n; i++) { for (int u: gs[i]) { if (u != 1 && sd[i].find(u) == sd[i].end() && ss[i].find(u) != ss[i].end()) { ss[i].erase(u); ss[u].erase(i); res_rem.push_back({i, u}); } } } for (int i = 1; i <= n; i++) odw[i] = false; dfs_rem(1); cout << res_add.size() + res_rem.size() << "\n"; for (pii p: res_add) { cout << "+ " << p.first << " " << p.second << "\n"; } for (pii p: res_rem) { cout << "- " << p.first << " " << p.second << "\n"; } return 0; } |