#include <bits/stdc++.h> using namespace std; int n, m, cm; bool is_sas[30005]; map <pair <int, int>, bool> exists; map <pair <int, int>, bool> shall; vector <int> sas[30005]; vector <int> asa; bool visa[30005]; bool visd[30005]; queue <pair <bool, pair <int, int>>> q; void add_edge(int v) { if (!is_sas[v]) { asa.push_back(v); sas[v].push_back(1); exists[{1, v}] = true; q.push({true, {1, v}}); } visa[v] = true; for (int i : sas[v]) { if (!visa[i]) add_edge(i); } } void del_edge(int v, int p = -1) { visd[v] = true; for (int i : sas[v]) { if (!visd[i] && shall[{min(i, v), max(i, v)}]) del_edge(i, v); } if (v == 1) return; int a, b; for (int i : sas[v]) { if (i == 1) continue; a = i, b = v; if (a > b) swap(a, b); if (exists[{a, b}] && !shall[{a, b}]) { exists[{a, b}] = false; q.push({false, {a, b}}); } } a = 1, b = v; if (a > b) swap(a, b); if (exists[{a, b}] && !shall[{a, b}]) { exists[{a, b}] = false; q.push({false, {a, b}}); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int a, b; cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> a >> b; if (a > b) swap(a, b); exists[{a, b}] = true; sas[a].push_back(b); sas[b].push_back(a); if (a == 1) { is_sas[b] = true; } } cin >> cm; is_sas[1] = true; add_edge(1); for (int i = 0; i < cm; ++i) { cin >> a >> b; if (a > b) swap(a, b); shall[{a, b}] = true; if (!exists[{a, b}]) { sas[a].push_back(b); sas[b].push_back(a); exists[{a, b}] = true; q.push({true, {a, b}}); } } for (int i : asa) { sas[1].push_back(i); } del_edge(1); cout << q.size() << "\n"; while (!q.empty()) { if (q.front().first) cout << "+ "; else cout << "- "; cout << q.front().second.first << " " << q.front().second.second << "\n"; q.pop(); } }
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 | #include <bits/stdc++.h> using namespace std; int n, m, cm; bool is_sas[30005]; map <pair <int, int>, bool> exists; map <pair <int, int>, bool> shall; vector <int> sas[30005]; vector <int> asa; bool visa[30005]; bool visd[30005]; queue <pair <bool, pair <int, int>>> q; void add_edge(int v) { if (!is_sas[v]) { asa.push_back(v); sas[v].push_back(1); exists[{1, v}] = true; q.push({true, {1, v}}); } visa[v] = true; for (int i : sas[v]) { if (!visa[i]) add_edge(i); } } void del_edge(int v, int p = -1) { visd[v] = true; for (int i : sas[v]) { if (!visd[i] && shall[{min(i, v), max(i, v)}]) del_edge(i, v); } if (v == 1) return; int a, b; for (int i : sas[v]) { if (i == 1) continue; a = i, b = v; if (a > b) swap(a, b); if (exists[{a, b}] && !shall[{a, b}]) { exists[{a, b}] = false; q.push({false, {a, b}}); } } a = 1, b = v; if (a > b) swap(a, b); if (exists[{a, b}] && !shall[{a, b}]) { exists[{a, b}] = false; q.push({false, {a, b}}); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int a, b; cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> a >> b; if (a > b) swap(a, b); exists[{a, b}] = true; sas[a].push_back(b); sas[b].push_back(a); if (a == 1) { is_sas[b] = true; } } cin >> cm; is_sas[1] = true; add_edge(1); for (int i = 0; i < cm; ++i) { cin >> a >> b; if (a > b) swap(a, b); shall[{a, b}] = true; if (!exists[{a, b}]) { sas[a].push_back(b); sas[b].push_back(a); exists[{a, b}] = true; q.push({true, {a, b}}); } } for (int i : asa) { sas[1].push_back(i); } del_edge(1); cout << q.size() << "\n"; while (!q.empty()) { if (q.front().first) cout << "+ "; else cout << "- "; cout << q.front().second.first << " " << q.front().second.second << "\n"; q.pop(); } } |