#include <iostream> #include <vector> #include <set> #include <utility> using namespace std; int main() { ios_base::sync_with_stdio(0); int n, ms, md; cin >> n >> ms; vector<vector<int>> g(n + 1); set<pair<int, int>> kk,kk2; set<int> k1, k12, k12c; for (int i = 0; i < ms; ++i) { int a, b; cin >> a >> b; if (a > b) swap(a, b); g[a].push_back(b); g[b].push_back(a); if (a == 1) k1.insert(b); else kk.insert({a, b}); } cin >> md; vector<vector<int>> g2(n + 1); for (int i = 0; i < md; ++i) { int a, b; cin >> a >> b; if (a > b) swap(a, b); g2[a].push_back(b); g2[b].push_back(a); if (a == 1) { k12.insert(b); k12c.insert(b); } else kk2.insert({a,b}); } vector<string> ans; int i1 = 0; while (k1.size() < n - 1) { int v = g[1][i1]; for (int u : g[v]) { if (u != 1 && k1.find(u) == k1.end()) { k1.insert(u); g[1].push_back(u); g[u].push_back(1); ans.push_back("+ 1 " + to_string(u)); } } ++i1; } i1 = 0; while (k12.size() < n - 1) { int v = g2[1][i1]; for (int u : g2[v]) { if (u != 1 && k12.find(u) == k12.end()) { k12.insert(u); g2[1].push_back(u); g2[u].push_back(1); } } ++i1; } for (auto i : kk2) { if (kk.find(i) == kk.end()) ans.push_back("+ " + to_string(i.first) + " " + to_string(i.second)); } for (auto i : kk) { //cout << i.first<<","<<i.second<<endl; if (kk2.find(i) == kk2.end()) ans.push_back("- " + to_string(i.first) + " " + to_string(i.second)); } for (auto it = g2[1].rbegin(); it != g2[1].rend(); ++it) { if (k12c.find(*it) == k12c.end()) ans.push_back("- 1 " + to_string(*it)); } cout << ans.size() << endl; for (const string &a : ans) { cout << a << endl; } 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 | #include <iostream> #include <vector> #include <set> #include <utility> using namespace std; int main() { ios_base::sync_with_stdio(0); int n, ms, md; cin >> n >> ms; vector<vector<int>> g(n + 1); set<pair<int, int>> kk,kk2; set<int> k1, k12, k12c; for (int i = 0; i < ms; ++i) { int a, b; cin >> a >> b; if (a > b) swap(a, b); g[a].push_back(b); g[b].push_back(a); if (a == 1) k1.insert(b); else kk.insert({a, b}); } cin >> md; vector<vector<int>> g2(n + 1); for (int i = 0; i < md; ++i) { int a, b; cin >> a >> b; if (a > b) swap(a, b); g2[a].push_back(b); g2[b].push_back(a); if (a == 1) { k12.insert(b); k12c.insert(b); } else kk2.insert({a,b}); } vector<string> ans; int i1 = 0; while (k1.size() < n - 1) { int v = g[1][i1]; for (int u : g[v]) { if (u != 1 && k1.find(u) == k1.end()) { k1.insert(u); g[1].push_back(u); g[u].push_back(1); ans.push_back("+ 1 " + to_string(u)); } } ++i1; } i1 = 0; while (k12.size() < n - 1) { int v = g2[1][i1]; for (int u : g2[v]) { if (u != 1 && k12.find(u) == k12.end()) { k12.insert(u); g2[1].push_back(u); g2[u].push_back(1); } } ++i1; } for (auto i : kk2) { if (kk.find(i) == kk.end()) ans.push_back("+ " + to_string(i.first) + " " + to_string(i.second)); } for (auto i : kk) { //cout << i.first<<","<<i.second<<endl; if (kk2.find(i) == kk2.end()) ans.push_back("- " + to_string(i.first) + " " + to_string(i.second)); } for (auto it = g2[1].rbegin(); it != g2[1].rend(); ++it) { if (k12c.find(*it) == k12c.end()) ans.push_back("- 1 " + to_string(*it)); } cout << ans.size() << endl; for (const string &a : ans) { cout << a << endl; } return 0; } |