#include <iostream> #include <vector> #include <unordered_set> #include <sstream> using namespace std; // Struktura reprezentująca krawędź grafu struct Edge { int from; int to; Edge(int f, int t) : from(f), to(t) {} }; // Struktura reprezentująca ruch struct Move { char op; int from; int to; Move(char o, int f, int t) : op(o), from(f), to(t) {} }; // Funkcja sprawdzająca, czy dwa wierzchołki są połączone krawędzią bool isConnected(const vector<Edge>& graph, int a, int b) { for (const auto& edge : graph) { if ((edge.from == a && edge.to == b) || (edge.from == b && edge.to == a)) { return true; } } return false; } // Funkcja sprawdzająca, czy dany wierzchołek jest połączony z wierzchołkami a i b bool isVertexConnectedWith(const vector<Edge>& graph, int vertex, int a, int b) { for (const auto& edge : graph) { if ((edge.from == vertex && (edge.to == a || edge.to == b)) || (edge.to == vertex && (edge.from == a || edge.from == b))) { return true; } } return false; } // Funkcja przekształcająca graf A w graf B i zapisująca potrzebne ruchy vector<Move> transformGraph(const vector<Edge>& graphA, const vector<Edge>& graphB, int n) { vector<Move> moves; vector<Edge> currentGraph = graphA; // Dodajemy brakujące krawędzie for (int a = 1; a <= n; ++a) { for (int b = a + 1; b <= n; ++b) { if (!isConnected(currentGraph, a, b)) { bool hasCommonVertex = false; for (int c = 1; c <= n; ++c) { if (c != a && c != b && isVertexConnectedWith(currentGraph, c, a, b)) { hasCommonVertex = true; currentGraph.emplace_back(a, b); moves.emplace_back('+', a, b); break; } } if (!hasCommonVertex) { for (const auto& edge : currentGraph) { if (edge.from == a && edge.to != b && !isConnected(currentGraph, b, edge.to)) { currentGraph.emplace_back(a, b); moves.emplace_back('+', a, b); break; } if (edge.to == a && edge.from != b && !isConnected(currentGraph, b, edge.from)) { currentGraph.emplace_back(a, b); moves.emplace_back('+', a, b); break; } if (edge.from == b && edge.to != a && !isConnected(currentGraph, a, edge.to)) { currentGraph.emplace_back(a, b); moves.emplace_back('+', a, b); break; } if (edge.to == b && edge.from != a && !isConnected(currentGraph, a, edge.from)) { currentGraph.emplace_back(a, b); moves.emplace_back('+', a, b); break; } } } } } } // Usuwamy nadmiarowe krawędzie for (auto it = currentGraph.begin(); it != currentGraph.end();) { const Edge& edgeA = *it; int from = edgeA.from; int to = edgeA.to; bool found = false; for (const auto& edgeB : graphB) { if ((edgeB.from == from && edgeB.to == to) || (edgeB.from == to && edgeB.to == from)) { found = true; break; } } if (!found) { moves.emplace_back('-', from, to); it = currentGraph.erase(it); } else { ++it; } } return moves; } int n, a, u, v; int main() { vector<Edge> graphA; vector<Edge> graphB; cin >> n; cin >> a; for (int i = 0; i < a; i++) { cin >> u >> v; graphA.push_back({ u, v }); } cin >> a; for (int i = 0; i < a; i++) { cin >> u >> v; graphB.push_back({ u, v }); } vector<Move> moves = transformGraph(graphA, graphB, n); for (const auto& move : moves) { cout << move.op << " " << move.from << " " << move.to << "\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 | #include <iostream> #include <vector> #include <unordered_set> #include <sstream> using namespace std; // Struktura reprezentująca krawędź grafu struct Edge { int from; int to; Edge(int f, int t) : from(f), to(t) {} }; // Struktura reprezentująca ruch struct Move { char op; int from; int to; Move(char o, int f, int t) : op(o), from(f), to(t) {} }; // Funkcja sprawdzająca, czy dwa wierzchołki są połączone krawędzią bool isConnected(const vector<Edge>& graph, int a, int b) { for (const auto& edge : graph) { if ((edge.from == a && edge.to == b) || (edge.from == b && edge.to == a)) { return true; } } return false; } // Funkcja sprawdzająca, czy dany wierzchołek jest połączony z wierzchołkami a i b bool isVertexConnectedWith(const vector<Edge>& graph, int vertex, int a, int b) { for (const auto& edge : graph) { if ((edge.from == vertex && (edge.to == a || edge.to == b)) || (edge.to == vertex && (edge.from == a || edge.from == b))) { return true; } } return false; } // Funkcja przekształcająca graf A w graf B i zapisująca potrzebne ruchy vector<Move> transformGraph(const vector<Edge>& graphA, const vector<Edge>& graphB, int n) { vector<Move> moves; vector<Edge> currentGraph = graphA; // Dodajemy brakujące krawędzie for (int a = 1; a <= n; ++a) { for (int b = a + 1; b <= n; ++b) { if (!isConnected(currentGraph, a, b)) { bool hasCommonVertex = false; for (int c = 1; c <= n; ++c) { if (c != a && c != b && isVertexConnectedWith(currentGraph, c, a, b)) { hasCommonVertex = true; currentGraph.emplace_back(a, b); moves.emplace_back('+', a, b); break; } } if (!hasCommonVertex) { for (const auto& edge : currentGraph) { if (edge.from == a && edge.to != b && !isConnected(currentGraph, b, edge.to)) { currentGraph.emplace_back(a, b); moves.emplace_back('+', a, b); break; } if (edge.to == a && edge.from != b && !isConnected(currentGraph, b, edge.from)) { currentGraph.emplace_back(a, b); moves.emplace_back('+', a, b); break; } if (edge.from == b && edge.to != a && !isConnected(currentGraph, a, edge.to)) { currentGraph.emplace_back(a, b); moves.emplace_back('+', a, b); break; } if (edge.to == b && edge.from != a && !isConnected(currentGraph, a, edge.from)) { currentGraph.emplace_back(a, b); moves.emplace_back('+', a, b); break; } } } } } } // Usuwamy nadmiarowe krawędzie for (auto it = currentGraph.begin(); it != currentGraph.end();) { const Edge& edgeA = *it; int from = edgeA.from; int to = edgeA.to; bool found = false; for (const auto& edgeB : graphB) { if ((edgeB.from == from && edgeB.to == to) || (edgeB.from == to && edgeB.to == from)) { found = true; break; } } if (!found) { moves.emplace_back('-', from, to); it = currentGraph.erase(it); } else { ++it; } } return moves; } int n, a, u, v; int main() { vector<Edge> graphA; vector<Edge> graphB; cin >> n; cin >> a; for (int i = 0; i < a; i++) { cin >> u >> v; graphA.push_back({ u, v }); } cin >> a; for (int i = 0; i < a; i++) { cin >> u >> v; graphB.push_back({ u, v }); } vector<Move> moves = transformGraph(graphA, graphB, n); for (const auto& move : moves) { cout << move.op << " " << move.from << " " << move.to << "\n"; } return 0; } |