// // Jan Zachar 13/03/2024 // Alchemik Bajtazar // #include <iostream> #include <queue> #include <set> using namespace std; int n; set<int> A[30006], B[30006]; queue<pair<char, pair<int, int>>> Answ; int vis[30006]{}; // Connect every node to root void DFS_r(int v) { vis[v] = 1; for (auto &u : A[v]) { if (vis[u]) continue; if (!A[1].count(u)) { Answ.push({'+', {u, 1}}); A[u].insert(1); A[1].insert(u); } DFS_r(u); } } // Remove old connections to root void DFS_u(int v) { vis[v] = 2; for (auto &u : A[v]) { if (vis[u] == 2) continue; DFS_u(u); } if (v == 1) return; if (B[1].count(v)) return; Answ.push({'-', {v, 1}}); A[1].erase(v); A[v].erase(1); } int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); cin >> n; int mA; cin >> mA; while (mA--) { int a, b; cin >> a >> b; A[a].insert(b); A[b].insert(a); } int mB; cin >> mB; while (mB--) { int a, b; cin >> a >> b; B[a].insert(b); B[b].insert(a); } DFS_r(1); for (int v = 2; v <= n; v++) { for (auto &u: B[v]) { if (A[v].count(u)) continue; Answ.push({'+', {u, v}}); A[u].insert(v); A[v].insert(u); } } for (int v = 2; v <= n; v++) { for (auto u = A[v].begin(); u != A[v].end();) { if (*u == 1 || B[v].count(*u)) { u++; continue; } Answ.push({'-', {*u, v}}); A[*u].erase(v); u = A[v].erase(u); } } DFS_u(1); cout << Answ.size() << '\n'; while (!Answ.empty()) { auto p = Answ.front(); Answ.pop(); cout << p.first << ' '; cout << p.second.first << ' '; cout << p.second.second << '\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 | // // Jan Zachar 13/03/2024 // Alchemik Bajtazar // #include <iostream> #include <queue> #include <set> using namespace std; int n; set<int> A[30006], B[30006]; queue<pair<char, pair<int, int>>> Answ; int vis[30006]{}; // Connect every node to root void DFS_r(int v) { vis[v] = 1; for (auto &u : A[v]) { if (vis[u]) continue; if (!A[1].count(u)) { Answ.push({'+', {u, 1}}); A[u].insert(1); A[1].insert(u); } DFS_r(u); } } // Remove old connections to root void DFS_u(int v) { vis[v] = 2; for (auto &u : A[v]) { if (vis[u] == 2) continue; DFS_u(u); } if (v == 1) return; if (B[1].count(v)) return; Answ.push({'-', {v, 1}}); A[1].erase(v); A[v].erase(1); } int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); cin >> n; int mA; cin >> mA; while (mA--) { int a, b; cin >> a >> b; A[a].insert(b); A[b].insert(a); } int mB; cin >> mB; while (mB--) { int a, b; cin >> a >> b; B[a].insert(b); B[b].insert(a); } DFS_r(1); for (int v = 2; v <= n; v++) { for (auto &u: B[v]) { if (A[v].count(u)) continue; Answ.push({'+', {u, v}}); A[u].insert(v); A[v].insert(u); } } for (int v = 2; v <= n; v++) { for (auto u = A[v].begin(); u != A[v].end();) { if (*u == 1 || B[v].count(*u)) { u++; continue; } Answ.push({'-', {*u, v}}); A[*u].erase(v); u = A[v].erase(u); } } DFS_u(1); cout << Answ.size() << '\n'; while (!Answ.empty()) { auto p = Answ.front(); Answ.pop(); cout << p.first << ' '; cout << p.second.first << ' '; cout << p.second.second << '\n'; } return 0; } |