#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = long double; using vi = vector<int>; using vll = vector<ll>; using vii = vector<pii>; const int N = 3e4 + 10; vector<int> G[N]; vector<int> G4[N]; struct Edges { set<pii> edges; void add(int u, int v) { if (u > v) { swap(u, v); } edges.insert({u, v}); } bool check(int u, int v) { if (u > v) { swap(u, v); } return edges.count({u, v}); } void remove(int u, int v) { if (u > v) { swap(u, v); } edges.erase({u, v}); } }; struct Event { int u, v; bool type; Event(int u_, int v_, bool type_) : u(u_), v(v_), type(type_) {} }; vector<Event> result; void add_event(int u, int v) { result.push_back(Event(u, v, true)); } void remove_event(int u, int v) { result.push_back(Event(u, v, false)); } void print_event(Event e) { cout << (e.type ? "+" : "-") << " " << e.u << " " << e.v << "\n"; } Edges actual_edges, good_edges, bad_edges; bool vis[N]; void dfs_phase1(int v) { vis[v] = true; if (v != 1 && !actual_edges.check(1, v)) { add_event(1, v); actual_edges.add(1, v); } for (auto u : G[v]) { if (!vis[u]) { dfs_phase1(u); } } } void dfs_phase4(int v) { vis[v] = true; for (auto u : G4[v]) { if (!vis[u]) { dfs_phase4(u); } } if (v != 1 && !good_edges.check(1, v) && actual_edges.check(1, v)) { remove_event(1, v); } } int main() { ios_base::sync_with_stdio(false); cin.tie(); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); actual_edges.add(u, v); } int m2; cin >> m2; for (int i = 0; i < m2; i++) { int u, v; cin >> u >> v; good_edges.add(u, v); } dfs_phase1(1); fill(vis + 1, vis + n + 1, false); // phase 2: add all necessary edges for (auto e : good_edges.edges) { int u = e.first; int v = e.second; if (!actual_edges.check(u, v)) { actual_edges.add(u, v); add_event(u, v); } } // phase 3: Delete unnecessary edges not from 1 for (auto e : actual_edges.edges) { int u = e.first; int v = e.second; if (u != 1 && v != 1 && !good_edges.check(u, v) && actual_edges.check(u, v)) { // actual_edges.remove(u, v); remove_event(u, v); } } for (auto e : good_edges.edges) { G4[e.first].push_back(e.second); G4[e.second].push_back(e.first); } // phase 4: Delete unnecessary edges from 1 dfs_phase4(1); cout << result.size() << "\n"; for (auto e : result) { print_event(e); } }
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 | #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = long double; using vi = vector<int>; using vll = vector<ll>; using vii = vector<pii>; const int N = 3e4 + 10; vector<int> G[N]; vector<int> G4[N]; struct Edges { set<pii> edges; void add(int u, int v) { if (u > v) { swap(u, v); } edges.insert({u, v}); } bool check(int u, int v) { if (u > v) { swap(u, v); } return edges.count({u, v}); } void remove(int u, int v) { if (u > v) { swap(u, v); } edges.erase({u, v}); } }; struct Event { int u, v; bool type; Event(int u_, int v_, bool type_) : u(u_), v(v_), type(type_) {} }; vector<Event> result; void add_event(int u, int v) { result.push_back(Event(u, v, true)); } void remove_event(int u, int v) { result.push_back(Event(u, v, false)); } void print_event(Event e) { cout << (e.type ? "+" : "-") << " " << e.u << " " << e.v << "\n"; } Edges actual_edges, good_edges, bad_edges; bool vis[N]; void dfs_phase1(int v) { vis[v] = true; if (v != 1 && !actual_edges.check(1, v)) { add_event(1, v); actual_edges.add(1, v); } for (auto u : G[v]) { if (!vis[u]) { dfs_phase1(u); } } } void dfs_phase4(int v) { vis[v] = true; for (auto u : G4[v]) { if (!vis[u]) { dfs_phase4(u); } } if (v != 1 && !good_edges.check(1, v) && actual_edges.check(1, v)) { remove_event(1, v); } } int main() { ios_base::sync_with_stdio(false); cin.tie(); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); actual_edges.add(u, v); } int m2; cin >> m2; for (int i = 0; i < m2; i++) { int u, v; cin >> u >> v; good_edges.add(u, v); } dfs_phase1(1); fill(vis + 1, vis + n + 1, false); // phase 2: add all necessary edges for (auto e : good_edges.edges) { int u = e.first; int v = e.second; if (!actual_edges.check(u, v)) { actual_edges.add(u, v); add_event(u, v); } } // phase 3: Delete unnecessary edges not from 1 for (auto e : actual_edges.edges) { int u = e.first; int v = e.second; if (u != 1 && v != 1 && !good_edges.check(u, v) && actual_edges.check(u, v)) { // actual_edges.remove(u, v); remove_event(u, v); } } for (auto e : good_edges.edges) { G4[e.first].push_back(e.second); G4[e.second].push_back(e.first); } // phase 4: Delete unnecessary edges from 1 dfs_phase4(1); cout << result.size() << "\n"; for (auto e : result) { print_event(e); } } |