#include <bits/stdc++.h> #define pb push_back #define st first #define nd second using namespace std; typedef long long ll; typedef pair<ll, ll> pll; const int MAX_N = 3e5; ll T[MAX_N]; void bfs(vector<vector<ll> > &v, int s, pll removedEdge = {-1, -1}) { for (int i = 0; i < v.size(); ++i) { T[i] = -1; } T[s] = 0; queue<ll> q; q.push(s); while(!q.empty()) { ll vec = q.front(); q.pop(); for (auto n: v[vec]) { if (make_pair(vec, n) == removedEdge || make_pair(n, vec) == removedEdge) continue; if (T[n] == -1) { T[n] = T[vec] + 1; q.push(n); } } } } vector<pair<char, pll> > result, totalResult; void walk(vector<vector<ll> > &v, ll s, int parent = -1, int added = -1, pll removedEdge = {-1, -1}) { if (T[s] == 0) return; for (auto n: v[s]) { if (make_pair(s, n) == removedEdge || make_pair(n, s) == removedEdge) continue; if (T[n] < T[s]) { if (parent != -1) { result.pb({'+', { parent + 1, n + 1 }}); if (added != -1) { result.pb({'-', { parent + 1, added + 1 }}); } added = n; } else parent = s; walk(v, n, parent, added, removedEdge); return; } } } void addEdge(vector<vector<ll> > &v, pll edge) { bfs(v, edge.nd); result.clear(); walk(v, edge.st); for (pair<char, pll> p: result) { totalResult.pb({ p.st, p.nd }); } v[edge.st].pb(edge.nd); v[edge.nd].pb(edge.st); } void removeEdge(vector<vector<ll> > &v, pll edge) { bfs(v, edge.nd, edge); result.clear(); walk(v, edge.st, -1, -1, edge); reverse(result.begin(), result.end()); for (pair<char, pll> p: result) { totalResult.pb({(p.st == '+' ? '-' : '+'), p.nd}); } for (int i = 0; i < v[edge.st].size(); ++i) { if (v[edge.st][i] == edge.nd) { v[edge.st].erase(v[edge.st].begin() + i); break; } } for (int i = 0; i < v[edge.nd].size(); ++i) { if (v[edge.nd][i] == edge.st) { v[edge.nd].erase(v[edge.nd].begin() + i); break; } } } int main() { ios_base::sync_with_stdio(0); ll n, m, m2; cin >> n >> m; vector<vector<ll> > v(n), v2(n); set<pll> edges; ll a, b; for (int i = 0; i < m; ++i) { cin >> a >> b; a--; b--; v[a].pb(b); v[b].pb(a); edges.insert({min(a, b), max(a, b)}); } cin >> m2; vector<pll> missing; for (int i = 0; i < m2; ++i) { cin >> a >> b; a--; b--; v2[a].pb(b); v2[b].pb(a); pll edge = {min(a, b), max(a, b)}; if (edges.find(edge) == edges.end()) { missing.pb(edge); } else { edges.erase(edge); } } for (auto e: missing) { addEdge(v, e); } for (auto e: edges) { removeEdge(v, e); } cout << totalResult.size() << endl; for (pair<char, pll> p: totalResult) { cout << p.st << ' ' << p.nd.st << ' ' << p.nd.nd << endl; } }
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 | #include <bits/stdc++.h> #define pb push_back #define st first #define nd second using namespace std; typedef long long ll; typedef pair<ll, ll> pll; const int MAX_N = 3e5; ll T[MAX_N]; void bfs(vector<vector<ll> > &v, int s, pll removedEdge = {-1, -1}) { for (int i = 0; i < v.size(); ++i) { T[i] = -1; } T[s] = 0; queue<ll> q; q.push(s); while(!q.empty()) { ll vec = q.front(); q.pop(); for (auto n: v[vec]) { if (make_pair(vec, n) == removedEdge || make_pair(n, vec) == removedEdge) continue; if (T[n] == -1) { T[n] = T[vec] + 1; q.push(n); } } } } vector<pair<char, pll> > result, totalResult; void walk(vector<vector<ll> > &v, ll s, int parent = -1, int added = -1, pll removedEdge = {-1, -1}) { if (T[s] == 0) return; for (auto n: v[s]) { if (make_pair(s, n) == removedEdge || make_pair(n, s) == removedEdge) continue; if (T[n] < T[s]) { if (parent != -1) { result.pb({'+', { parent + 1, n + 1 }}); if (added != -1) { result.pb({'-', { parent + 1, added + 1 }}); } added = n; } else parent = s; walk(v, n, parent, added, removedEdge); return; } } } void addEdge(vector<vector<ll> > &v, pll edge) { bfs(v, edge.nd); result.clear(); walk(v, edge.st); for (pair<char, pll> p: result) { totalResult.pb({ p.st, p.nd }); } v[edge.st].pb(edge.nd); v[edge.nd].pb(edge.st); } void removeEdge(vector<vector<ll> > &v, pll edge) { bfs(v, edge.nd, edge); result.clear(); walk(v, edge.st, -1, -1, edge); reverse(result.begin(), result.end()); for (pair<char, pll> p: result) { totalResult.pb({(p.st == '+' ? '-' : '+'), p.nd}); } for (int i = 0; i < v[edge.st].size(); ++i) { if (v[edge.st][i] == edge.nd) { v[edge.st].erase(v[edge.st].begin() + i); break; } } for (int i = 0; i < v[edge.nd].size(); ++i) { if (v[edge.nd][i] == edge.st) { v[edge.nd].erase(v[edge.nd].begin() + i); break; } } } int main() { ios_base::sync_with_stdio(0); ll n, m, m2; cin >> n >> m; vector<vector<ll> > v(n), v2(n); set<pll> edges; ll a, b; for (int i = 0; i < m; ++i) { cin >> a >> b; a--; b--; v[a].pb(b); v[b].pb(a); edges.insert({min(a, b), max(a, b)}); } cin >> m2; vector<pll> missing; for (int i = 0; i < m2; ++i) { cin >> a >> b; a--; b--; v2[a].pb(b); v2[b].pb(a); pll edge = {min(a, b), max(a, b)}; if (edges.find(edge) == edges.end()) { missing.pb(edge); } else { edges.erase(edge); } } for (auto e: missing) { addEdge(v, e); } for (auto e: edges) { removeEdge(v, e); } cout << totalResult.size() << endl; for (pair<char, pll> p: totalResult) { cout << p.st << ' ' << p.nd.st << ' ' << p.nd.nd << endl; } } |