#include <bits/stdc++.h> using namespace std; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define endl '\n' #define sp <<" "<< #define eb emplace_back #define MOD 1000000007 #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() using ll = long long; #define vec vector template <class T> void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "}\n"; } template <class T> void print_m(vector<vector<T>> &m) { for (auto v : m) print_v(v); cout << '\n'; } #define fora(a) for(auto e:a) #define it(i,s,e) for(long long int i=s;i<e;i++) #define ita(i,s,e) for(long long int i=s;i<=e;i++) #define itr(i,e,s) for(long long int i=e-1;i>=s;i--) #define urs(r...)typename decay<decltype(r)>::type #define rep(i,n)for(urs(n)i=0;i<(n);++i) const int MAX = 30000 + 10; int atoms; vec<set<int>> graph(MAX); vec<set<int>> target_graph(MAX); vec<tuple<char, int, int>> ops; void star_graph() { set<int> vis; queue<int> q; q.push(1); while (!q.empty()) { int elt = q.front(); q.pop(); if (vis.count(elt)) { continue; } vis.insert(elt); if (elt != 1 && !graph[elt].count(1)) { graph[1].insert(elt); graph[elt].insert(1); ops.eb(make_tuple('+', 1, elt)); } for (int neigh: graph[elt]) { q.push(neigh); } } } void connect_graph() { for (int node=2; node <= atoms; node++) { for (int neigh: target_graph[node]) { if (!graph[neigh].count(node)) { graph[neigh].insert(node); graph[node].insert(neigh); ops.eb(make_tuple('+', node, neigh)); } } } } vec<int> visit_order() { set<int> vis; vec<int> vis_vec; queue<int> q; q.push(1); while (!q.empty()) { int elt = q.front(); q.pop(); if (vis.count(elt)) { continue; } vis.insert(elt); if (elt!=1) vis_vec.eb(elt); for (int neigh: target_graph[elt]) { q.push(neigh); } } return vis_vec; } void purge_graph() { for (int node=2; node <= atoms; node++) { set<int> neighs = graph[node]; for (int neigh: neighs) { if (neigh!=1 && !target_graph[neigh].count(node)) { graph[neigh].erase(node); graph[node].erase(neigh); ops.eb(make_tuple('-', node, neigh)); } } } vec<int> vis_ord = visit_order(); reverse(vis_ord.begin(), vis_ord.end()); for (int neigh: vis_ord) { if (!target_graph[neigh].count(1)) { ops.eb(make_tuple('-', 1, neigh)); } } } void solve() { // vec<set<int>> cp = graph; star_graph(); connect_graph(); purge_graph(); // for (auto [c, a, b]: ops) { // //cout << c sp a sp b << endl; // bool found = false; // for (int c: cp[a]) { // if (cp[b].count(c)) { // found = true; // break; // } // } // if (!found) { // cout << "FAIL (unstable)"; // return; // } // if (c == '+') { // if (cp[a].count(b) || cp[b].count(a)) { // cout << "FAIL (already connected)"; // return; // } // cp[a].insert(b); // cp[b].insert(a); // } else { // if (!cp[a].count(b) || !cp[b].count(a)) { // cout << "FAIL (already disconnected)"; // return; // } // cp[a].erase(b); // cp[b].erase(a); // } // } // bool err = 0; // for (int i=1; i<=atoms; i++) { // if (cp[i] != target_graph[i]) { // for (int x: cp[i]) { // cout << x << " "; // } // cout << endl; // for (int x: target_graph[i]) { // cout << x << " "; // } // cout << endl; // } // } cout << ops.size() << endl; for (auto [c, a, b]: ops) { cout << c sp a sp b << endl; } } void load_graph(vec<set<int>> &graph) { int n; cin >> n; rep(_, n) { int a, b; cin >> a >> b; graph[a].insert(b); graph[b].insert(a); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> atoms; load_graph(graph); load_graph(target_graph); 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 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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | #include <bits/stdc++.h> using namespace std; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define endl '\n' #define sp <<" "<< #define eb emplace_back #define MOD 1000000007 #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() using ll = long long; #define vec vector template <class T> void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "}\n"; } template <class T> void print_m(vector<vector<T>> &m) { for (auto v : m) print_v(v); cout << '\n'; } #define fora(a) for(auto e:a) #define it(i,s,e) for(long long int i=s;i<e;i++) #define ita(i,s,e) for(long long int i=s;i<=e;i++) #define itr(i,e,s) for(long long int i=e-1;i>=s;i--) #define urs(r...)typename decay<decltype(r)>::type #define rep(i,n)for(urs(n)i=0;i<(n);++i) const int MAX = 30000 + 10; int atoms; vec<set<int>> graph(MAX); vec<set<int>> target_graph(MAX); vec<tuple<char, int, int>> ops; void star_graph() { set<int> vis; queue<int> q; q.push(1); while (!q.empty()) { int elt = q.front(); q.pop(); if (vis.count(elt)) { continue; } vis.insert(elt); if (elt != 1 && !graph[elt].count(1)) { graph[1].insert(elt); graph[elt].insert(1); ops.eb(make_tuple('+', 1, elt)); } for (int neigh: graph[elt]) { q.push(neigh); } } } void connect_graph() { for (int node=2; node <= atoms; node++) { for (int neigh: target_graph[node]) { if (!graph[neigh].count(node)) { graph[neigh].insert(node); graph[node].insert(neigh); ops.eb(make_tuple('+', node, neigh)); } } } } vec<int> visit_order() { set<int> vis; vec<int> vis_vec; queue<int> q; q.push(1); while (!q.empty()) { int elt = q.front(); q.pop(); if (vis.count(elt)) { continue; } vis.insert(elt); if (elt!=1) vis_vec.eb(elt); for (int neigh: target_graph[elt]) { q.push(neigh); } } return vis_vec; } void purge_graph() { for (int node=2; node <= atoms; node++) { set<int> neighs = graph[node]; for (int neigh: neighs) { if (neigh!=1 && !target_graph[neigh].count(node)) { graph[neigh].erase(node); graph[node].erase(neigh); ops.eb(make_tuple('-', node, neigh)); } } } vec<int> vis_ord = visit_order(); reverse(vis_ord.begin(), vis_ord.end()); for (int neigh: vis_ord) { if (!target_graph[neigh].count(1)) { ops.eb(make_tuple('-', 1, neigh)); } } } void solve() { // vec<set<int>> cp = graph; star_graph(); connect_graph(); purge_graph(); // for (auto [c, a, b]: ops) { // //cout << c sp a sp b << endl; // bool found = false; // for (int c: cp[a]) { // if (cp[b].count(c)) { // found = true; // break; // } // } // if (!found) { // cout << "FAIL (unstable)"; // return; // } // if (c == '+') { // if (cp[a].count(b) || cp[b].count(a)) { // cout << "FAIL (already connected)"; // return; // } // cp[a].insert(b); // cp[b].insert(a); // } else { // if (!cp[a].count(b) || !cp[b].count(a)) { // cout << "FAIL (already disconnected)"; // return; // } // cp[a].erase(b); // cp[b].erase(a); // } // } // bool err = 0; // for (int i=1; i<=atoms; i++) { // if (cp[i] != target_graph[i]) { // for (int x: cp[i]) { // cout << x << " "; // } // cout << endl; // for (int x: target_graph[i]) { // cout << x << " "; // } // cout << endl; // } // } cout << ops.size() << endl; for (auto [c, a, b]: ops) { cout << c sp a sp b << endl; } } void load_graph(vec<set<int>> &graph) { int n; cin >> n; rep(_, n) { int a, b; cin >> a >> b; graph[a].insert(b); graph[b].insert(a); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> atoms; load_graph(graph); load_graph(target_graph); solve(); return 0; } |