#include <iostream> #include <vector> #include <unordered_set> int n, m, a, b, roz = 0; struct answer { bool add; int a; int b; }; std::unordered_set<unsigned int> us; std::vector<int> neighs[30001], NEIGHS[30001]; std::vector<answer> ans; bool visited[30001], VISITED[30001]; unsigned int gen(unsigned int a, unsigned int b) { return a * 100000 + b; } void dfs(int x) { visited[x] = true; for (int i = 0; i < neighs[x].size(); i++) { if (!visited[neighs[x][i]]) { ans.push_back({ 1, 1, neighs[x][i] }); dfs(neighs[x][i]); } } } void DFS(int x, bool b) { VISITED[x] = true; for (int i = 0; i < NEIGHS[x].size(); i++) { if (!VISITED[NEIGHS[x][i]]) { DFS(NEIGHS[x][i], 1); } } if (b == 1) ans.push_back({ 0, 1, x }); } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cin >> n >> m; for (int i = 0; i < m; i++) { std::cin >> a >> b; if (b < a) std::swap(a, b); if (a != 1) us.insert(gen(a, b)); neighs[a].push_back(b); neighs[b].push_back(a); } visited[1] = true; for (int i = 0; i < neighs[1].size(); i++) visited[neighs[1][i]] = true; for (int i = 0; i < neighs[1].size(); i++) { dfs(neighs[1][i]); } std::cin >> m; for (int i = 0; i < m; i++) { std::cin >> a >> b; if (b < a) std::swap(a, b); NEIGHS[a].push_back(b); NEIGHS[b].push_back(a); if (a != 1) { if (us.find(gen(a, b)) != us.end()) { us.erase(us.find(gen(a, b))); } else { ans.push_back({ 1, a, b }); } } } for (auto itr = us.begin(); itr != us.end(); ++itr) ans.push_back({ 0, int(*itr / 100000), int(*itr % 100000) }); VISITED[1] = true; for (int i = 0; i < NEIGHS[1].size(); i++) VISITED[NEIGHS[1][i]] = true; for (int i = 0; i < NEIGHS[1].size(); i++) { DFS(NEIGHS[1][i], 0); } std::cout << ans.size(); for (int i = 0; i < ans.size(); i++) { if (ans[i].add) std::cout << "\n+ "; else std::cout << "\n- "; std::cout << ans[i].a << ' ' << ans[i].b; } }
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 | #include <iostream> #include <vector> #include <unordered_set> int n, m, a, b, roz = 0; struct answer { bool add; int a; int b; }; std::unordered_set<unsigned int> us; std::vector<int> neighs[30001], NEIGHS[30001]; std::vector<answer> ans; bool visited[30001], VISITED[30001]; unsigned int gen(unsigned int a, unsigned int b) { return a * 100000 + b; } void dfs(int x) { visited[x] = true; for (int i = 0; i < neighs[x].size(); i++) { if (!visited[neighs[x][i]]) { ans.push_back({ 1, 1, neighs[x][i] }); dfs(neighs[x][i]); } } } void DFS(int x, bool b) { VISITED[x] = true; for (int i = 0; i < NEIGHS[x].size(); i++) { if (!VISITED[NEIGHS[x][i]]) { DFS(NEIGHS[x][i], 1); } } if (b == 1) ans.push_back({ 0, 1, x }); } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cin >> n >> m; for (int i = 0; i < m; i++) { std::cin >> a >> b; if (b < a) std::swap(a, b); if (a != 1) us.insert(gen(a, b)); neighs[a].push_back(b); neighs[b].push_back(a); } visited[1] = true; for (int i = 0; i < neighs[1].size(); i++) visited[neighs[1][i]] = true; for (int i = 0; i < neighs[1].size(); i++) { dfs(neighs[1][i]); } std::cin >> m; for (int i = 0; i < m; i++) { std::cin >> a >> b; if (b < a) std::swap(a, b); NEIGHS[a].push_back(b); NEIGHS[b].push_back(a); if (a != 1) { if (us.find(gen(a, b)) != us.end()) { us.erase(us.find(gen(a, b))); } else { ans.push_back({ 1, a, b }); } } } for (auto itr = us.begin(); itr != us.end(); ++itr) ans.push_back({ 0, int(*itr / 100000), int(*itr % 100000) }); VISITED[1] = true; for (int i = 0; i < NEIGHS[1].size(); i++) VISITED[NEIGHS[1][i]] = true; for (int i = 0; i < NEIGHS[1].size(); i++) { DFS(NEIGHS[1][i], 0); } std::cout << ans.size(); for (int i = 0; i < ans.size(); i++) { if (ans[i].add) std::cout << "\n+ "; else std::cout << "\n- "; std::cout << ans[i].a << ' ' << ans[i].b; } } |