#include <bits/stdc++.h> #include <fstream> #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<size_t Index = 0, typename... Types> ostream& printTupleElements(ostream& o, const tuple<Types...>& t) {if constexpr (Index < sizeof...(Types)){if(Index > 0){o << ", ";}o << get<Index>(t);printTupleElements<Index + 1>(o, t);}return o;} template<typename... Types> ostream& operator<<(ostream& o, const tuple<Types...>& t){o << "(";printTupleElements(o, t);return o << ")";} template<class T> auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o){o << '{';bool first = true;for (const auto& e : x){if (!first){o << ", ";}o << e;first = false;} return o << '}';} struct custom_hash {static uint64_t splitmix64(uint64_t x) {x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x = (x ^ (x >> 27)) * 0x94d049bb133111eb;return x ^ (x >> 31);} size_t operator()(uint64_t x) const {static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();return splitmix64(x + FIXED_RANDOM);}}; //#define DEBUG #ifdef DEBUG #define fastio() #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define fastio() ios_base::sync_with_stdio(0); cin.tie(0); #define debug(...) #endif typedef long long ll; typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; #define pi pair<int, int> #define pll pair<ll, ll> #define st first #define nd second #define vi vector<int> #define vll vector<ll> #define eb emplace_back #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() void solve() { //ifstream cin("nazwa.in"); //ofstream cout("nazwa.out"); int n; cin >> n; int m1; cin >> m1; vector<vi>g1(n + 1); vector<pi>kr1; for(int i = 1; i <= m1; i++) { int a, b; cin >> a >> b; g1[a].eb(b); g1[b].eb(a); kr1.eb(a, b); } int m2; cin >> m2; vector<vi>g2(n + 1); map<pi, bool>czy2; for(int i = 1; i <= m2; i++) { int a, b; cin >> a >> b; czy2[{a, b}] = 1; g2[a].eb(b); g2[b].eb(a); } vector<tuple<int, int, int> >res; auto bfs = [&](int v, vector<vi>&g) { vi dist(n + 1, 1e9); queue<int>q; q.emplace(v); dist[v] = 0; vector<pi> ans; while(!q.empty()) { int v = q.front(); q.pop(); ans.eb(v, dist[v]); for(auto x : g[v]) { if(dist[x] == 1e9) { dist[x] = dist[v] + 1; q.emplace(x); } } } return ans; }; vector<pi> kol = bfs(1, g1); for(auto [v, d] : kol) { if(d > 1) { res.eb(1, 1, v); } } for(auto [a, b] : kr1) { if(a == 1 || b == 1) { continue; } if(czy2[{a, b}] || czy2[{b, a}]) { czy2[{a, b}] = 0; czy2[{b, a}] = 0; } else { res.eb(2, a, b); } } for(auto [kt, val] : czy2) { if(!val || kt.st == 1 || kt.nd == 1) { continue; } res.eb(1, kt.st, kt.nd); } kol = bfs(1, g2); reverse(all(kol)); for(auto [v, d] : kol) { if(d > 1) { res.eb(2, 1, v); } } cout << sz(res) << '\n'; for(auto [typ, a, b] : res) { if(typ == 1) { cout << "+ "; } else { cout << "- "; } cout << a << ' ' << b << '\n'; } } int main() { fastio(); 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 | #include <bits/stdc++.h> #include <fstream> #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<size_t Index = 0, typename... Types> ostream& printTupleElements(ostream& o, const tuple<Types...>& t) {if constexpr (Index < sizeof...(Types)){if(Index > 0){o << ", ";}o << get<Index>(t);printTupleElements<Index + 1>(o, t);}return o;} template<typename... Types> ostream& operator<<(ostream& o, const tuple<Types...>& t){o << "(";printTupleElements(o, t);return o << ")";} template<class T> auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o){o << '{';bool first = true;for (const auto& e : x){if (!first){o << ", ";}o << e;first = false;} return o << '}';} struct custom_hash {static uint64_t splitmix64(uint64_t x) {x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x = (x ^ (x >> 27)) * 0x94d049bb133111eb;return x ^ (x >> 31);} size_t operator()(uint64_t x) const {static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();return splitmix64(x + FIXED_RANDOM);}}; //#define DEBUG #ifdef DEBUG #define fastio() #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define fastio() ios_base::sync_with_stdio(0); cin.tie(0); #define debug(...) #endif typedef long long ll; typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; #define pi pair<int, int> #define pll pair<ll, ll> #define st first #define nd second #define vi vector<int> #define vll vector<ll> #define eb emplace_back #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() void solve() { //ifstream cin("nazwa.in"); //ofstream cout("nazwa.out"); int n; cin >> n; int m1; cin >> m1; vector<vi>g1(n + 1); vector<pi>kr1; for(int i = 1; i <= m1; i++) { int a, b; cin >> a >> b; g1[a].eb(b); g1[b].eb(a); kr1.eb(a, b); } int m2; cin >> m2; vector<vi>g2(n + 1); map<pi, bool>czy2; for(int i = 1; i <= m2; i++) { int a, b; cin >> a >> b; czy2[{a, b}] = 1; g2[a].eb(b); g2[b].eb(a); } vector<tuple<int, int, int> >res; auto bfs = [&](int v, vector<vi>&g) { vi dist(n + 1, 1e9); queue<int>q; q.emplace(v); dist[v] = 0; vector<pi> ans; while(!q.empty()) { int v = q.front(); q.pop(); ans.eb(v, dist[v]); for(auto x : g[v]) { if(dist[x] == 1e9) { dist[x] = dist[v] + 1; q.emplace(x); } } } return ans; }; vector<pi> kol = bfs(1, g1); for(auto [v, d] : kol) { if(d > 1) { res.eb(1, 1, v); } } for(auto [a, b] : kr1) { if(a == 1 || b == 1) { continue; } if(czy2[{a, b}] || czy2[{b, a}]) { czy2[{a, b}] = 0; czy2[{b, a}] = 0; } else { res.eb(2, a, b); } } for(auto [kt, val] : czy2) { if(!val || kt.st == 1 || kt.nd == 1) { continue; } res.eb(1, kt.st, kt.nd); } kol = bfs(1, g2); reverse(all(kol)); for(auto [v, d] : kol) { if(d > 1) { res.eb(2, 1, v); } } cout << sz(res) << '\n'; for(auto [typ, a, b] : res) { if(typ == 1) { cout << "+ "; } else { cout << "- "; } cout << a << ' ' << b << '\n'; } } int main() { fastio(); int t = 1; //cin >> t; while(t--) { solve(); } } |