#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < n; i++) #define f first #define s second #define pb push_back #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() using vi = vector<int>; using ii = pair<int, int>; int n; ii get_edge(int a, int b) { return {min(a, b), max(a, b)}; } set<ii> edges, e2; vector<pair<char, ii>> ans; void usuwaj(int a, int b) { ii edge = get_edge(a, b); // assert(edges.count(edge)); ans.pb({'-', edge}); // cout << "- " << a << " " << b << endl; edges.erase(edge); } void dodaj(int a, int b) { ii edge = get_edge(a, b); // assert(!edges.count(edge)); // cout << "+ " << a << " " << b << endl; ans.pb({'+', edge}); edges.insert(edge); } void solve() { int m1, m2; cin >> n; cin >> m1; vector<vi> g1(n + 1), g2(n + 1); FOR(i, m1) { int a, b; cin >> a >> b; edges.insert(get_edge(a, b)); g1[a].pb(b); g1[b].pb(a); } cin >> m2; FOR(i, m2) { int a, b; cin >> a >> b; e2.insert(get_edge(a, b)); g2[a].pb(b); g2[b].pb(a); } vi vis(n + 1); set<ii> tree_edges; set<ii> later; function<void(int, int)> dfs = [&](int start, int p) { vis[start] = 1; for (int u : g1[start]) { if (u == p) { continue; } if (vis[u]) { if (u != 1 and start != 1) { later.insert(get_edge(start, u)); } } else { if (start != 1) { if (!edges.count(get_edge(u, 1))) { dodaj(u, 1); } usuwaj(start, u); } dfs(u, start); } } }; dfs(1, -1); // cout << "REMOVING TREE EDGES FINISHED " << endl; for (auto & [a, b] : later) { usuwaj(a, b); } later.clear(); // cout << "REMOVING NON-TREE EDGES FINISHED " << endl; // assert(sz(edges) == n - 1); vis = vi(n + 1); function<void(int, int, bool)> dfs2 = [&](int start, int p, bool change) { vis[start] = 1; for (int u : g2[start]) { if (u == p) continue; if (vis[u]) { if (not change) { if (start != 1 and u != 1) { later.insert(get_edge(start, u)); } } } else { dfs2(u, start, change); if (start != 1 and change) { dodaj(start, u); if (!e2.count(get_edge(1, u))) { usuwaj(1, u); } } } } }; dfs2(1, -1, false); vis = vi(n + 1); for (auto & u : later) { dodaj(u.f, u.s); } later.clear(); // cout << "ADDING NON-TREE EDGES FINISHED " << endl; dfs2(1, -1, true); // cout << "ADDING TREE EDGES FINISHED " << endl; // assert(edges == e2); cout << sz(ans) << '\n'; for(auto & [c, edge] : ans) { cout << c << ' ' << edge.f << ' ' << edge.s << '\n'; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int tests = 1; // cin >> tests; for (int test = 1; test <= tests; test++) { 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 192 193 194 | #pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < n; i++) #define f first #define s second #define pb push_back #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() using vi = vector<int>; using ii = pair<int, int>; int n; ii get_edge(int a, int b) { return {min(a, b), max(a, b)}; } set<ii> edges, e2; vector<pair<char, ii>> ans; void usuwaj(int a, int b) { ii edge = get_edge(a, b); // assert(edges.count(edge)); ans.pb({'-', edge}); // cout << "- " << a << " " << b << endl; edges.erase(edge); } void dodaj(int a, int b) { ii edge = get_edge(a, b); // assert(!edges.count(edge)); // cout << "+ " << a << " " << b << endl; ans.pb({'+', edge}); edges.insert(edge); } void solve() { int m1, m2; cin >> n; cin >> m1; vector<vi> g1(n + 1), g2(n + 1); FOR(i, m1) { int a, b; cin >> a >> b; edges.insert(get_edge(a, b)); g1[a].pb(b); g1[b].pb(a); } cin >> m2; FOR(i, m2) { int a, b; cin >> a >> b; e2.insert(get_edge(a, b)); g2[a].pb(b); g2[b].pb(a); } vi vis(n + 1); set<ii> tree_edges; set<ii> later; function<void(int, int)> dfs = [&](int start, int p) { vis[start] = 1; for (int u : g1[start]) { if (u == p) { continue; } if (vis[u]) { if (u != 1 and start != 1) { later.insert(get_edge(start, u)); } } else { if (start != 1) { if (!edges.count(get_edge(u, 1))) { dodaj(u, 1); } usuwaj(start, u); } dfs(u, start); } } }; dfs(1, -1); // cout << "REMOVING TREE EDGES FINISHED " << endl; for (auto & [a, b] : later) { usuwaj(a, b); } later.clear(); // cout << "REMOVING NON-TREE EDGES FINISHED " << endl; // assert(sz(edges) == n - 1); vis = vi(n + 1); function<void(int, int, bool)> dfs2 = [&](int start, int p, bool change) { vis[start] = 1; for (int u : g2[start]) { if (u == p) continue; if (vis[u]) { if (not change) { if (start != 1 and u != 1) { later.insert(get_edge(start, u)); } } } else { dfs2(u, start, change); if (start != 1 and change) { dodaj(start, u); if (!e2.count(get_edge(1, u))) { usuwaj(1, u); } } } } }; dfs2(1, -1, false); vis = vi(n + 1); for (auto & u : later) { dodaj(u.f, u.s); } later.clear(); // cout << "ADDING NON-TREE EDGES FINISHED " << endl; dfs2(1, -1, true); // cout << "ADDING TREE EDGES FINISHED " << endl; // assert(edges == e2); cout << sz(ans) << '\n'; for(auto & [c, edge] : ans) { cout << c << ' ' << edge.f << ' ' << edge.s << '\n'; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int tests = 1; // cin >> tests; for (int test = 1; test <= tests; test++) { solve(); } return 0; } |