#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define mp make_pair #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define eb emplace_back #define st first #define nd second #define vt vector #define VAR(__var) #__var << ": " << __var << " " #define PAIR(__var) #__var << ": " << __var.first << ", " << __var.second << " " #define FOR(__var, __start, __end) for(int __var=__start; __var<__end; ++__var) #define FORB(__var, __start, __end) for(int __var=__start; __var>__end; --__var) #define REP(__var, __cnt) for(int __var=0; __var<(__cnt); ++__var) #define EACH(__var, __x) for(auto __var : __x) #define maxi(__x, __y) __x = (__x>__y?__x:__y) #define mini(__x, __y) __x = (__x<__y?__x:__y) #define all(__var) (__var).begin(),(__var).end() #define rall(__var) (__var).rbegin(),(__var).rend() #define sz(__var) (int)(__var).size() using namespace std; using namespace __gnu_pbds; template <typename T> using ord_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; #ifdef DEBUG template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; const bool __test_cases = false; const int INF = 1e9+2137; vt<tuple<char, int, int>> ans; vt<vt<int>> g; vt<int> depth; set<pii> edges, required, to_rem; void add_edge(int a, int b, bool print=true) { if(a > b) swap(a, b); if(edges.count({a, b}) == 0) { if(print) ans.pb({'+', a, b}); edges.insert({a, b}); } } void rem_edge(int a, int b, bool print=true) { if(a > b) swap(a, b); if(print) ans.pb({'-', a, b}); edges.erase({a, b}); } void dfs(int v) { for(auto u : g[v]) if(depth[u] == -1) { depth[u] = depth[v]+1; dfs(u); } } void run_dfs(int v) { fill(all(depth), -1); depth[v] = 0; dfs(v); } void construct_graph(set<pii> &ed) { fill(all(g), vt<int>()); for(auto [a, b] : ed) { g[a].pb(b); g[b].pb(a); } } void solve() { int n, m; cin >> n >> m; g.resize(n+1); depth.resize(n+1); FOR(i, 0, m) { int a, b; cin >> a >> b; add_edge(a, b, false); } construct_graph(edges); int m2; cin >> m2; FOR(i, 0, m2) { int a, b; cin >> a >> b; if(a > b) swap(a, b); required.insert({a, b}); } for(auto [a, b] : edges) { if(required.count({a, b}) == 0) to_rem.insert({a, b}); } // order by depth run_dfs(1); vt<tuple<int, int, int>> to_add; FOR(i, 2, n+1) to_add.pb({depth[i], 1, i}); sort(all(to_add)); for(auto [t, a, b] : to_add) { add_edge(a, b); } // add needed for(auto [a, b] : required) { add_edge(a, b); } // remove not needed for(auto [a, b] : to_rem) { if(a != 1) rem_edge(a, b); } // order by depth construct_graph(required); run_dfs(1); vt<tuple<int, int, int>> rem_help; FOR(i, 2, n+1) { if(required.count({1, i}) == 0) rem_help.pb({-depth[i], 1, i}); } sort(all(rem_help)); for(auto [_, a, b] : rem_help) { rem_edge(a, b); } cout << sz(ans) << '\n'; for(auto [t, x, y] : ans) cout << t << ' ' << x << ' ' << y << '\n'; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; if(__test_cases) cin >> t; while(t--) { 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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define mp make_pair #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define eb emplace_back #define st first #define nd second #define vt vector #define VAR(__var) #__var << ": " << __var << " " #define PAIR(__var) #__var << ": " << __var.first << ", " << __var.second << " " #define FOR(__var, __start, __end) for(int __var=__start; __var<__end; ++__var) #define FORB(__var, __start, __end) for(int __var=__start; __var>__end; --__var) #define REP(__var, __cnt) for(int __var=0; __var<(__cnt); ++__var) #define EACH(__var, __x) for(auto __var : __x) #define maxi(__x, __y) __x = (__x>__y?__x:__y) #define mini(__x, __y) __x = (__x<__y?__x:__y) #define all(__var) (__var).begin(),(__var).end() #define rall(__var) (__var).rbegin(),(__var).rend() #define sz(__var) (int)(__var).size() using namespace std; using namespace __gnu_pbds; template <typename T> using ord_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; #ifdef DEBUG template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; const bool __test_cases = false; const int INF = 1e9+2137; vt<tuple<char, int, int>> ans; vt<vt<int>> g; vt<int> depth; set<pii> edges, required, to_rem; void add_edge(int a, int b, bool print=true) { if(a > b) swap(a, b); if(edges.count({a, b}) == 0) { if(print) ans.pb({'+', a, b}); edges.insert({a, b}); } } void rem_edge(int a, int b, bool print=true) { if(a > b) swap(a, b); if(print) ans.pb({'-', a, b}); edges.erase({a, b}); } void dfs(int v) { for(auto u : g[v]) if(depth[u] == -1) { depth[u] = depth[v]+1; dfs(u); } } void run_dfs(int v) { fill(all(depth), -1); depth[v] = 0; dfs(v); } void construct_graph(set<pii> &ed) { fill(all(g), vt<int>()); for(auto [a, b] : ed) { g[a].pb(b); g[b].pb(a); } } void solve() { int n, m; cin >> n >> m; g.resize(n+1); depth.resize(n+1); FOR(i, 0, m) { int a, b; cin >> a >> b; add_edge(a, b, false); } construct_graph(edges); int m2; cin >> m2; FOR(i, 0, m2) { int a, b; cin >> a >> b; if(a > b) swap(a, b); required.insert({a, b}); } for(auto [a, b] : edges) { if(required.count({a, b}) == 0) to_rem.insert({a, b}); } // order by depth run_dfs(1); vt<tuple<int, int, int>> to_add; FOR(i, 2, n+1) to_add.pb({depth[i], 1, i}); sort(all(to_add)); for(auto [t, a, b] : to_add) { add_edge(a, b); } // add needed for(auto [a, b] : required) { add_edge(a, b); } // remove not needed for(auto [a, b] : to_rem) { if(a != 1) rem_edge(a, b); } // order by depth construct_graph(required); run_dfs(1); vt<tuple<int, int, int>> rem_help; FOR(i, 2, n+1) { if(required.count({1, i}) == 0) rem_help.pb({-depth[i], 1, i}); } sort(all(rem_help)); for(auto [_, a, b] : rem_help) { rem_edge(a, b); } cout << sz(ans) << '\n'; for(auto [t, x, y] : ans) cout << t << ' ' << x << ' ' << y << '\n'; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; if(__test_cases) cin >> t; while(t--) { solve(); } return 0; } |