#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long> vll; const ll LINF = 1e18; const int INF = 1e9; const bool CHECK = false; typedef vector<unordered_set<int>> Graph; struct Operation { char op; int a; int b; }; vector<int> bfs(int start, Graph &g) { queue<int> q; vector<bool> visited(g.size()); vector<int> order; q.push(start); visited[start] = true; while (!q.empty()) { int v = q.front(); q.pop(); for (int next: g[v]) { if (!visited[next]) { q.push(next); visited[next] = true; if (v != start) { order.push_back(next); } } } } return order; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m_start, m_final; int a, b; cin >> n; vector<unordered_set<int>> g_start(n + 1); vector<unordered_set<int>> g_final(n + 1); cin >> m_start; for (int i = 0; i < m_start; ++i) { cin >> a >> b; g_start[a].insert(b); g_start[b].insert(a); } cin >> m_final; for (int i = 0; i < m_final; ++i) { cin >> a >> b; g_final[a].insert(b); g_final[b].insert(a); } auto order_start = bfs(1, g_start); auto order_final = bfs(1, g_final); vector<Operation> ops; for (size_t i = 0; i < order_start.size(); ++i) { ops.push_back({'+', 1, order_start[i]}); } for (int i = 2; i <= n; ++i) { for (int next: g_start[i]) { if (i < next && next != 1 && g_final[i].find(next) == g_final[i].end()) { ops.push_back({'-', i, next}); } } } for (int i = 2; i <= n; ++i) { for (int next: g_final[i]) { if (i < next && next != 1 && g_start[i].find(next) == g_start[i].end()) { ops.push_back({'+', i, next}); } } } for (int i = order_final.size() - 1; i >= 0; --i) { ops.push_back({'-', 1, order_final[i]}); } cout << ops.size() << '\n'; // Graph g_check; // if (CHECK) { // g_check = g_start; // } for (size_t i = 0; i < ops.size(); ++i) { Operation &o = ops[i]; cout << o.op << ' ' << o.a << ' ' << o.b << '\n'; // if (CHECK) { // if (o.op == '+') { // g_check[o.a].insert(o.b); // g_check[o.b].insert(o.a); // } else { // g_check[o.a].erase(o.b); // g_check[o.b].erase(o.a); // } // } } // if (CHECK) { // if (g_check == g_final) { // cerr << "\033[32;1mOK\033[0m" << endl; // } else { // cerr << "\033[31;1mWRONG ANSWER\033[0m" << 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long> vll; const ll LINF = 1e18; const int INF = 1e9; const bool CHECK = false; typedef vector<unordered_set<int>> Graph; struct Operation { char op; int a; int b; }; vector<int> bfs(int start, Graph &g) { queue<int> q; vector<bool> visited(g.size()); vector<int> order; q.push(start); visited[start] = true; while (!q.empty()) { int v = q.front(); q.pop(); for (int next: g[v]) { if (!visited[next]) { q.push(next); visited[next] = true; if (v != start) { order.push_back(next); } } } } return order; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m_start, m_final; int a, b; cin >> n; vector<unordered_set<int>> g_start(n + 1); vector<unordered_set<int>> g_final(n + 1); cin >> m_start; for (int i = 0; i < m_start; ++i) { cin >> a >> b; g_start[a].insert(b); g_start[b].insert(a); } cin >> m_final; for (int i = 0; i < m_final; ++i) { cin >> a >> b; g_final[a].insert(b); g_final[b].insert(a); } auto order_start = bfs(1, g_start); auto order_final = bfs(1, g_final); vector<Operation> ops; for (size_t i = 0; i < order_start.size(); ++i) { ops.push_back({'+', 1, order_start[i]}); } for (int i = 2; i <= n; ++i) { for (int next: g_start[i]) { if (i < next && next != 1 && g_final[i].find(next) == g_final[i].end()) { ops.push_back({'-', i, next}); } } } for (int i = 2; i <= n; ++i) { for (int next: g_final[i]) { if (i < next && next != 1 && g_start[i].find(next) == g_start[i].end()) { ops.push_back({'+', i, next}); } } } for (int i = order_final.size() - 1; i >= 0; --i) { ops.push_back({'-', 1, order_final[i]}); } cout << ops.size() << '\n'; // Graph g_check; // if (CHECK) { // g_check = g_start; // } for (size_t i = 0; i < ops.size(); ++i) { Operation &o = ops[i]; cout << o.op << ' ' << o.a << ' ' << o.b << '\n'; // if (CHECK) { // if (o.op == '+') { // g_check[o.a].insert(o.b); // g_check[o.b].insert(o.a); // } else { // g_check[o.a].erase(o.b); // g_check[o.b].erase(o.a); // } // } } // if (CHECK) { // if (g_check == g_final) { // cerr << "\033[32;1mOK\033[0m" << endl; // } else { // cerr << "\033[31;1mWRONG ANSWER\033[0m" << endl; // } // } return 0; } |