#include <bits/stdc++.h> using namespace std; typedef long long ll; class V { public: bool connected_to_one; }; class E { public: int rev; }; template <class V, class E> struct Graph { struct Ed : E { int v; Ed(E p, int w) : E(p), v(w) { } }; struct Ve : V, vector<Ed> { bool visited; }; vector<Ve> g; Graph(int n = 0) : g(n) { } void EdgeD(int b, int e, E d = E()) { g[b].push_back(Ed(d, e)); } void EdgeU(int b, int e, E d = E()) { Ed eg(d, e); eg.rev = g[e].size() + (b == e); g[b].push_back(eg); eg.rev = g[eg.v = b].size() - 1; g[e].push_back(eg); } vector<int> commands; void dfs_join(int u) { g[u].visited = 1; if (!g[u].connected_to_one && u != 1) { commands.push_back(u); } for (auto it = g[u].begin(); it != g[u].end(); it++) { if (!g[it->v].visited) { dfs_join(it->v); } } return; } void DFS_join() { for (int i = 0; i < g.size(); i++) { g[i].visited = 0; } for (auto it = g[1].begin(); it != g[1].end(); it++) { g[it->v].connected_to_one = 1; } dfs_join(1); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, ms, md; cin >> n; cin >> ms; Graph<V, E> Gs(n + 1); pair<int, int> initial_edges[ms]; int cnt_s = 0; for (int i = 0; i < ms; i++) { cin >> initial_edges[i].first >> initial_edges[i].second; Gs.EdgeU(initial_edges[i].first, initial_edges[i].second); if (initial_edges[i].first != 1 && initial_edges[i].second != 1) { cnt_s++; } } Graph<V, E> Gd(n + 1); cin >> md; pair<int, int> final_edges[md]; int cnt_d = 0; for (int i = 0; i < md; i++) { cin >> final_edges[i].first >> final_edges[i].second; Gd.EdgeU(final_edges[i].first, final_edges[i].second); if (final_edges[i].first != 1 && final_edges[i].second != 1) { cnt_d++; } } Gs.DFS_join(); Gd.DFS_join(); int r = Gs.commands.size() + cnt_s + cnt_d + Gd.commands.size(); cout << r << "\n"; for (int i = 0; i < Gs.commands.size(); i++) { cout << "+ 1 " << Gs.commands[i] << "\n"; } for (int i = 0; i < ms; i++) { if (initial_edges[i].first != 1 && initial_edges[i].second != 1) { cout << "- " << initial_edges[i].first << " " << initial_edges[i].second << "\n"; } } for (int i = 0; i < md; i++) { if (final_edges[i].first != 1 && final_edges[i].second != 1) { cout << "+ " << final_edges[i].first << " " << final_edges[i].second << "\n"; } } for (int i = Gd.commands.size() - 1; i >= 0; i--) { cout << "- 1 " << Gd.commands[i] << "\n"; } 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; class V { public: bool connected_to_one; }; class E { public: int rev; }; template <class V, class E> struct Graph { struct Ed : E { int v; Ed(E p, int w) : E(p), v(w) { } }; struct Ve : V, vector<Ed> { bool visited; }; vector<Ve> g; Graph(int n = 0) : g(n) { } void EdgeD(int b, int e, E d = E()) { g[b].push_back(Ed(d, e)); } void EdgeU(int b, int e, E d = E()) { Ed eg(d, e); eg.rev = g[e].size() + (b == e); g[b].push_back(eg); eg.rev = g[eg.v = b].size() - 1; g[e].push_back(eg); } vector<int> commands; void dfs_join(int u) { g[u].visited = 1; if (!g[u].connected_to_one && u != 1) { commands.push_back(u); } for (auto it = g[u].begin(); it != g[u].end(); it++) { if (!g[it->v].visited) { dfs_join(it->v); } } return; } void DFS_join() { for (int i = 0; i < g.size(); i++) { g[i].visited = 0; } for (auto it = g[1].begin(); it != g[1].end(); it++) { g[it->v].connected_to_one = 1; } dfs_join(1); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, ms, md; cin >> n; cin >> ms; Graph<V, E> Gs(n + 1); pair<int, int> initial_edges[ms]; int cnt_s = 0; for (int i = 0; i < ms; i++) { cin >> initial_edges[i].first >> initial_edges[i].second; Gs.EdgeU(initial_edges[i].first, initial_edges[i].second); if (initial_edges[i].first != 1 && initial_edges[i].second != 1) { cnt_s++; } } Graph<V, E> Gd(n + 1); cin >> md; pair<int, int> final_edges[md]; int cnt_d = 0; for (int i = 0; i < md; i++) { cin >> final_edges[i].first >> final_edges[i].second; Gd.EdgeU(final_edges[i].first, final_edges[i].second); if (final_edges[i].first != 1 && final_edges[i].second != 1) { cnt_d++; } } Gs.DFS_join(); Gd.DFS_join(); int r = Gs.commands.size() + cnt_s + cnt_d + Gd.commands.size(); cout << r << "\n"; for (int i = 0; i < Gs.commands.size(); i++) { cout << "+ 1 " << Gs.commands[i] << "\n"; } for (int i = 0; i < ms; i++) { if (initial_edges[i].first != 1 && initial_edges[i].second != 1) { cout << "- " << initial_edges[i].first << " " << initial_edges[i].second << "\n"; } } for (int i = 0; i < md; i++) { if (final_edges[i].first != 1 && final_edges[i].second != 1) { cout << "+ " << final_edges[i].first << " " << final_edges[i].second << "\n"; } } for (int i = Gd.commands.size() - 1; i >= 0; i--) { cout << "- 1 " << Gd.commands[i] << "\n"; } return 0; } |