#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> //#pragma GCC target ("avx2") //#pragma GCC optimize ("Ofast") //#pragma GCC optimize ("unroll-loops") #define f first #define s second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((int) (x).size()) #define pb push_back #define mp make_pair //#define int long long using namespace std; using namespace __gnu_pbds; template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> inline bool umin(T &a, const T &b) { if(a > b) { a = b; return 1; } return 0; } template <typename T> inline bool umax(T &a, const T &b) { if(a < b) { a = b; return 1; } return 0; } typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll mod = 998244353; const ll base = 1e6 + 9; const ll inf = 1e18; const int MAX = 3e5 + 42; const int LG = 20; vector<int> adj[MAX]; bool checker(const vector<vector<int>> &G, const vector<array<int, 3>> &ans) { int n = sz(G) - 1; vector<set<int>> g(n + 1); set<pii> have; for(int v = 1; v <= n; v++) { for(auto to : G[v]) { g[v].insert(to); } } auto add = [&](int u, int v) { g[u].insert(v); g[v].insert(u); }; auto del = [&](int u, int v) { g[u].erase(v); g[v].erase(u); }; auto legal = [&](int u, int v) { if(u == v) return 0; if(sz(g[v]) < sz(g[u])) swap(u, v); for(auto x : g[v]) { if(x != u && g[u].count(x)) return 1; } return 0; }; auto can_del = [&](int u, int v) { if(!g[u].count(v) || !g[v].count(u)) return 0; return legal(u, v); }; auto can_add = [&](int u, int v) { if(g[u].count(v) || g[v].count(u)) return 0; return legal(u, v); }; for(auto [t, u, v] : ans) { if(t == 1) { if(!can_add(u, v)) return 0; add(u, v); } else { if(!can_del(u, v)) return 0; del(u, v); } } vector<set<int>> a(n + 1); for(int v = 1; v <= n; v++) { for(auto to : adj[v]) a[v].insert(to); } for(int v = 1; v <= n; v++) { if(a[v] != g[v]) return 0; } return 1; } int timer = 0; int tin[MAX], low[MAX]; vector<int> good; int used_sec[MAX]; void articulation_points(int v, int p = 0) { used_sec[v] = 1; tin[v] = low[v] = timer++; int c = 0; for(auto to : adj[v]) { if(to == p) continue; if(used_sec[to]) umin(low[v], tin[to]); else { articulation_points(to, v); umin(low[v], low[to]); if(low[to] >= tin[v] && p); else good.pb(v); c++; } } if(c == 1 && !p) good.pb(v); } void solve() { int n, m1, m2; cin >> n >> m1; set<pii> need; set<pii> have; vector<vector<int>> g(n + 1); while(m1--) { int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); have.insert({u, v}); have.insert({v, u}); } cin >> m2; while(m2--) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); need.insert({u, v}); need.insert({v, u}); } auto check = [&](int u, int v) { return need.count({u, v}); }; articulation_points(1); assert(sz(good) > 0); int s = good[0]; vector<array<int, 3>> ans; auto make = [&](int u, int v) { if(have.count({u, v})) { have.erase({u, v}); have.erase({v, u}); ans.pb({0, u, v}); } else { have.insert({u, v}); have.insert({v, u}); ans.pb({1, u, v}); } }; vector<int> used(n + 1); function<void(int, int)> dfs_connect = [&](int v, int p) { used[v] = 1; if(s != v && !have.count({s, v})) make(s, v); for(auto to : g[v]) { if(to == p) continue; if(used[to]) continue; dfs_connect(to, v); } }; dfs_connect(s, 0); for(auto [u, v] : need) { if(v == s || u == s) continue; if(!have.count({u, v})) { make(u, v); } } auto it = have.begin(); while(it != have.end()) { auto [u, v] = *it; if(!check(u, v) && v != s && u != s) { make(u, v); } it = have.upper_bound({u, v}); } fill(all(used), 0); used[s] = 1; for(auto to : adj[s]) { used[to] = 1; } function<void(int, int)> dfs = [&](int v, int p) { bool flag = !used[v]; used[v] = 1; for(auto to : adj[v]) { if(to == p || used[to]) continue; dfs(to, v); } if(flag) { make(s, v); } }; for(auto to : adj[s]) { dfs(to, s); } cout << sz(ans) << '\n'; for(auto [t, u, v] : ans) { cout << (t? '+' : '-') << " " << u << " " << v << '\n'; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ttt = 1; // cin >> ttt; while(ttt--) { 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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> //#pragma GCC target ("avx2") //#pragma GCC optimize ("Ofast") //#pragma GCC optimize ("unroll-loops") #define f first #define s second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((int) (x).size()) #define pb push_back #define mp make_pair //#define int long long using namespace std; using namespace __gnu_pbds; template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> inline bool umin(T &a, const T &b) { if(a > b) { a = b; return 1; } return 0; } template <typename T> inline bool umax(T &a, const T &b) { if(a < b) { a = b; return 1; } return 0; } typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll mod = 998244353; const ll base = 1e6 + 9; const ll inf = 1e18; const int MAX = 3e5 + 42; const int LG = 20; vector<int> adj[MAX]; bool checker(const vector<vector<int>> &G, const vector<array<int, 3>> &ans) { int n = sz(G) - 1; vector<set<int>> g(n + 1); set<pii> have; for(int v = 1; v <= n; v++) { for(auto to : G[v]) { g[v].insert(to); } } auto add = [&](int u, int v) { g[u].insert(v); g[v].insert(u); }; auto del = [&](int u, int v) { g[u].erase(v); g[v].erase(u); }; auto legal = [&](int u, int v) { if(u == v) return 0; if(sz(g[v]) < sz(g[u])) swap(u, v); for(auto x : g[v]) { if(x != u && g[u].count(x)) return 1; } return 0; }; auto can_del = [&](int u, int v) { if(!g[u].count(v) || !g[v].count(u)) return 0; return legal(u, v); }; auto can_add = [&](int u, int v) { if(g[u].count(v) || g[v].count(u)) return 0; return legal(u, v); }; for(auto [t, u, v] : ans) { if(t == 1) { if(!can_add(u, v)) return 0; add(u, v); } else { if(!can_del(u, v)) return 0; del(u, v); } } vector<set<int>> a(n + 1); for(int v = 1; v <= n; v++) { for(auto to : adj[v]) a[v].insert(to); } for(int v = 1; v <= n; v++) { if(a[v] != g[v]) return 0; } return 1; } int timer = 0; int tin[MAX], low[MAX]; vector<int> good; int used_sec[MAX]; void articulation_points(int v, int p = 0) { used_sec[v] = 1; tin[v] = low[v] = timer++; int c = 0; for(auto to : adj[v]) { if(to == p) continue; if(used_sec[to]) umin(low[v], tin[to]); else { articulation_points(to, v); umin(low[v], low[to]); if(low[to] >= tin[v] && p); else good.pb(v); c++; } } if(c == 1 && !p) good.pb(v); } void solve() { int n, m1, m2; cin >> n >> m1; set<pii> need; set<pii> have; vector<vector<int>> g(n + 1); while(m1--) { int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); have.insert({u, v}); have.insert({v, u}); } cin >> m2; while(m2--) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); need.insert({u, v}); need.insert({v, u}); } auto check = [&](int u, int v) { return need.count({u, v}); }; articulation_points(1); assert(sz(good) > 0); int s = good[0]; vector<array<int, 3>> ans; auto make = [&](int u, int v) { if(have.count({u, v})) { have.erase({u, v}); have.erase({v, u}); ans.pb({0, u, v}); } else { have.insert({u, v}); have.insert({v, u}); ans.pb({1, u, v}); } }; vector<int> used(n + 1); function<void(int, int)> dfs_connect = [&](int v, int p) { used[v] = 1; if(s != v && !have.count({s, v})) make(s, v); for(auto to : g[v]) { if(to == p) continue; if(used[to]) continue; dfs_connect(to, v); } }; dfs_connect(s, 0); for(auto [u, v] : need) { if(v == s || u == s) continue; if(!have.count({u, v})) { make(u, v); } } auto it = have.begin(); while(it != have.end()) { auto [u, v] = *it; if(!check(u, v) && v != s && u != s) { make(u, v); } it = have.upper_bound({u, v}); } fill(all(used), 0); used[s] = 1; for(auto to : adj[s]) { used[to] = 1; } function<void(int, int)> dfs = [&](int v, int p) { bool flag = !used[v]; used[v] = 1; for(auto to : adj[v]) { if(to == p || used[to]) continue; dfs(to, v); } if(flag) { make(s, v); } }; for(auto to : adj[s]) { dfs(to, s); } cout << sz(ans) << '\n'; for(auto [t, u, v] : ans) { cout << (t? '+' : '-') << " " << u << " " << v << '\n'; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ttt = 1; // cin >> ttt; while(ttt--) { solve(); } } |