#include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif struct Edge { int want; int have; int v, u; Edge(int _v, int _u) { want = have = 0; v = _v; u = _u; } }; vector<Edge> edges; map<pii, int> edge_id; vector<vector<pii> > g; void addEdge(int v, int u, int have, int want) { if (v > u) swap(v, u); if (edge_id.find({v, u}) == end(edge_id)) { edge_id[{v, u}] = sz(edges); g[v].push_back({u, sz(edges)}); g[u].push_back({v, sz(edges)}); edges.push_back(Edge(v, u)); } int id = edge_id[{v, u}]; if (have) edges[id].have = 1; if (want) edges[id].want = 1; } vi vis, root_id; int query_count = 0; const int query_max = 200 * 1000; vector<pii> queries; void print_queries() { cout << sz(queries) << '\n'; for (auto [t, id] : queries) { if (t == 1) { cout << "+ " << edges[id].v + 1 << ' ' << edges[id].u + 1 << '\n'; } else if (t == -1) { cout << "- " << edges[id].v + 1 << ' ' << edges[id].u + 1 << '\n'; } } } void query_add(int id) { queries.push_back({1, id}); query_count += 1; assert(edges[id].have == 0); edges[id].have = 1; } void query_rem(int id) { queries.push_back({-1, id}); query_count += 1; assert(edges[id].have == 1); edges[id].have = 0; } void add_root_edges(int v) { vis[v] = 1; if (v != 0 && edges[root_id[v]].have == 0) query_add(root_id[v]); for (auto [u, id] : g[v]) if (!vis[u] && edges[id].have == 1) add_root_edges(u); } void add_other_edges(int v) { vis[v] = 1; for (auto [u, id] : g[v]) if (edges[id].have == 0) query_add(id); for (auto [u, id] : g[v]) if (!vis[u]) add_other_edges(u); } void rem_other_edges(int v) { vis[v] = 1; if (v != 0) { for (auto [u, id] : g[v]) if (id != root_id[v] && edges[id].want == 0 && edges[id].have == 1) query_rem(id); } for (auto [u, id] : g[v]) if (!vis[u]) rem_other_edges(u); } void rem_root_edges(int v) { vis[v] = 1; for (auto [u, id] : g[v]) if (!vis[u] && edges[id].want == 1) rem_root_edges(u); if (v != 0 && edges[root_id[v]].want == 0) query_rem(root_id[v]); } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin >> n; g.resize(n); vis.resize(n); int ms; cin >> ms; rep(i, ms) { int v, u; cin >> v >> u; v --, u --; addEdge(v, u, 1, 0); } int md; cin >> md; rep(i, md) { int v, u; cin >> v >> u; v --, u --; addEdge(v, u, 0, 1); } fwd(i, 1, n) addEdge(0, i, 0, 0); root_id.resize(n, -1); rep(v, n) { for (auto [u, id] : g[v]) if (u == 0) { root_id[v] = id; break; } } fill(all(vis), 0); add_root_edges(0); fill(all(vis), 0); add_other_edges(0); fill(all(vis), 0); rem_other_edges(0); fill(all(vis), 0); rem_root_edges(0); rep(i, sz(edges)) assert(edges[i].have == edges[i].want); assert(query_count <= query_max); print_queries(); 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 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif struct Edge { int want; int have; int v, u; Edge(int _v, int _u) { want = have = 0; v = _v; u = _u; } }; vector<Edge> edges; map<pii, int> edge_id; vector<vector<pii> > g; void addEdge(int v, int u, int have, int want) { if (v > u) swap(v, u); if (edge_id.find({v, u}) == end(edge_id)) { edge_id[{v, u}] = sz(edges); g[v].push_back({u, sz(edges)}); g[u].push_back({v, sz(edges)}); edges.push_back(Edge(v, u)); } int id = edge_id[{v, u}]; if (have) edges[id].have = 1; if (want) edges[id].want = 1; } vi vis, root_id; int query_count = 0; const int query_max = 200 * 1000; vector<pii> queries; void print_queries() { cout << sz(queries) << '\n'; for (auto [t, id] : queries) { if (t == 1) { cout << "+ " << edges[id].v + 1 << ' ' << edges[id].u + 1 << '\n'; } else if (t == -1) { cout << "- " << edges[id].v + 1 << ' ' << edges[id].u + 1 << '\n'; } } } void query_add(int id) { queries.push_back({1, id}); query_count += 1; assert(edges[id].have == 0); edges[id].have = 1; } void query_rem(int id) { queries.push_back({-1, id}); query_count += 1; assert(edges[id].have == 1); edges[id].have = 0; } void add_root_edges(int v) { vis[v] = 1; if (v != 0 && edges[root_id[v]].have == 0) query_add(root_id[v]); for (auto [u, id] : g[v]) if (!vis[u] && edges[id].have == 1) add_root_edges(u); } void add_other_edges(int v) { vis[v] = 1; for (auto [u, id] : g[v]) if (edges[id].have == 0) query_add(id); for (auto [u, id] : g[v]) if (!vis[u]) add_other_edges(u); } void rem_other_edges(int v) { vis[v] = 1; if (v != 0) { for (auto [u, id] : g[v]) if (id != root_id[v] && edges[id].want == 0 && edges[id].have == 1) query_rem(id); } for (auto [u, id] : g[v]) if (!vis[u]) rem_other_edges(u); } void rem_root_edges(int v) { vis[v] = 1; for (auto [u, id] : g[v]) if (!vis[u] && edges[id].want == 1) rem_root_edges(u); if (v != 0 && edges[root_id[v]].want == 0) query_rem(root_id[v]); } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin >> n; g.resize(n); vis.resize(n); int ms; cin >> ms; rep(i, ms) { int v, u; cin >> v >> u; v --, u --; addEdge(v, u, 1, 0); } int md; cin >> md; rep(i, md) { int v, u; cin >> v >> u; v --, u --; addEdge(v, u, 0, 1); } fwd(i, 1, n) addEdge(0, i, 0, 0); root_id.resize(n, -1); rep(v, n) { for (auto [u, id] : g[v]) if (u == 0) { root_id[v] = id; break; } } fill(all(vis), 0); add_root_edges(0); fill(all(vis), 0); add_other_edges(0); fill(all(vis), 0); rem_other_edges(0); fill(all(vis), 0); rem_root_edges(0); rep(i, sz(edges)) assert(edges[i].have == edges[i].want); assert(query_count <= query_max); print_queries(); return 0; } |