#include <iostream> #include <queue> #include <set> #include <vector> using namespace std; struct operation { char sign; int b; int e; }; vector<operation> operations; struct Graph { struct E { int v; }; struct V : vector<E> { bool visited = false; }; vector<V> g; set<pair<int, int>> target_edges; Graph(int n) : g(n) {}; void add_to_graph(int b, int e, bool target) { if (target) { target_edges.insert({min(b, e), max(b, e)}); } else { g[b].push_back({e}); g[e].push_back({b}); } } void bfs() { queue<int> q; g[1].visited = true; for (auto &ve : g[1]) if (!g[ve.v].visited) { g[ve.v].visited = true; q.push(ve.v); } int v; while (!q.empty()) { v = q.front(); q.pop(); for (auto &ve : g[v]) if (!g[ve.v].visited) { g[ve.v].visited = true; q.push(ve.v); operations.push_back({'+', 1, ve.v}); add_to_graph(1, ve.v, false); } } } void clear_visited() { for (auto &v : g) v.visited = false; } void dfs(int v) { g[v].visited = true; for (auto &ve : g[v]) { if (!g[ve.v].visited && target_edges.count({min(v, ve.v), max(v, ve.v)})) { dfs(ve.v); } } if (v != 1 && !target_edges.count({1, v})) { operations.push_back({'-', 1, v}); } } }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; Graph g(n + 1); int m; cin >> m; int b, e; set<pair<int, int>> edges; for (int i = 0; i < m; i++) { cin >> b >> e; edges.insert({min(b, e), max(b, e)}); g.add_to_graph(b, e, false); } cin >> m; set<pair<int, int>> target_edges; for (int i = 0; i < m; i++) { cin >> b >> e; target_edges.insert({min(b, e), max(b, e)}); g.add_to_graph(b, e, true); } g.bfs(); for (auto &edge : target_edges) { if (!edges.count(edge) && edge.first != 1) { operations.push_back({'+', edge.first, edge.second}); g.add_to_graph(edge.first, edge.second, false); } } for (auto &edge : edges) { if (!target_edges.count(edge) && edge.first != 1) operations.push_back({'-', edge.first, edge.second}); } g.clear_visited(); g.dfs(1); cout << operations.size() << '\n'; for (auto &op : operations) { cout << op.sign << ' ' << op.b << ' ' << op.e << '\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 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 | #include <iostream> #include <queue> #include <set> #include <vector> using namespace std; struct operation { char sign; int b; int e; }; vector<operation> operations; struct Graph { struct E { int v; }; struct V : vector<E> { bool visited = false; }; vector<V> g; set<pair<int, int>> target_edges; Graph(int n) : g(n) {}; void add_to_graph(int b, int e, bool target) { if (target) { target_edges.insert({min(b, e), max(b, e)}); } else { g[b].push_back({e}); g[e].push_back({b}); } } void bfs() { queue<int> q; g[1].visited = true; for (auto &ve : g[1]) if (!g[ve.v].visited) { g[ve.v].visited = true; q.push(ve.v); } int v; while (!q.empty()) { v = q.front(); q.pop(); for (auto &ve : g[v]) if (!g[ve.v].visited) { g[ve.v].visited = true; q.push(ve.v); operations.push_back({'+', 1, ve.v}); add_to_graph(1, ve.v, false); } } } void clear_visited() { for (auto &v : g) v.visited = false; } void dfs(int v) { g[v].visited = true; for (auto &ve : g[v]) { if (!g[ve.v].visited && target_edges.count({min(v, ve.v), max(v, ve.v)})) { dfs(ve.v); } } if (v != 1 && !target_edges.count({1, v})) { operations.push_back({'-', 1, v}); } } }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; Graph g(n + 1); int m; cin >> m; int b, e; set<pair<int, int>> edges; for (int i = 0; i < m; i++) { cin >> b >> e; edges.insert({min(b, e), max(b, e)}); g.add_to_graph(b, e, false); } cin >> m; set<pair<int, int>> target_edges; for (int i = 0; i < m; i++) { cin >> b >> e; target_edges.insert({min(b, e), max(b, e)}); g.add_to_graph(b, e, true); } g.bfs(); for (auto &edge : target_edges) { if (!edges.count(edge) && edge.first != 1) { operations.push_back({'+', edge.first, edge.second}); g.add_to_graph(edge.first, edge.second, false); } } for (auto &edge : edges) { if (!target_edges.count(edge) && edge.first != 1) operations.push_back({'-', edge.first, edge.second}); } g.clear_visited(); g.dfs(1); cout << operations.size() << '\n'; for (auto &op : operations) { cout << op.sign << ' ' << op.b << ' ' << op.e << '\n'; } return 0; } |