#include <iostream> #include <unordered_set> #include <vector> #include <queue> #include <algorithm> #include <list> using namespace std; void bfsAndLinkToFirstVertex(vector<unordered_set<int>>& adjList, int startVertex, list<int>& connectedTo1, vector<bool>& visited) { queue<pair<int,bool>> q; visited[startVertex] = true; for (int neighbor : adjList[startVertex]) { if (!visited[neighbor]) { q.push(make_pair(neighbor, false)); visited[neighbor] = true; } } while (!q.empty()) { pair<int, bool> currentVertex = q.front(); q.pop(); if (currentVertex.second) { adjList[currentVertex.first].insert(startVertex); adjList[startVertex].insert(currentVertex.first); connectedTo1.push_back(currentVertex.first); } for (int neighbor : adjList[currentVertex.first]) { if (!visited[neighbor]) { q.push(make_pair(neighbor, true)); visited[neighbor] = true; } } } } int main() { // Process the first graph int n1, m; cin >> n1; cin >> m; vector<unordered_set<int>> adjList1(n1 + 1); for (int k = 0; k < m; ++k) { int i, j; cin >> i >> j; adjList1[i].insert(j); adjList1[j].insert(i); } list<int> connectedTo1; vector<bool> visited1(n1 + 1, false); bfsAndLinkToFirstVertex(adjList1, 1, connectedTo1, visited1); // Process the second graph vector<unordered_set<int>> adjList2(n1 + 1); vector<bool> notConnectedToVertex1(n1, true); notConnectedToVertex1[1] = false; int m2; cin >> m2; for (int k = 0; k < m2; ++k) { int i, j; cin >> i >> j; adjList2[i].insert(j); adjList2[j].insert(i); if (i == 1) { notConnectedToVertex1[j] = false; } else if (j == 1) { notConnectedToVertex1[i] = false; } } // List to store added edges list<pair<int, int>> addedEdges; // Add edges from the second graph to the first graph (avoiding duplicates) for (int i = 1; i <= n1; ++i) { for (int j : adjList2[i]) { if (adjList1[i].find(j) == adjList1[i].end()) { // Edge doesn't exist in the first graph, add it adjList1[i].insert(j); adjList1[j].insert(i); addedEdges.push_back(make_pair(i, j)); } } } // List to store deleted vertices list<int> deletedVertices; // Iterate through connectedTo1 vector from the back for (std::list<int>::reverse_iterator rit = connectedTo1.rbegin(); rit != connectedTo1.rend(); ++rit) { int currentVertex = *rit; if (notConnectedToVertex1[currentVertex]) { deletedVertices.push_back(currentVertex); } } cout << connectedTo1.size() + deletedVertices.size() + addedEdges.size() << endl; for (int vertex : connectedTo1) { cout << "+ " << "1 " << vertex << endl; } for (auto edge : addedEdges) { cout << "+ " << edge.first <<" " << edge.second << endl; } for (auto vertex : deletedVertices) { cout << "- " << "1" << " " << vertex << 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 | #include <iostream> #include <unordered_set> #include <vector> #include <queue> #include <algorithm> #include <list> using namespace std; void bfsAndLinkToFirstVertex(vector<unordered_set<int>>& adjList, int startVertex, list<int>& connectedTo1, vector<bool>& visited) { queue<pair<int,bool>> q; visited[startVertex] = true; for (int neighbor : adjList[startVertex]) { if (!visited[neighbor]) { q.push(make_pair(neighbor, false)); visited[neighbor] = true; } } while (!q.empty()) { pair<int, bool> currentVertex = q.front(); q.pop(); if (currentVertex.second) { adjList[currentVertex.first].insert(startVertex); adjList[startVertex].insert(currentVertex.first); connectedTo1.push_back(currentVertex.first); } for (int neighbor : adjList[currentVertex.first]) { if (!visited[neighbor]) { q.push(make_pair(neighbor, true)); visited[neighbor] = true; } } } } int main() { // Process the first graph int n1, m; cin >> n1; cin >> m; vector<unordered_set<int>> adjList1(n1 + 1); for (int k = 0; k < m; ++k) { int i, j; cin >> i >> j; adjList1[i].insert(j); adjList1[j].insert(i); } list<int> connectedTo1; vector<bool> visited1(n1 + 1, false); bfsAndLinkToFirstVertex(adjList1, 1, connectedTo1, visited1); // Process the second graph vector<unordered_set<int>> adjList2(n1 + 1); vector<bool> notConnectedToVertex1(n1, true); notConnectedToVertex1[1] = false; int m2; cin >> m2; for (int k = 0; k < m2; ++k) { int i, j; cin >> i >> j; adjList2[i].insert(j); adjList2[j].insert(i); if (i == 1) { notConnectedToVertex1[j] = false; } else if (j == 1) { notConnectedToVertex1[i] = false; } } // List to store added edges list<pair<int, int>> addedEdges; // Add edges from the second graph to the first graph (avoiding duplicates) for (int i = 1; i <= n1; ++i) { for (int j : adjList2[i]) { if (adjList1[i].find(j) == adjList1[i].end()) { // Edge doesn't exist in the first graph, add it adjList1[i].insert(j); adjList1[j].insert(i); addedEdges.push_back(make_pair(i, j)); } } } // List to store deleted vertices list<int> deletedVertices; // Iterate through connectedTo1 vector from the back for (std::list<int>::reverse_iterator rit = connectedTo1.rbegin(); rit != connectedTo1.rend(); ++rit) { int currentVertex = *rit; if (notConnectedToVertex1[currentVertex]) { deletedVertices.push_back(currentVertex); } } cout << connectedTo1.size() + deletedVertices.size() + addedEdges.size() << endl; for (int vertex : connectedTo1) { cout << "+ " << "1 " << vertex << endl; } for (auto edge : addedEdges) { cout << "+ " << edge.first <<" " << edge.second << endl; } for (auto vertex : deletedVertices) { cout << "- " << "1" << " " << vertex << endl; } return 0; } |