#include <algorithm> #include <bits/stdc++.h> #include <iterator> using namespace std; struct op { char type; int x, y; }; vector<op> fix_seq; struct graph { int n, m; vector<vector<int>> v; void input() { cin >> m; v.resize(n); for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; add_edge(x - 1, y - 1); } } void add_edge(const int x, const int y) { v[x].push_back(y); v[y].push_back(x); } }; void print_edge_added(const int x, const int y) { fix_seq.push_back({'+', x, y}); } void print_edge_removed(const int x, const int y) { fix_seq.push_back({'-', x, y}); } graph connect_all(const graph &g, const int x) { graph res = g; vector<int> dist(g.n, -1); dist[x] = 0; queue<int> q{{x}}; while (!q.empty()) { int top = q.front(); q.pop(); if (dist[top] > 1) { res.add_edge(x, top); print_edge_added(x, top); } for (const int u : g.v[top]) { if (dist[u] >= 0) continue; dist[u] = dist[top] + 1; q.push(u); } } return res; } vector<pair<int, int>> get_nonincindent(const graph &g, const int forb) { vector<pair<int, int>> res; for (int x = 0; x < g.n; ++x) { if (x == forb) continue; for (int u : g.v[x]) { if (u == forb) continue; if (x < u) res.emplace_back(x, u); } } return res; } void fix_nonincident(const graph &g, const graph &target, const int forb) { vector<pair<int, int>> g_edges = get_nonincindent(g, forb); vector<pair<int, int>> target_edges = get_nonincindent(target, forb); sort(g_edges.begin(), g_edges.end()); sort(target_edges.begin(), target_edges.end()); vector<pair<int, int>> to_add; set_difference(target_edges.begin(), target_edges.end(), g_edges.begin(), g_edges.end(), back_inserter(to_add)); for (const auto &[x, y] : to_add) { print_edge_added(x, y); } vector<pair<int, int>> to_remove; set_difference(g_edges.begin(), g_edges.end(), target_edges.begin(), target_edges.end(), back_inserter(to_remove)); for (const auto &[x, y] : to_remove) { print_edge_removed(x, y); } } void fix_incident(const graph &g, const int x) { vector<int> dist(g.n, -1); dist[x] = 0; queue<int> q{{x}}; stack<int> to_remove; while (!q.empty()) { int top = q.front(); q.pop(); if (dist[top] > 1) to_remove.push(top); for (const int u : g.v[top]) { if (dist[u] >= 0) continue; dist[u] = dist[top] + 1; q.push(u); } } while (!to_remove.empty()) { print_edge_removed(x, to_remove.top()); to_remove.pop(); } } void transform_into(const graph &start, const graph &target) { graph with_added = connect_all(start, 0); fix_nonincident(with_added, target, 0); fix_incident(target, 0); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); graph start, target; int n; cin >> n; start.n = target.n = n; start.input(); target.input(); transform_into(start, target); cout << fix_seq.size() << '\n'; for (auto &[c, x, y] : fix_seq) cout << c << ' ' << x + 1 << ' ' << y + 1 << '\n'; }
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 | #include <algorithm> #include <bits/stdc++.h> #include <iterator> using namespace std; struct op { char type; int x, y; }; vector<op> fix_seq; struct graph { int n, m; vector<vector<int>> v; void input() { cin >> m; v.resize(n); for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; add_edge(x - 1, y - 1); } } void add_edge(const int x, const int y) { v[x].push_back(y); v[y].push_back(x); } }; void print_edge_added(const int x, const int y) { fix_seq.push_back({'+', x, y}); } void print_edge_removed(const int x, const int y) { fix_seq.push_back({'-', x, y}); } graph connect_all(const graph &g, const int x) { graph res = g; vector<int> dist(g.n, -1); dist[x] = 0; queue<int> q{{x}}; while (!q.empty()) { int top = q.front(); q.pop(); if (dist[top] > 1) { res.add_edge(x, top); print_edge_added(x, top); } for (const int u : g.v[top]) { if (dist[u] >= 0) continue; dist[u] = dist[top] + 1; q.push(u); } } return res; } vector<pair<int, int>> get_nonincindent(const graph &g, const int forb) { vector<pair<int, int>> res; for (int x = 0; x < g.n; ++x) { if (x == forb) continue; for (int u : g.v[x]) { if (u == forb) continue; if (x < u) res.emplace_back(x, u); } } return res; } void fix_nonincident(const graph &g, const graph &target, const int forb) { vector<pair<int, int>> g_edges = get_nonincindent(g, forb); vector<pair<int, int>> target_edges = get_nonincindent(target, forb); sort(g_edges.begin(), g_edges.end()); sort(target_edges.begin(), target_edges.end()); vector<pair<int, int>> to_add; set_difference(target_edges.begin(), target_edges.end(), g_edges.begin(), g_edges.end(), back_inserter(to_add)); for (const auto &[x, y] : to_add) { print_edge_added(x, y); } vector<pair<int, int>> to_remove; set_difference(g_edges.begin(), g_edges.end(), target_edges.begin(), target_edges.end(), back_inserter(to_remove)); for (const auto &[x, y] : to_remove) { print_edge_removed(x, y); } } void fix_incident(const graph &g, const int x) { vector<int> dist(g.n, -1); dist[x] = 0; queue<int> q{{x}}; stack<int> to_remove; while (!q.empty()) { int top = q.front(); q.pop(); if (dist[top] > 1) to_remove.push(top); for (const int u : g.v[top]) { if (dist[u] >= 0) continue; dist[u] = dist[top] + 1; q.push(u); } } while (!to_remove.empty()) { print_edge_removed(x, to_remove.top()); to_remove.pop(); } } void transform_into(const graph &start, const graph &target) { graph with_added = connect_all(start, 0); fix_nonincident(with_added, target, 0); fix_incident(target, 0); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); graph start, target; int n; cin >> n; start.n = target.n = n; start.input(); target.input(); transform_into(start, target); cout << fix_seq.size() << '\n'; for (auto &[c, x, y] : fix_seq) cout << c << ' ' << x + 1 << ' ' << y + 1 << '\n'; } |