#include <bits/stdc++.h> using namespace std; #ifdef D #define DEBUG(x) \ do { \ x \ cout.flush(); \ } while (0) #else #define DEBUG(x) #endif using ll = long long; using pii = pair<int, int>; const int NMAX = 3e4 + 7; int n, a, b, m1, m2; set<pii> s, t, to_add, to_remove; set<int> S; vector<int> g[NMAX]; vector<string> res; bool vis[NMAX]; bool vis2[NMAX]; void dfs1(int v) { vis[v] = true; if (v != 1 && !s.contains({1, v})) { res.push_back("+ " + to_string(1) + " " + to_string(v) + "\n"); } for (int u : g[v]) { if (!vis[u]) { dfs1(u); } } } void dfs2(int v) { vis2[v] = true; for (int u : g[v]) { if (!vis2[u]) { dfs2(u); } } if (v != 1 && !t.contains({1, v})) { res.push_back("- " + to_string(1) + " " + to_string(v) + "\n"); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m1; for (int i = 0; i < m1; ++i) { cin >> a >> b; if (b < a) { swap(a, b); } s.emplace(a, b); g[a].push_back(b); g[b].push_back(a); } cin >> m2; for (int i = 0; i < m2; ++i) { cin >> a >> b; if (b < a) { swap(a, b); } t.emplace(a, b); } ranges::set_difference(t, s, std::inserter(to_add, to_add.begin())); ranges::set_difference(s, t, std::inserter(to_remove, to_remove.begin())); if (to_add.empty() && to_remove.empty()) { cout << "0\n"; return 0; } for (int i = 2; i <= n; ++i) { to_add.erase({1, i}); to_remove.erase({1, i}); } dfs1(1); for (auto &[x, y] : to_add) { res.push_back("+ " + to_string(x) + " " + to_string(y) + "\n"); } for (auto &[x, y] : to_remove) { res.push_back("- " + to_string(x) + " " + to_string(y) + "\n"); } dfs2(1); cout << res.size() << "\n"; for (const string &s : res) { cout << s; } 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 109 | #include <bits/stdc++.h> using namespace std; #ifdef D #define DEBUG(x) \ do { \ x \ cout.flush(); \ } while (0) #else #define DEBUG(x) #endif using ll = long long; using pii = pair<int, int>; const int NMAX = 3e4 + 7; int n, a, b, m1, m2; set<pii> s, t, to_add, to_remove; set<int> S; vector<int> g[NMAX]; vector<string> res; bool vis[NMAX]; bool vis2[NMAX]; void dfs1(int v) { vis[v] = true; if (v != 1 && !s.contains({1, v})) { res.push_back("+ " + to_string(1) + " " + to_string(v) + "\n"); } for (int u : g[v]) { if (!vis[u]) { dfs1(u); } } } void dfs2(int v) { vis2[v] = true; for (int u : g[v]) { if (!vis2[u]) { dfs2(u); } } if (v != 1 && !t.contains({1, v})) { res.push_back("- " + to_string(1) + " " + to_string(v) + "\n"); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m1; for (int i = 0; i < m1; ++i) { cin >> a >> b; if (b < a) { swap(a, b); } s.emplace(a, b); g[a].push_back(b); g[b].push_back(a); } cin >> m2; for (int i = 0; i < m2; ++i) { cin >> a >> b; if (b < a) { swap(a, b); } t.emplace(a, b); } ranges::set_difference(t, s, std::inserter(to_add, to_add.begin())); ranges::set_difference(s, t, std::inserter(to_remove, to_remove.begin())); if (to_add.empty() && to_remove.empty()) { cout << "0\n"; return 0; } for (int i = 2; i <= n; ++i) { to_add.erase({1, i}); to_remove.erase({1, i}); } dfs1(1); for (auto &[x, y] : to_add) { res.push_back("+ " + to_string(x) + " " + to_string(y) + "\n"); } for (auto &[x, y] : to_remove) { res.push_back("- " + to_string(x) + " " + to_string(y) + "\n"); } dfs2(1); cout << res.size() << "\n"; for (const string &s : res) { cout << s; } return 0; } |