#include <bits/stdc++.h> typedef long long int lli; using namespace std; #define MAX4 10010 #define MAX5 100010 #define MAX6 1000010 #define FOR(n) for (int i = 0; i < n; i++) using vti = vector<int>; using vts = vector<string>; template <typename T> void dbg(T last) { cout << last << "\n"; } template <typename T, typename... args> void dbg(T current, args... next) { cout << current << ' '; dbg(next...); } template <typename T> void getInput(vector<T> &v, int n, int x = 0) { for (int i = x; i < n; i++) { cin >> v[i]; } } //-----------------------------------------------------------------------------------------------// int cnt[MAX6]; set<int> tab1[MAX5]; set<int> tab2[MAX5]; set<int> visited1; void solve() { int n; cin >> n; int q1, q2; cin >> q1; for (int i = 0; i < q1; i++) { int a, b; cin >> a >> b; tab1[a].insert(b); tab1[b].insert(a); } queue<int> q; vector<pair<int, int>> del; vector<pair<int, int>> add; for (int x : tab1[1]) { q.push(x); visited1.insert(x); } visited1.insert(1); while (!q.empty()) { int x = q.front(); q.pop(); for (int y : tab1[x]) { if (visited1.insert(y).second) { add.push_back({1, y}); q.push(y); } } } cin >> q2; for (int i = 0; i < q2; i++) { int a, b; cin >> a >> b; tab2[a].insert(b); tab2[b].insert(a); } for (int i = 2; i <= n; i++) { for (int x : tab2[i]) { if (x != 1 && !tab1[i].count(x)) { add.push_back({i, x}); tab1[x].insert(i); tab1[i].insert(x); } } } for (int i = 2; i <= n; i++) { for (int x : tab1[i]) { if (x != 1 && !tab2[i].count(x)) { del.push_back({i, x}); tab1[x].erase(i); } } } visited1.clear(); for (int x : tab2[1]) { q.push(x); visited1.insert(x); } visited1.insert(1); stack<int> s; while (!q.empty()) { int x = q.front(); q.pop(); for (int y : tab2[x]) { if (visited1.insert(y).second) { q.push(y); s.push(y); } } } while (!s.empty()) { del.push_back({1, s.top()}); s.pop(); } std::cout << add.size() + del.size() << "\n"; for (auto p : add) { cout << "+ " << p.first << " " << p.second << "\n"; } for (auto p : del) { cout << "- " << p.first << " " << p.second << "\n"; } } int main() { std::ios::sync_with_stdio(false); solve(); 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 135 | #include <bits/stdc++.h> typedef long long int lli; using namespace std; #define MAX4 10010 #define MAX5 100010 #define MAX6 1000010 #define FOR(n) for (int i = 0; i < n; i++) using vti = vector<int>; using vts = vector<string>; template <typename T> void dbg(T last) { cout << last << "\n"; } template <typename T, typename... args> void dbg(T current, args... next) { cout << current << ' '; dbg(next...); } template <typename T> void getInput(vector<T> &v, int n, int x = 0) { for (int i = x; i < n; i++) { cin >> v[i]; } } //-----------------------------------------------------------------------------------------------// int cnt[MAX6]; set<int> tab1[MAX5]; set<int> tab2[MAX5]; set<int> visited1; void solve() { int n; cin >> n; int q1, q2; cin >> q1; for (int i = 0; i < q1; i++) { int a, b; cin >> a >> b; tab1[a].insert(b); tab1[b].insert(a); } queue<int> q; vector<pair<int, int>> del; vector<pair<int, int>> add; for (int x : tab1[1]) { q.push(x); visited1.insert(x); } visited1.insert(1); while (!q.empty()) { int x = q.front(); q.pop(); for (int y : tab1[x]) { if (visited1.insert(y).second) { add.push_back({1, y}); q.push(y); } } } cin >> q2; for (int i = 0; i < q2; i++) { int a, b; cin >> a >> b; tab2[a].insert(b); tab2[b].insert(a); } for (int i = 2; i <= n; i++) { for (int x : tab2[i]) { if (x != 1 && !tab1[i].count(x)) { add.push_back({i, x}); tab1[x].insert(i); tab1[i].insert(x); } } } for (int i = 2; i <= n; i++) { for (int x : tab1[i]) { if (x != 1 && !tab2[i].count(x)) { del.push_back({i, x}); tab1[x].erase(i); } } } visited1.clear(); for (int x : tab2[1]) { q.push(x); visited1.insert(x); } visited1.insert(1); stack<int> s; while (!q.empty()) { int x = q.front(); q.pop(); for (int y : tab2[x]) { if (visited1.insert(y).second) { q.push(y); s.push(y); } } } while (!s.empty()) { del.push_back({1, s.top()}); s.pop(); } std::cout << add.size() + del.size() << "\n"; for (auto p : add) { cout << "+ " << p.first << " " << p.second << "\n"; } for (auto p : del) { cout << "- " << p.first << " " << p.second << "\n"; } } int main() { std::ios::sync_with_stdio(false); solve(); return 0; } |