#include <bits/stdc++.h> using namespace std; struct Node { int id; set<int> neighbours; }; struct Move { int from; int to; char op; }; vector<int> bfs(const vector<Node>& graph, int start) { vector<int> distances(graph.size(), -1); vector<int> order; distances[start] = 0; queue<int> q; q.push(start); while (!q.empty()) { int current = q.front(); q.pop(); if (distances[current] > 1) order.push_back(current); for (int neighbour : graph[current].neighbours) { if (distances[neighbour] == -1) { distances[neighbour] = distances[current] + 1; q.push(neighbour); } } } return order; } void checker(int n, set<pair<int, int>> s, set<pair<int, int>> e, vector<Move> m) { vector<Node> graph(n); for (const auto& [a, b] : s) { graph[a].neighbours.insert(b); graph[b].neighbours.insert(a); } for (const auto m: m) { if (m.op == '+') { bool found = false; for (const auto neighbour: graph[m.from].neighbours) { if (graph[m.to].neighbours.find(neighbour) != graph[m.to].neighbours.end()) { found = true; graph[m.from].neighbours.insert(m.to); graph[m.to].neighbours.insert(m.from); break; } } if (!found) { cout << "Invalid move: " << m.op << " " << m.from << " " << m.to << endl; return; } } else { bool found = false; for (const auto neighbour: graph[m.from].neighbours) { if (graph[m.to].neighbours.find(neighbour) != graph[m.to].neighbours.end()) { found = true; graph[m.from].neighbours.erase(m.to); graph[m.to].neighbours.erase(m.from); break; } } if (!found) { cout << "Invalid move: " << m.op << " " << m.from << " " << m.to << endl; return; } } } int edges = 0; for (const auto& node: graph) { edges += node.neighbours.size(); } edges /= 2; for (const auto& [a, b] : e) { if (graph[a].neighbours.find(b) == graph[a].neighbours.end()) { cout << "Not connected: " << a << " " << b << endl; return; } } if (edges != e.size()) { cout << "Invalid number of edges: " << edges << " " << e.size() << endl; return; } cout << "OK" << endl; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n; cin >> n; vector<Node> graph(n); vector<Node> graph2(n); set<pair<int, int>> starting; set<pair<int, int>> ending; vector<Move> moves; int edges; cin >> edges; for (int i = 0; i < edges; i++) { int a, b; cin >> a >> b; a--; b--; starting.insert({a, b}); graph[a].neighbours.insert(b); graph[b].neighbours.insert(a); } cin >> edges; for (int i = 0; i < edges; i++) { int a, b; cin >> a >> b; a--; b--; ending.insert({a, b}); } for (const auto b: bfs(graph, 0)) { moves.push_back({0, b, '+'}); } for (const auto& [a, b] : starting) { if (a != 0 and b != 0) { moves.push_back({a, b, '-'}); } } for (const auto& [a, b] : ending) { graph2[a].neighbours.insert(b); graph2[b].neighbours.insert(a); if (a != 0 and b != 0) { moves.push_back({b, a, '+'}); } } vector<int> order = bfs(graph2, 0); reverse(order.begin(), order.end()); for (const auto v: order) { if (ending.find({0, v}) == ending.end() and ending.find({v, 0}) == ending.end()) { moves.push_back({0, v, '-'}); } } // checker(n, starting, ending, moves); // cout << moves.size() << endl; for (const auto m: moves) { cout << m.op << " " << m.from + 1 << " " << m.to + 1 << '\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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | #include <bits/stdc++.h> using namespace std; struct Node { int id; set<int> neighbours; }; struct Move { int from; int to; char op; }; vector<int> bfs(const vector<Node>& graph, int start) { vector<int> distances(graph.size(), -1); vector<int> order; distances[start] = 0; queue<int> q; q.push(start); while (!q.empty()) { int current = q.front(); q.pop(); if (distances[current] > 1) order.push_back(current); for (int neighbour : graph[current].neighbours) { if (distances[neighbour] == -1) { distances[neighbour] = distances[current] + 1; q.push(neighbour); } } } return order; } void checker(int n, set<pair<int, int>> s, set<pair<int, int>> e, vector<Move> m) { vector<Node> graph(n); for (const auto& [a, b] : s) { graph[a].neighbours.insert(b); graph[b].neighbours.insert(a); } for (const auto m: m) { if (m.op == '+') { bool found = false; for (const auto neighbour: graph[m.from].neighbours) { if (graph[m.to].neighbours.find(neighbour) != graph[m.to].neighbours.end()) { found = true; graph[m.from].neighbours.insert(m.to); graph[m.to].neighbours.insert(m.from); break; } } if (!found) { cout << "Invalid move: " << m.op << " " << m.from << " " << m.to << endl; return; } } else { bool found = false; for (const auto neighbour: graph[m.from].neighbours) { if (graph[m.to].neighbours.find(neighbour) != graph[m.to].neighbours.end()) { found = true; graph[m.from].neighbours.erase(m.to); graph[m.to].neighbours.erase(m.from); break; } } if (!found) { cout << "Invalid move: " << m.op << " " << m.from << " " << m.to << endl; return; } } } int edges = 0; for (const auto& node: graph) { edges += node.neighbours.size(); } edges /= 2; for (const auto& [a, b] : e) { if (graph[a].neighbours.find(b) == graph[a].neighbours.end()) { cout << "Not connected: " << a << " " << b << endl; return; } } if (edges != e.size()) { cout << "Invalid number of edges: " << edges << " " << e.size() << endl; return; } cout << "OK" << endl; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n; cin >> n; vector<Node> graph(n); vector<Node> graph2(n); set<pair<int, int>> starting; set<pair<int, int>> ending; vector<Move> moves; int edges; cin >> edges; for (int i = 0; i < edges; i++) { int a, b; cin >> a >> b; a--; b--; starting.insert({a, b}); graph[a].neighbours.insert(b); graph[b].neighbours.insert(a); } cin >> edges; for (int i = 0; i < edges; i++) { int a, b; cin >> a >> b; a--; b--; ending.insert({a, b}); } for (const auto b: bfs(graph, 0)) { moves.push_back({0, b, '+'}); } for (const auto& [a, b] : starting) { if (a != 0 and b != 0) { moves.push_back({a, b, '-'}); } } for (const auto& [a, b] : ending) { graph2[a].neighbours.insert(b); graph2[b].neighbours.insert(a); if (a != 0 and b != 0) { moves.push_back({b, a, '+'}); } } vector<int> order = bfs(graph2, 0); reverse(order.begin(), order.end()); for (const auto v: order) { if (ending.find({0, v}) == ending.end() and ending.find({v, 0}) == ending.end()) { moves.push_back({0, v, '-'}); } } // checker(n, starting, ending, moves); // cout << moves.size() << endl; for (const auto m: moves) { cout << m.op << " " << m.from + 1 << " " << m.to + 1 << '\n'; } return 0; } |