#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) 42 #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) #define each(a,x) for(auto& a : (x)) #define sz(x) int((x).size()) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } vector<pi> rd() { int m; cin >> m; vector<pi> ret(m); for (auto &[a, b] : ret) cin >> a >> b; return ret; } set<pi> curr; vector<tuple<char, int, int>> ops; bool has(int a, int b) { if (a > b) swap(a, b); assert(a < b); return curr.count({a, b}); } void add(int a, int b, int c) { if (a > b) swap(a, b); assert(a < b); assert(has(a, c) && has(b, c) && !has(a, b)); curr.emplace(a, b); ops.emplace_back('+', a, b); } void rem(int a, int b, int c) { if (a > b) swap(a, b); assert(a < b); assert(has(a, c) && has(b, c) && has(a, b)); curr.erase({a, b}); ops.emplace_back('-', a, b); } vector<pi> bfsord(int n, const vector<pi> &ge, int from) { vvi g(n+1); for (auto [a, b] : ge) { g[a].push_back(b); g[b].push_back(a); } vi push(n+1); push[from] = true; vector<pi> ret = {pi{from, -1}}; rep(id, n) { int i = ret[id].first; for (int j : g[i]) if (!push[j]) { push[j] = true; ret.emplace_back(j, i); } } assert(sz(ret) == n); return ret; } void solve() { int n; cin >> n; const vector<pi> act = rd(), goal = rd(); set<int> goal1; for (auto [a, b] : act) curr.emplace(min(a, b), max(a, b)); for (auto [a, b] : goal) if (a == 1 || b == 1) goal1.insert(a ^ b ^ 1); auto oa = bfsord(n, act, 1), og = bfsord(n, goal, 1); for (auto [i, c] : oa) if (i > 1 && !has(i, 1)) add(1, i, c); for (auto [a, b] : act) if (a > 1 && b > 1) rem(a, b, 1); for (auto [a, b] : goal) if (a > 1 && b > 1) add(a, b, 1); reverse(all(og)); for (auto [i, c] : og) if (i > 1 && !goal1.count(i)) rem(1, i, c); assert(sz(curr) == sz(goal)); for (auto [a, b] : goal) assert(has(a, b)); assert(sz(ops) <= 200000); cout << sz(ops) << '\n'; for (auto [sg, a, b] : ops) cout << sg << ' ' << a << ' ' << b << '\n'; } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // cin >> z; while (z--) solve(); 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 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) 42 #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) #define each(a,x) for(auto& a : (x)) #define sz(x) int((x).size()) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } vector<pi> rd() { int m; cin >> m; vector<pi> ret(m); for (auto &[a, b] : ret) cin >> a >> b; return ret; } set<pi> curr; vector<tuple<char, int, int>> ops; bool has(int a, int b) { if (a > b) swap(a, b); assert(a < b); return curr.count({a, b}); } void add(int a, int b, int c) { if (a > b) swap(a, b); assert(a < b); assert(has(a, c) && has(b, c) && !has(a, b)); curr.emplace(a, b); ops.emplace_back('+', a, b); } void rem(int a, int b, int c) { if (a > b) swap(a, b); assert(a < b); assert(has(a, c) && has(b, c) && has(a, b)); curr.erase({a, b}); ops.emplace_back('-', a, b); } vector<pi> bfsord(int n, const vector<pi> &ge, int from) { vvi g(n+1); for (auto [a, b] : ge) { g[a].push_back(b); g[b].push_back(a); } vi push(n+1); push[from] = true; vector<pi> ret = {pi{from, -1}}; rep(id, n) { int i = ret[id].first; for (int j : g[i]) if (!push[j]) { push[j] = true; ret.emplace_back(j, i); } } assert(sz(ret) == n); return ret; } void solve() { int n; cin >> n; const vector<pi> act = rd(), goal = rd(); set<int> goal1; for (auto [a, b] : act) curr.emplace(min(a, b), max(a, b)); for (auto [a, b] : goal) if (a == 1 || b == 1) goal1.insert(a ^ b ^ 1); auto oa = bfsord(n, act, 1), og = bfsord(n, goal, 1); for (auto [i, c] : oa) if (i > 1 && !has(i, 1)) add(1, i, c); for (auto [a, b] : act) if (a > 1 && b > 1) rem(a, b, 1); for (auto [a, b] : goal) if (a > 1 && b > 1) add(a, b, 1); reverse(all(og)); for (auto [i, c] : og) if (i > 1 && !goal1.count(i)) rem(1, i, c); assert(sz(curr) == sz(goal)); for (auto [a, b] : goal) assert(has(a, b)); assert(sz(ops) <= 200000); cout << sz(ops) << '\n'; for (auto [sg, a, b] : ops) cout << sg << ' ' << a << ' ' << b << '\n'; } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // cin >> z; while (z--) solve(); return 0; } |