#include <bits/stdc++.h> using namespace std; vector <int> graf[30000], koncowyGraf[30000]; bool odwiedzone[30000]; vector <char> w1; vector <pair<int, int>> w2; int r = 0; void dfs (int start, unordered_set <int> secik) { //cout << start+1 << "\n"; odwiedzone[start] = 1; for (int i = 0; i < koncowyGraf[start].size(); i++) { int n = koncowyGraf[start][i]; if (odwiedzone[n] == 0) { dfs (n, secik); } } if (secik.find(start) == secik.end()) { r++; w1.push_back('-'); w2.push_back({0, start}); } return; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; int m1; cin >> m1; unordered_set <int> glownyWierzcholek; for (int i = 0, t1, t2; i < m1; i++) { cin >> t1 >> t2; t1--; t2--; graf[t1].push_back(t2); graf[t2].push_back(t1); if (t1 == 0) { glownyWierzcholek.insert(t2); } else if (t2 == 0) { glownyWierzcholek.insert(t1); } } odwiedzone[0] = 1; for (int i = 1; i < n; i++) { odwiedzone[i] = 0; } queue <int> q; for (int i = 0; i < graf[0].size(); i++) { q.push(graf[0][i]); odwiedzone[graf[0][i]] = 1; } while (!q.empty()) { int n2 = q.front(); q.pop(); for (int i = 0; i < graf[n2].size(); i++) { int n3 = graf[n2][i]; if (odwiedzone[n3] == 0) { r++; w1.push_back('+'); w2.push_back({0, n3}); q.push(n3); odwiedzone[n3] = 1; } } } for (int i = 0; i < n; i++) { odwiedzone[i] = 0; } odwiedzone[0] = 1; for (int i = 1; i < n; i++) { odwiedzone[i] = 1; for (int j = 0; j < graf[i].size(); j++) { if (odwiedzone[graf[i][j]] == 0) { r++; w1.push_back('-'); w2.push_back({graf[i][j], i}); } } } unordered_set <int> secik; int m2; cin >> m2; for (int i = 0, t1, t2; i < m2; i++) { cin >> t1 >> t2; t1--; t2--; koncowyGraf[t1].push_back(t2); koncowyGraf[t2].push_back(t1); if (t1 == 0) secik.insert(t2); else if (t2 == 0) secik.insert(t1); else { r++; w1.push_back('+'); w2.push_back({t1, t2}); } } /*for (auto itr = secik.begin(); itr != secik.end(); itr++) { cout << *itr+1 << ' '; } cout << "\n";*/ for (int i = 0; i < n; i++) { odwiedzone[i] = 0; } odwiedzone[0] = 1; for (int i = 0; i < koncowyGraf[0].size(); i++) { dfs (koncowyGraf[0][i], secik); } cout << r << "\n"; for (int i = 0; i < r; i++) { cout << w1[i] << ' ' << w2[i].first+1 << ' ' << w2[i].second+1 << "\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 128 129 130 131 132 133 134 | #include <bits/stdc++.h> using namespace std; vector <int> graf[30000], koncowyGraf[30000]; bool odwiedzone[30000]; vector <char> w1; vector <pair<int, int>> w2; int r = 0; void dfs (int start, unordered_set <int> secik) { //cout << start+1 << "\n"; odwiedzone[start] = 1; for (int i = 0; i < koncowyGraf[start].size(); i++) { int n = koncowyGraf[start][i]; if (odwiedzone[n] == 0) { dfs (n, secik); } } if (secik.find(start) == secik.end()) { r++; w1.push_back('-'); w2.push_back({0, start}); } return; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; int m1; cin >> m1; unordered_set <int> glownyWierzcholek; for (int i = 0, t1, t2; i < m1; i++) { cin >> t1 >> t2; t1--; t2--; graf[t1].push_back(t2); graf[t2].push_back(t1); if (t1 == 0) { glownyWierzcholek.insert(t2); } else if (t2 == 0) { glownyWierzcholek.insert(t1); } } odwiedzone[0] = 1; for (int i = 1; i < n; i++) { odwiedzone[i] = 0; } queue <int> q; for (int i = 0; i < graf[0].size(); i++) { q.push(graf[0][i]); odwiedzone[graf[0][i]] = 1; } while (!q.empty()) { int n2 = q.front(); q.pop(); for (int i = 0; i < graf[n2].size(); i++) { int n3 = graf[n2][i]; if (odwiedzone[n3] == 0) { r++; w1.push_back('+'); w2.push_back({0, n3}); q.push(n3); odwiedzone[n3] = 1; } } } for (int i = 0; i < n; i++) { odwiedzone[i] = 0; } odwiedzone[0] = 1; for (int i = 1; i < n; i++) { odwiedzone[i] = 1; for (int j = 0; j < graf[i].size(); j++) { if (odwiedzone[graf[i][j]] == 0) { r++; w1.push_back('-'); w2.push_back({graf[i][j], i}); } } } unordered_set <int> secik; int m2; cin >> m2; for (int i = 0, t1, t2; i < m2; i++) { cin >> t1 >> t2; t1--; t2--; koncowyGraf[t1].push_back(t2); koncowyGraf[t2].push_back(t1); if (t1 == 0) secik.insert(t2); else if (t2 == 0) secik.insert(t1); else { r++; w1.push_back('+'); w2.push_back({t1, t2}); } } /*for (auto itr = secik.begin(); itr != secik.end(); itr++) { cout << *itr+1 << ' '; } cout << "\n";*/ for (int i = 0; i < n; i++) { odwiedzone[i] = 0; } odwiedzone[0] = 1; for (int i = 0; i < koncowyGraf[0].size(); i++) { dfs (koncowyGraf[0][i], secik); } cout << r << "\n"; for (int i = 0; i < r; i++) { cout << w1[i] << ' ' << w2[i].first+1 << ' ' << w2[i].second+1 << "\n"; } return 0; } |