#include <bits/stdc++.h> using namespace std; bool isLeftLess(pair<int, int> & l, pair<int, int> & r) { int firstLength, secondLength; if(l.second > l.first) firstLength = l.second-l.first; else firstLength = l.first-l.second; if(r.second > r.first) secondLength = r.second-r.first; else secondLength = r.first-r.second; return firstLength < secondLength; } void theShortestPath(vector<vector<int>> & graph, int start, int dest, stack<int> & result) { vector<int> parents(graph.size(), -1); queue<int> bfsQue; bfsQue.push(start); parents[start] = 0; int node; bool found = false; while(bfsQue.empty() == false) { node = bfsQue.front(); bfsQue.pop(); for(auto neighbour : graph[node]) { if(parents[neighbour] == -1) { parents[neighbour] = node; if(neighbour == dest) {found = true; break;} bfsQue.push(neighbour); } } if(found) break; } int t = dest; while(t != 0) { result.push(t); t = parents[t]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<vector<int>> graph(n+1); set<pair<int, int>> edgesIHave; vector<pair<int, int>> edgesIMiss; // edges i have int e; cin >> e; for(int i {}, a, b; i < e; ++i) { cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); edgesIHave.insert(pair<int,int>(a,b)); edgesIHave.insert(pair<int, int>(b, a)); } // edges i miss cin >> e; for(int i{}, a, b; i < e; ++i) { cin >> a >> b; // check if i have this edge if(edgesIHave.find(pair<int, int>(a, b)) != edgesIHave.end() || edgesIHave.find(pair<int, int>(b, a)) != edgesIHave.end()) continue; else // if not add to edgesIMiss edgesIMiss.push_back(pair<int, int>(a, b)); } // iterate through edges i miss, find the shortest path in the graph, add all vertices in the path to the que, // use the algorithm sort(edgesIMiss.begin(), edgesIMiss.end(), isLeftLess); queue<pair<int, int>> added, toDelete; for(int i {}; i < edgesIMiss.size(); ++i) { stack<int> path, secondPath; theShortestPath(graph, edgesIMiss[i].first, edgesIMiss[i].second, path); int first; while(true) { // get second path first = true; while(path.empty() == false) { if(first) { secondPath.push(path.top()); first = false; } else first = true; path.pop(); } // add edges from second path if(secondPath.size() == 2) { int first = secondPath.top(); secondPath.pop(); int second = secondPath.top(); secondPath.pop(); if((first == edgesIMiss[i].first && second == edgesIMiss[i].second) || ( first == edgesIMiss[i].second && second == edgesIMiss[i].first)) { added.push(pair<int, int>(first, second)); graph[first].push_back(second); graph[second].push_back(first); } else { graph[first].push_back(second); graph[second].push_back(first); added.push(pair<int, int>(first, second)); toDelete.push(pair<int, int>(first,second)); graph[edgesIMiss[i].first].push_back(edgesIMiss[i].second); graph[edgesIMiss[i].second].push_back(edgesIMiss[i].first); added.push(pair<int, int>(edgesIMiss[i].first, edgesIMiss[i].second)); } break; } stack<int> tempPath = secondPath; int last = tempPath.top(); tempPath.pop(); first = false; while(tempPath.empty() == false) { added.push(pair<int, int>(last, tempPath.top())); toDelete.push(pair<int, int>(last, tempPath.top())); graph[last].push_back(tempPath.top()); graph[tempPath.top()].push_back(last); last = tempPath.top(); tempPath.pop(); } // change path to second path and repeat proccess path = secondPath; while(secondPath.empty() == false) secondPath.pop(); } } cout << added.size() + toDelete.size() << '\n'; while(added.empty() == false) { cout << "+ " << added.front().first << " " << added.front().second << '\n'; added.pop(); } while(toDelete.empty() == false) { cout << "- " << toDelete.front().first << " " << toDelete.front().second << '\n'; toDelete.pop(); } }
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | #include <bits/stdc++.h> using namespace std; bool isLeftLess(pair<int, int> & l, pair<int, int> & r) { int firstLength, secondLength; if(l.second > l.first) firstLength = l.second-l.first; else firstLength = l.first-l.second; if(r.second > r.first) secondLength = r.second-r.first; else secondLength = r.first-r.second; return firstLength < secondLength; } void theShortestPath(vector<vector<int>> & graph, int start, int dest, stack<int> & result) { vector<int> parents(graph.size(), -1); queue<int> bfsQue; bfsQue.push(start); parents[start] = 0; int node; bool found = false; while(bfsQue.empty() == false) { node = bfsQue.front(); bfsQue.pop(); for(auto neighbour : graph[node]) { if(parents[neighbour] == -1) { parents[neighbour] = node; if(neighbour == dest) {found = true; break;} bfsQue.push(neighbour); } } if(found) break; } int t = dest; while(t != 0) { result.push(t); t = parents[t]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<vector<int>> graph(n+1); set<pair<int, int>> edgesIHave; vector<pair<int, int>> edgesIMiss; // edges i have int e; cin >> e; for(int i {}, a, b; i < e; ++i) { cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); edgesIHave.insert(pair<int,int>(a,b)); edgesIHave.insert(pair<int, int>(b, a)); } // edges i miss cin >> e; for(int i{}, a, b; i < e; ++i) { cin >> a >> b; // check if i have this edge if(edgesIHave.find(pair<int, int>(a, b)) != edgesIHave.end() || edgesIHave.find(pair<int, int>(b, a)) != edgesIHave.end()) continue; else // if not add to edgesIMiss edgesIMiss.push_back(pair<int, int>(a, b)); } // iterate through edges i miss, find the shortest path in the graph, add all vertices in the path to the que, // use the algorithm sort(edgesIMiss.begin(), edgesIMiss.end(), isLeftLess); queue<pair<int, int>> added, toDelete; for(int i {}; i < edgesIMiss.size(); ++i) { stack<int> path, secondPath; theShortestPath(graph, edgesIMiss[i].first, edgesIMiss[i].second, path); int first; while(true) { // get second path first = true; while(path.empty() == false) { if(first) { secondPath.push(path.top()); first = false; } else first = true; path.pop(); } // add edges from second path if(secondPath.size() == 2) { int first = secondPath.top(); secondPath.pop(); int second = secondPath.top(); secondPath.pop(); if((first == edgesIMiss[i].first && second == edgesIMiss[i].second) || ( first == edgesIMiss[i].second && second == edgesIMiss[i].first)) { added.push(pair<int, int>(first, second)); graph[first].push_back(second); graph[second].push_back(first); } else { graph[first].push_back(second); graph[second].push_back(first); added.push(pair<int, int>(first, second)); toDelete.push(pair<int, int>(first,second)); graph[edgesIMiss[i].first].push_back(edgesIMiss[i].second); graph[edgesIMiss[i].second].push_back(edgesIMiss[i].first); added.push(pair<int, int>(edgesIMiss[i].first, edgesIMiss[i].second)); } break; } stack<int> tempPath = secondPath; int last = tempPath.top(); tempPath.pop(); first = false; while(tempPath.empty() == false) { added.push(pair<int, int>(last, tempPath.top())); toDelete.push(pair<int, int>(last, tempPath.top())); graph[last].push_back(tempPath.top()); graph[tempPath.top()].push_back(last); last = tempPath.top(); tempPath.pop(); } // change path to second path and repeat proccess path = secondPath; while(secondPath.empty() == false) secondPath.pop(); } } cout << added.size() + toDelete.size() << '\n'; while(added.empty() == false) { cout << "+ " << added.front().first << " " << added.front().second << '\n'; added.pop(); } while(toDelete.empty() == false) { cout << "- " << toDelete.front().first << " " << toDelete.front().second << '\n'; toDelete.pop(); } } |