#include <bits/stdc++.h> #define MAX_N 30007 #define MAX_M 50007 #define mp make_pair using namespace std; int n, m, a, b; bool cb[MAX_N]; bool bc[MAX_N]; vector<int> vis[MAX_N]; vector<int> nvis[MAX_N]; set<pair<int, int>> vr; set<pair<int, int>> connections; stack<int> ayaya; vector<pair<int, int>> fuwafuwa; vector<pair<bool, pair<int, int>>> res; void root_me(int v) { cb[v] = true; for (int nei : vis[v]) { if (!cb[nei]) { if (!connections.contains(mp(1, nei))) { connections.insert(mp(1, nei)); connections.insert(mp(nei, 1)); res.push_back(mp(true, mp(1, nei))); } root_me(nei); } } } void me_root(int v) { bc[v] = true; for (int nei : nvis[v]) { if (!bc[nei]) { ayaya.push(nei); me_root(nei); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; cin >> m; for (int i = 1; i <= m; i++) { cin >> a >> b; vis[a].push_back(b); vis[b].push_back(a); connections.insert(mp(a, b)); connections.insert(mp(b, a)); } root_me(1); cin >> m; for (int i = 1; i <= m; i++) { cin >> a >> b; vr.insert(mp(a, b)); vr.insert(mp(b, a)); nvis[a].push_back(b); nvis[b].push_back(a); if (a != 1 && b != 1 && !connections.contains(mp(a, b))) { connections.insert(mp(a, b)); connections.insert(mp(b, a)); res.push_back(mp(true, mp(a, b))); } } for (int i = 1; i <= n; i++) { for (int j : vis[i]) { if (i != 1 && j != 1 && i < j && !vr.contains(mp(i, j))) { res.push_back(mp(false, mp(i, j))); } } } me_root(1); while(!ayaya.empty()) { int temp = ayaya.top(); ayaya.pop(); if (!vr.contains(mp(1, temp))) { res.push_back(mp(false, mp(1, temp))); } } cout << res.size() << "\n"; for (auto i : res) { if (i.first) { cout << "+ "; } else { cout << "- "; } cout << i.second.first << " " << i.second.second << "\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 101 102 103 | #include <bits/stdc++.h> #define MAX_N 30007 #define MAX_M 50007 #define mp make_pair using namespace std; int n, m, a, b; bool cb[MAX_N]; bool bc[MAX_N]; vector<int> vis[MAX_N]; vector<int> nvis[MAX_N]; set<pair<int, int>> vr; set<pair<int, int>> connections; stack<int> ayaya; vector<pair<int, int>> fuwafuwa; vector<pair<bool, pair<int, int>>> res; void root_me(int v) { cb[v] = true; for (int nei : vis[v]) { if (!cb[nei]) { if (!connections.contains(mp(1, nei))) { connections.insert(mp(1, nei)); connections.insert(mp(nei, 1)); res.push_back(mp(true, mp(1, nei))); } root_me(nei); } } } void me_root(int v) { bc[v] = true; for (int nei : nvis[v]) { if (!bc[nei]) { ayaya.push(nei); me_root(nei); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; cin >> m; for (int i = 1; i <= m; i++) { cin >> a >> b; vis[a].push_back(b); vis[b].push_back(a); connections.insert(mp(a, b)); connections.insert(mp(b, a)); } root_me(1); cin >> m; for (int i = 1; i <= m; i++) { cin >> a >> b; vr.insert(mp(a, b)); vr.insert(mp(b, a)); nvis[a].push_back(b); nvis[b].push_back(a); if (a != 1 && b != 1 && !connections.contains(mp(a, b))) { connections.insert(mp(a, b)); connections.insert(mp(b, a)); res.push_back(mp(true, mp(a, b))); } } for (int i = 1; i <= n; i++) { for (int j : vis[i]) { if (i != 1 && j != 1 && i < j && !vr.contains(mp(i, j))) { res.push_back(mp(false, mp(i, j))); } } } me_root(1); while(!ayaya.empty()) { int temp = ayaya.top(); ayaya.pop(); if (!vr.contains(mp(1, temp))) { res.push_back(mp(false, mp(1, temp))); } } cout << res.size() << "\n"; for (auto i : res) { if (i.first) { cout << "+ "; } else { cout << "- "; } cout << i.second.first << " " << i.second.second << "\n"; } } |