#include <iostream> #include <algorithm> #include <unordered_set> #include <vector> #define MAXN 30002 using namespace std; int n, ms, md; int a, b; vector<unordered_set<int>> vs(MAXN); vector<unordered_set<int>> vd(MAXN); vector<int> viss(MAXN), visd(MAXN); vector<int> as, ad; vector<pair<int, int>> res; int dfs(int p, vector<unordered_set<int>> &v, vector<int> &vis, vector<int> &a) { if (vis[p]) return 0; vis[p] = 1; if (!v[1].count(p)) { a.push_back(p); } for (int el : v[p]) { dfs(el, v, vis, a); } return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> ms; for (int i = 0; i < ms; i++) { cin >> a >> b; vs[a].insert(b); vs[b].insert(a); } vs[1].insert(1); dfs(1, vs, viss, as); cin >> md; for (int i = 0; i < md; i++) { cin >> a >> b; vd[a].insert(b); vd[b].insert(a); } vd[1].insert(1); dfs(1, vd, visd, ad); for (int a : as) res.push_back({1, a}); for (int i = 2; i <= n; i++) { for (int a : vs[i]) if (a > i && !vd[i].count(a)) { res.push_back({-i, -a}); } for (int a : vd[i]) if (a > i && !vs[i].count(a)) { res.push_back({i, a}); } } for (auto a = ad.rbegin(); a != ad.rend(); a++) res.push_back({-1, -*a}); cout << res.size() << "\n"; for (auto p : res) { if (p.first < 0) { cout << "- " << -p.first << " " << -p.second << "\n"; } else { 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 | #include <iostream> #include <algorithm> #include <unordered_set> #include <vector> #define MAXN 30002 using namespace std; int n, ms, md; int a, b; vector<unordered_set<int>> vs(MAXN); vector<unordered_set<int>> vd(MAXN); vector<int> viss(MAXN), visd(MAXN); vector<int> as, ad; vector<pair<int, int>> res; int dfs(int p, vector<unordered_set<int>> &v, vector<int> &vis, vector<int> &a) { if (vis[p]) return 0; vis[p] = 1; if (!v[1].count(p)) { a.push_back(p); } for (int el : v[p]) { dfs(el, v, vis, a); } return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> ms; for (int i = 0; i < ms; i++) { cin >> a >> b; vs[a].insert(b); vs[b].insert(a); } vs[1].insert(1); dfs(1, vs, viss, as); cin >> md; for (int i = 0; i < md; i++) { cin >> a >> b; vd[a].insert(b); vd[b].insert(a); } vd[1].insert(1); dfs(1, vd, visd, ad); for (int a : as) res.push_back({1, a}); for (int i = 2; i <= n; i++) { for (int a : vs[i]) if (a > i && !vd[i].count(a)) { res.push_back({-i, -a}); } for (int a : vd[i]) if (a > i && !vs[i].count(a)) { res.push_back({i, a}); } } for (auto a = ad.rbegin(); a != ad.rend(); a++) res.push_back({-1, -*a}); cout << res.size() << "\n"; for (auto p : res) { if (p.first < 0) { cout << "- " << -p.first << " " << -p.second << "\n"; } else { cout << "+ " << p.first << " " << p.second << "\n"; } } return 0; } |