#include <bits/stdc++.h> using namespace std; const int maxn = 3e5; vector<int> g1[maxn], g2[maxn]; set<pair<int, int> > edges1set, edges2set; vector<pair<int, int> > edges2, edges1; bool used[maxn]; struct Node { int type; int u; int v; }; vector<Node> ans; void add_edge_if_need(int u, int v) { if (edges1set.count({1, v})) return; Node node; node.type = 1, node.u = 1, node.v = v; ans.push_back(node); edges1set.insert({1, v}); edges1set.insert({v, 1}); } void dfs(int u, int p) { used[u] = false; for (const auto& ne : g1[u]) { if (!used[ne]) continue; if (u != 1) { add_edge_if_need(u, ne); } dfs(ne, u); } } void dfs2(int u, int p) { used[u] = false; for (const auto& ne : g1[u]) { if (!used[ne]) continue; if (u == 1 && !edges2set.count({1, ne})) { continue; } dfs2(ne, u); } if (u != 1 && !edges2set.count({1, u})) { Node node; node.type = -1, node.u = 1, node.v = u; ans.push_back(node); } } int main() { // freopen("input.txt", "r", stdin); ios_base::sync_with_stdio(false); int n, m1, m2; cin >> n >> m1; for (int i = 1; i <= m1; ++i) { int u, v; cin >> u >> v; g1[u].push_back(v); g1[v].push_back(u); edges1set.insert({u, v}); edges1set.insert({v, u}); edges1.push_back({u, v}); } cin >> m2; for (int i = 1; i <= m2; ++i) { int u, v; cin >> u >> v; edges2set.insert({u, v}); edges2set.insert({v, u}); edges2.push_back({u, v}); } // 1 -> all for (int i = 1; i <= n; ++i) used[i] = true; dfs(1, 1); // delete diff without node 1 for (const auto& e : edges2) { if (!edges1set.count(e)) { Node node; node.type = 1, node.u = e.first, node.v = e.second; ans.push_back(node); edges1set.insert({e.first, e.second}); edges1set.insert({e.second, e.first}); } } for (const auto&e : edges1) { if (e.first == 1 || e.second == 1) continue; if (edges2set.count(e)) { continue; } Node node; node.type = -1, node.u = e.first, node.v = e.second; ans.push_back(node); edges1set.erase({e.first, e.second}); edges1set.erase({e.second, e.first}); } // delete from node 1 for (int i = 1; i <= n; ++i) used[i] = true; for (int i = 1; i <= n; ++i) g1[i].clear(); for (const auto& e : edges1set) { int u = e.first, v = e.second; g1[u].push_back(v); g1[v].push_back(u); } dfs2(1, 1); cout << ans.size() << endl; for (const auto& node : ans) { if (node.type == 1) cout << "+ "; else cout << "- "; cout << node.u << " " << node.v << endl; } 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 | #include <bits/stdc++.h> using namespace std; const int maxn = 3e5; vector<int> g1[maxn], g2[maxn]; set<pair<int, int> > edges1set, edges2set; vector<pair<int, int> > edges2, edges1; bool used[maxn]; struct Node { int type; int u; int v; }; vector<Node> ans; void add_edge_if_need(int u, int v) { if (edges1set.count({1, v})) return; Node node; node.type = 1, node.u = 1, node.v = v; ans.push_back(node); edges1set.insert({1, v}); edges1set.insert({v, 1}); } void dfs(int u, int p) { used[u] = false; for (const auto& ne : g1[u]) { if (!used[ne]) continue; if (u != 1) { add_edge_if_need(u, ne); } dfs(ne, u); } } void dfs2(int u, int p) { used[u] = false; for (const auto& ne : g1[u]) { if (!used[ne]) continue; if (u == 1 && !edges2set.count({1, ne})) { continue; } dfs2(ne, u); } if (u != 1 && !edges2set.count({1, u})) { Node node; node.type = -1, node.u = 1, node.v = u; ans.push_back(node); } } int main() { // freopen("input.txt", "r", stdin); ios_base::sync_with_stdio(false); int n, m1, m2; cin >> n >> m1; for (int i = 1; i <= m1; ++i) { int u, v; cin >> u >> v; g1[u].push_back(v); g1[v].push_back(u); edges1set.insert({u, v}); edges1set.insert({v, u}); edges1.push_back({u, v}); } cin >> m2; for (int i = 1; i <= m2; ++i) { int u, v; cin >> u >> v; edges2set.insert({u, v}); edges2set.insert({v, u}); edges2.push_back({u, v}); } // 1 -> all for (int i = 1; i <= n; ++i) used[i] = true; dfs(1, 1); // delete diff without node 1 for (const auto& e : edges2) { if (!edges1set.count(e)) { Node node; node.type = 1, node.u = e.first, node.v = e.second; ans.push_back(node); edges1set.insert({e.first, e.second}); edges1set.insert({e.second, e.first}); } } for (const auto&e : edges1) { if (e.first == 1 || e.second == 1) continue; if (edges2set.count(e)) { continue; } Node node; node.type = -1, node.u = e.first, node.v = e.second; ans.push_back(node); edges1set.erase({e.first, e.second}); edges1set.erase({e.second, e.first}); } // delete from node 1 for (int i = 1; i <= n; ++i) used[i] = true; for (int i = 1; i <= n; ++i) g1[i].clear(); for (const auto& e : edges1set) { int u = e.first, v = e.second; g1[u].push_back(v); g1[v].push_back(u); } dfs2(1, 1); cout << ans.size() << endl; for (const auto& node : ans) { if (node.type == 1) cout << "+ "; else cout << "- "; cout << node.u << " " << node.v << endl; } return 0; } |