#include <bits/stdc++.h> using namespace std; int amountOfNodes; int amountOfInitialConnections; int amountOfTargetConnections; set<int> graph[30007]; set<int> target[30007]; vector<pair<char, pair<int, int>>> steps; bool visitedAdd[30007]; bool visitedRemove[30007]; void dfsAdd(int node) { for (int child : graph[node]) { if (!visitedAdd[child]) { visitedAdd[child] = true; graph[1].insert(child); graph[child].insert(1); steps.emplace_back('+', make_pair(1, child)); dfsAdd(child); } } } void dfsRemove(int node) { for (int child : graph[node]) { if (!visitedRemove[child]) { visitedRemove[child] = true; dfsRemove(child); if (target[1].count(child) == 0) { steps.emplace_back('-', make_pair(1, child)); } } } } void addMissing() { for (int i = 2; i <= amountOfNodes; ++i) { for (int child : target[i]) { if (graph[i].count(child) == 0) { graph[i].insert(child); graph[child].insert(i); steps.emplace_back('+', make_pair(i, child)); } } vector<pair<int, int>> toErase; for (int child : graph[i]) { if (target[i].count(child) == 0 && child != 1) { toErase.emplace_back(i, child); steps.emplace_back('-', make_pair(i, child)); } } for (auto value : toErase) { graph[value.first].erase(value.second); graph[value.second].erase(value.first); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> amountOfNodes; cin >> amountOfInitialConnections; for (int i = 0; i < amountOfInitialConnections; ++i) { int first, second; cin >> first >> second; graph[first].insert(second); graph[second].insert(first); } cin >> amountOfTargetConnections; for (int i = 0; i < amountOfTargetConnections; ++i) { int first, second; cin >> first >> second; target[first].insert(second); target[second].insert(first); } visitedAdd[1] = true; for (int child : graph[1]) { visitedAdd[child] = true; } for (int child : graph[1]) { dfsAdd(child); } addMissing(); visitedRemove[1] = true; for (int child : target[1]) { visitedRemove[child] = true; } for (int child : target[1]) { dfsRemove(child); } cout << steps.size() << '\n'; for (auto step : steps) { cout << step.first << ' ' << step.second.first << ' ' << step.second.second << '\n'; } }
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 | #include <bits/stdc++.h> using namespace std; int amountOfNodes; int amountOfInitialConnections; int amountOfTargetConnections; set<int> graph[30007]; set<int> target[30007]; vector<pair<char, pair<int, int>>> steps; bool visitedAdd[30007]; bool visitedRemove[30007]; void dfsAdd(int node) { for (int child : graph[node]) { if (!visitedAdd[child]) { visitedAdd[child] = true; graph[1].insert(child); graph[child].insert(1); steps.emplace_back('+', make_pair(1, child)); dfsAdd(child); } } } void dfsRemove(int node) { for (int child : graph[node]) { if (!visitedRemove[child]) { visitedRemove[child] = true; dfsRemove(child); if (target[1].count(child) == 0) { steps.emplace_back('-', make_pair(1, child)); } } } } void addMissing() { for (int i = 2; i <= amountOfNodes; ++i) { for (int child : target[i]) { if (graph[i].count(child) == 0) { graph[i].insert(child); graph[child].insert(i); steps.emplace_back('+', make_pair(i, child)); } } vector<pair<int, int>> toErase; for (int child : graph[i]) { if (target[i].count(child) == 0 && child != 1) { toErase.emplace_back(i, child); steps.emplace_back('-', make_pair(i, child)); } } for (auto value : toErase) { graph[value.first].erase(value.second); graph[value.second].erase(value.first); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> amountOfNodes; cin >> amountOfInitialConnections; for (int i = 0; i < amountOfInitialConnections; ++i) { int first, second; cin >> first >> second; graph[first].insert(second); graph[second].insert(first); } cin >> amountOfTargetConnections; for (int i = 0; i < amountOfTargetConnections; ++i) { int first, second; cin >> first >> second; target[first].insert(second); target[second].insert(first); } visitedAdd[1] = true; for (int child : graph[1]) { visitedAdd[child] = true; } for (int child : graph[1]) { dfsAdd(child); } addMissing(); visitedRemove[1] = true; for (int child : target[1]) { visitedRemove[child] = true; } for (int child : target[1]) { dfsRemove(child); } cout << steps.size() << '\n'; for (auto step : steps) { cout << step.first << ' ' << step.second.first << ' ' << step.second.second << '\n'; } } |