#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <unordered_map> #include <assert.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/trie_policy.hpp> //using namespace __gnu_pbds using namespace std; template<class A, class B> ostream& operator<<(ostream& o, const pair<A, B>& p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class A, class B, class C> ostream& operator<<(ostream& o, const tuple<A, B, C>& t) { return o << '(' << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ')'; } template<class T> auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o) { o << '{'; int i = 0; for (const auto& e : x) { o << (", ") + 2 * !i++ << e; } return o << '}'; } auto operator<<(ostream& o, const string& s) -> decltype(o) { for (const auto& e : s) { o << e; } return o; } //#define DEBUG #ifdef DEBUG #define fastio() #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #define asrt(x) if (!(x)) { cerr << "Assertion failed: " << #x << " in line " << __LINE__ << endl; exit(1); } #else #define fastio() ios_base::sync_with_stdio(0); cin.tie(0); #define debug(...) #define asrt(x) #endif #define pii pair<int,int> #define inf 2'000'000'000 #define llinf 8'000'000'000'000'000'000 #define mod 998'244'353 #define mod7 1'000'000'007 #define mod9 1'000'000'009 #define eps 0.00000000001 #define all(x) x.begin(),x.end() #define rev(x) x.rbegin(),x.rend() #define decrease(x) x,vector<x>,greater<x> #define maxn 30'000 typedef long long ll; typedef long double ld; vector <tuple<bool, int, int> > ans; vector <int> g[maxn + 5]; bool visited[maxn + 5]; void clear_vis(int n) {for (int i = 0; i <= n; i++) visited[i] = false;} void centralize(int v) { visited[v] = true; bool b = false; for (auto i : g[v]) b |= i == 1; if (v != 1 && !b) { g[1].push_back(v); g[v].push_back(1); ans.push_back({true, 1, v}); } for (auto i: g[v]) if (!visited[i]) centralize(i); } vector <int> gg[maxn + 5]; vector <pii> to_add; void merge_fill(int n) { //adding for (int i = 1; i <= n; i++) { sort(all(g[i])); sort(all(gg[i])); int ind1 = 0, ind2 = 0; while (ind1 < (int)g[i].size() && ind2 < (int)gg[i].size()) { if (g[i][ind1] == gg[i][ind2]) { ind1++; ind2++; } else if (g[i][ind1] < gg[i][ind2]) ind1++; else { if (gg[i][ind2] < i) to_add.push_back({i, gg[i][ind2]}); ind2++; } } for (int j = ind2; j < (int)gg[i].size(); j++) if (gg[i][j] < i) to_add.push_back({i, gg[i][j]}); } for (auto [a, b]: to_add) { ans.push_back({1, a, b}); g[a].push_back(b); g[b].push_back(a); } //removing for (int i = 1; i <= n; i++) { sort(all(g[i])); int ind1 = 0, ind2 = 0; g[i].push_back(inf); gg[i].push_back(inf); debug(i); debug(g[i]); debug(gg[i]); while (ind1 < (int)g[i].size() && ind2 < (int)gg[i].size()) { if (g[i][ind1] == gg[i][ind2]) { ind1++; ind2++; } else if (g[i][ind1] < gg[i][ind2]) { if (i > 1 && g[i][ind1] > 1) { if (g[i][ind1] < i) ans.push_back({0, g[i][ind1], i}); g[i][ind1] = inf; } ind1++; } else ind2++; } } } void dfs(int v) { debug(v, g[v]); visited[v] = true; for (auto i: g[v]) if (i != inf && i != 1) if (!visited[i]) dfs(i); if (gg[v][0] != 1 && v != 1) ans.push_back({0, 1, v}); } void decentralize() { gg[1].pop_back(); for (auto i: gg[1]) dfs(i); } void solve() { int n, ms; scanf("%d%d", &n, &ms); while (ms--) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } centralize(1); int md; scanf("%d", &md); for (int i = 1; i <= md; i++) { int a, b; scanf("%d%d", &a, &b); // debug(a, b); gg[a].push_back(b); gg[b].push_back(a); } merge_fill(n); clear_vis(n); decentralize(); printf("%d\n", (int)ans.size()); for (auto [a, b, c]: ans) printf("%c %d %d\n", '-' + ('+' - '-') * a, b, c); } signed main() { int t = 1; //cin >> t; while (t--) solve(); }
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <unordered_map> #include <assert.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/trie_policy.hpp> //using namespace __gnu_pbds using namespace std; template<class A, class B> ostream& operator<<(ostream& o, const pair<A, B>& p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class A, class B, class C> ostream& operator<<(ostream& o, const tuple<A, B, C>& t) { return o << '(' << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ')'; } template<class T> auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o) { o << '{'; int i = 0; for (const auto& e : x) { o << (", ") + 2 * !i++ << e; } return o << '}'; } auto operator<<(ostream& o, const string& s) -> decltype(o) { for (const auto& e : s) { o << e; } return o; } //#define DEBUG #ifdef DEBUG #define fastio() #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #define asrt(x) if (!(x)) { cerr << "Assertion failed: " << #x << " in line " << __LINE__ << endl; exit(1); } #else #define fastio() ios_base::sync_with_stdio(0); cin.tie(0); #define debug(...) #define asrt(x) #endif #define pii pair<int,int> #define inf 2'000'000'000 #define llinf 8'000'000'000'000'000'000 #define mod 998'244'353 #define mod7 1'000'000'007 #define mod9 1'000'000'009 #define eps 0.00000000001 #define all(x) x.begin(),x.end() #define rev(x) x.rbegin(),x.rend() #define decrease(x) x,vector<x>,greater<x> #define maxn 30'000 typedef long long ll; typedef long double ld; vector <tuple<bool, int, int> > ans; vector <int> g[maxn + 5]; bool visited[maxn + 5]; void clear_vis(int n) {for (int i = 0; i <= n; i++) visited[i] = false;} void centralize(int v) { visited[v] = true; bool b = false; for (auto i : g[v]) b |= i == 1; if (v != 1 && !b) { g[1].push_back(v); g[v].push_back(1); ans.push_back({true, 1, v}); } for (auto i: g[v]) if (!visited[i]) centralize(i); } vector <int> gg[maxn + 5]; vector <pii> to_add; void merge_fill(int n) { //adding for (int i = 1; i <= n; i++) { sort(all(g[i])); sort(all(gg[i])); int ind1 = 0, ind2 = 0; while (ind1 < (int)g[i].size() && ind2 < (int)gg[i].size()) { if (g[i][ind1] == gg[i][ind2]) { ind1++; ind2++; } else if (g[i][ind1] < gg[i][ind2]) ind1++; else { if (gg[i][ind2] < i) to_add.push_back({i, gg[i][ind2]}); ind2++; } } for (int j = ind2; j < (int)gg[i].size(); j++) if (gg[i][j] < i) to_add.push_back({i, gg[i][j]}); } for (auto [a, b]: to_add) { ans.push_back({1, a, b}); g[a].push_back(b); g[b].push_back(a); } //removing for (int i = 1; i <= n; i++) { sort(all(g[i])); int ind1 = 0, ind2 = 0; g[i].push_back(inf); gg[i].push_back(inf); debug(i); debug(g[i]); debug(gg[i]); while (ind1 < (int)g[i].size() && ind2 < (int)gg[i].size()) { if (g[i][ind1] == gg[i][ind2]) { ind1++; ind2++; } else if (g[i][ind1] < gg[i][ind2]) { if (i > 1 && g[i][ind1] > 1) { if (g[i][ind1] < i) ans.push_back({0, g[i][ind1], i}); g[i][ind1] = inf; } ind1++; } else ind2++; } } } void dfs(int v) { debug(v, g[v]); visited[v] = true; for (auto i: g[v]) if (i != inf && i != 1) if (!visited[i]) dfs(i); if (gg[v][0] != 1 && v != 1) ans.push_back({0, 1, v}); } void decentralize() { gg[1].pop_back(); for (auto i: gg[1]) dfs(i); } void solve() { int n, ms; scanf("%d%d", &n, &ms); while (ms--) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } centralize(1); int md; scanf("%d", &md); for (int i = 1; i <= md; i++) { int a, b; scanf("%d%d", &a, &b); // debug(a, b); gg[a].push_back(b); gg[b].push_back(a); } merge_fill(n); clear_vis(n); decentralize(); printf("%d\n", (int)ans.size()); for (auto [a, b, c]: ans) printf("%c %d %d\n", '-' + ('+' - '-') * a, b, c); } signed main() { int t = 1; //cin >> t; while (t--) solve(); } |