#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <array> #include <random> #include <cmath> #include <chrono> #include <list> #include <ctime> #include <sstream> #include <queue> #include <climits> #include <stack> #include <valarray> #include <random> #include <bitset> #include <numeric> #include <iomanip> #include <cassert> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; #define rep(x, b, e) for(int x=(b); x<(e); ++x) #define trav(a, x) for(auto& a : x) #define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define all(c) c.begin(),c.end() #define sz(x) ((int)((x).size())) #define pb push_back #define eb emplace_back #define st first #define nd second #define mp(x,y) make_pair(x,y) typedef short int sint; template<typename T> bool ckmin(T& a, const T& b){return b<a?a=b,1:0;} template<typename T> bool ckmax(T& a, const T& b){return b>a?a=b,1:0;} template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(char c) { return string(1, c); } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif // #include <atcoder/modint> // using namespace atcoder; // using mint = modint998244353; // modint1000000007; // typedef vector<mint> vmi; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // use rng() to get unsigned int // mt19937_64 for random long longs vector<vi> moves; vector<pii> read_graph(int n) { int m; cin >> m; vector<pii> ans(m); rep(i, 0, m) { int a, b; cin >> a >> b; --a; --b; ans[i] = {a, b}; } return ans; } pair<vector<vi>, set<pii>> parse_graph(int n, vector<pii>& e) { int m = sz(e); vector<vi> g(n); set<pii> edges; rep(i, 0, m) { int a = e[i].st; int b = e[i].nd; g[a].pb(b); g[b].pb(a); edges.emplace(min(a, b), max(a, b)); } rep(i, 0, n) { sort(all(g[i])); } return {g, edges}; } void add_move(int op, int a, int b) { char opk = op == 0 ? '+' : '-'; debug("op: ", opk, a + 1, b + 1); // cout << "adding move: " << sz(moves) << endl; moves.push_back({op, a, b}); // cout << "added: " << sz(moves) << endl; } void dodaj(int v, vi& vis, vector<vi>&g, bool add, set<pii>& s) { if (add) { add_move(0, 0, v); s.insert({0, v}); } vis[v] = 1; for (auto u : g[v]) { if (vis[u]) continue; dodaj(u, vis, g, true, s); } } void usun(int v, vi& vis, vector<vi>&g, bool rem) { vis[v] = 1; for (auto u : g[v]) { if (vis[u]) continue; usun(u, vis, g, true); } if (rem) { add_move(1, 0, v); } } bool solve() { int n; cin >> n; auto e1 = read_graph(n); auto e2 = read_graph(n); auto [g1, s1] = parse_graph(n, e1); auto [g2, s2] = parse_graph(n, e2); vi vis(n); vis[0] = 1; for (auto u : g1[0]) { vis[u] = 1; } for (auto u : g1[0]) { dodaj(u, vis, g1, false, s1); } // debug("After add to 1"); for (auto &[u, v] : s1) { if (s2.count({u, v}) == 0 && u != 0) { add_move(1, u, v); } } for (auto &[u, v]: s2 ) { if (s1.count({u, v}) == 0) { add_move(0, u, v); } } vi vis2(n); vis2[0] = 1; for (auto u : g2[0]) { vis2[u] = 1; } for (auto u : g2[0]) { usun(u, vis2, g2, false); } cout << sz(moves) << "\n"; for (auto move : moves) { cout << (move[0] ? '-' : '+') << ' ' << (move[1] + 1) << ' ' << (move[2] + 1) << '\n'; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); solve(); }
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <array> #include <random> #include <cmath> #include <chrono> #include <list> #include <ctime> #include <sstream> #include <queue> #include <climits> #include <stack> #include <valarray> #include <random> #include <bitset> #include <numeric> #include <iomanip> #include <cassert> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; #define rep(x, b, e) for(int x=(b); x<(e); ++x) #define trav(a, x) for(auto& a : x) #define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define all(c) c.begin(),c.end() #define sz(x) ((int)((x).size())) #define pb push_back #define eb emplace_back #define st first #define nd second #define mp(x,y) make_pair(x,y) typedef short int sint; template<typename T> bool ckmin(T& a, const T& b){return b<a?a=b,1:0;} template<typename T> bool ckmax(T& a, const T& b){return b>a?a=b,1:0;} template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(char c) { return string(1, c); } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif // #include <atcoder/modint> // using namespace atcoder; // using mint = modint998244353; // modint1000000007; // typedef vector<mint> vmi; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // use rng() to get unsigned int // mt19937_64 for random long longs vector<vi> moves; vector<pii> read_graph(int n) { int m; cin >> m; vector<pii> ans(m); rep(i, 0, m) { int a, b; cin >> a >> b; --a; --b; ans[i] = {a, b}; } return ans; } pair<vector<vi>, set<pii>> parse_graph(int n, vector<pii>& e) { int m = sz(e); vector<vi> g(n); set<pii> edges; rep(i, 0, m) { int a = e[i].st; int b = e[i].nd; g[a].pb(b); g[b].pb(a); edges.emplace(min(a, b), max(a, b)); } rep(i, 0, n) { sort(all(g[i])); } return {g, edges}; } void add_move(int op, int a, int b) { char opk = op == 0 ? '+' : '-'; debug("op: ", opk, a + 1, b + 1); // cout << "adding move: " << sz(moves) << endl; moves.push_back({op, a, b}); // cout << "added: " << sz(moves) << endl; } void dodaj(int v, vi& vis, vector<vi>&g, bool add, set<pii>& s) { if (add) { add_move(0, 0, v); s.insert({0, v}); } vis[v] = 1; for (auto u : g[v]) { if (vis[u]) continue; dodaj(u, vis, g, true, s); } } void usun(int v, vi& vis, vector<vi>&g, bool rem) { vis[v] = 1; for (auto u : g[v]) { if (vis[u]) continue; usun(u, vis, g, true); } if (rem) { add_move(1, 0, v); } } bool solve() { int n; cin >> n; auto e1 = read_graph(n); auto e2 = read_graph(n); auto [g1, s1] = parse_graph(n, e1); auto [g2, s2] = parse_graph(n, e2); vi vis(n); vis[0] = 1; for (auto u : g1[0]) { vis[u] = 1; } for (auto u : g1[0]) { dodaj(u, vis, g1, false, s1); } // debug("After add to 1"); for (auto &[u, v] : s1) { if (s2.count({u, v}) == 0 && u != 0) { add_move(1, u, v); } } for (auto &[u, v]: s2 ) { if (s1.count({u, v}) == 0) { add_move(0, u, v); } } vi vis2(n); vis2[0] = 1; for (auto u : g2[0]) { vis2[u] = 1; } for (auto u : g2[0]) { usun(u, vis2, g2, false); } cout << sz(moves) << "\n"; for (auto move : moves) { cout << (move[0] ? '-' : '+') << ' ' << (move[1] + 1) << ' ' << (move[2] + 1) << '\n'; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); solve(); } |