#include<bits/stdc++.h> #define pb push_back #define pob pop_back #define eb emplace_back #define fi first #define se second #define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define mid ((l+r)/2) #define midLL ((l+r)/2LL) #define mide ((lo+hi)/2) #define mideLL ((lo+hi)/2LL) #define endl '\n' #define shandom_ruffle random_shuffle #define lowbit(x) x&(-x) #define bits(x) __builtin_popcount(x) #define mins(se) (*se.begin()) #define maxs(se) (*--se.end()) #define all(x) x.begin(), x.end() #define log2_floor(i) (i ? __builtin_clzll(1) - __builtin_clzll(i) : -1) #define siz(cont) ((int)cont.size()) #define each(it, cont) for(auto &it : cont) using ll = long long; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; using pli = std::pair<ll, int>; using pil = std::pair<int, ll>; using vll = std::vector<pll>; using vii = std::vector<pii>; using vi = std::vector<int>; using vl = std::vector<ll>; using vvl = std::vector<vl>; using vvi = std::vector<vi>; using vvii = std::vector<vii>; using vli = std::vector<pli>; using vvli = std::vector<vli>; using vil = std::vector<pil>; using vvil = std::vector<vil>; using vc = std::vector<char>; using vvc = std::vector<std::vector<char>>; using vb = std::vector<bool>; using vvb = std::vector<vb>; void err() { std::cout << "\n"; fflush(stdout); } template<class T, class... Ts> void err(T arg, Ts &... args) { std::cout << arg << ' '; err(args...); } using namespace std; constexpr int N = 30010; vvi v(N), v2(N); vii res; set<pair<int, int>> edgs; set<pair<int, int>> final; vi autostradki; inline void add(int a, int b, bool st = true) { edgs.insert({min(a, b), max(a, b)}); if (st) res.pb({min(a, b), max(a, b)}); } inline void rem(int a, int b, bool st = true) { edgs.erase({min(a, b), max(a, b)}); if (st) res.pb({-min(a, b), max(a, b)}); } inline bool is(int a, int b) { return edgs.contains({min(a, b), max(a, b)}); } void build_autostradka() { queue<int> q; vb vis(N); vis[1] = true; for (auto it : v[1]) { q.push(it); vis[it] = true; } while (!q.empty()) { auto f = q.front(); q.pop(); if (!is(f, 1)) { add(f, 1); } for (auto it : v[f]) if (!vis[it]) { q.push(it); vis[it] = true; } } } void zwin_autostradke() { queue<int> q; vi pom; vb vis(N); vis[1] = true; for (auto it : v2[1]) { q.push(it); vis[it] = true; } while (!q.empty()) { auto f = q.front(); q.pop(); pom.pb(f); for (auto it : v2[f]) if (!vis[it]) { q.push(it); vis[it] = true; } } for (int i = siz(pom)-1; i>=0; i--) if (!final.contains({1, pom[i]}) and is(1, pom[i])) rem(1, pom[i]); } signed main() { turbo; int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; v[a].pb(b); v[b].pb(a); add(a, b, false); } build_autostradka(); int m2; cin >> m2; for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; final.insert({min(a, b), max(a, b)}); v2[a].pb(b); v2[b].pb(a); if (!is(a, b)) add(a, b); } vii rm; for (auto it : edgs) if (it.fi != 1 and !final.contains(it)) rm.pb(it); for (auto it : rm) rem(it.fi, it.se); zwin_autostradke(); cout << siz(res) << endl; for (auto [a, b] : res) cout << (a>0 ? "+ " : "- ") << abs(a) << " " << b << endl; }
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 | #include<bits/stdc++.h> #define pb push_back #define pob pop_back #define eb emplace_back #define fi first #define se second #define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define mid ((l+r)/2) #define midLL ((l+r)/2LL) #define mide ((lo+hi)/2) #define mideLL ((lo+hi)/2LL) #define endl '\n' #define shandom_ruffle random_shuffle #define lowbit(x) x&(-x) #define bits(x) __builtin_popcount(x) #define mins(se) (*se.begin()) #define maxs(se) (*--se.end()) #define all(x) x.begin(), x.end() #define log2_floor(i) (i ? __builtin_clzll(1) - __builtin_clzll(i) : -1) #define siz(cont) ((int)cont.size()) #define each(it, cont) for(auto &it : cont) using ll = long long; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; using pli = std::pair<ll, int>; using pil = std::pair<int, ll>; using vll = std::vector<pll>; using vii = std::vector<pii>; using vi = std::vector<int>; using vl = std::vector<ll>; using vvl = std::vector<vl>; using vvi = std::vector<vi>; using vvii = std::vector<vii>; using vli = std::vector<pli>; using vvli = std::vector<vli>; using vil = std::vector<pil>; using vvil = std::vector<vil>; using vc = std::vector<char>; using vvc = std::vector<std::vector<char>>; using vb = std::vector<bool>; using vvb = std::vector<vb>; void err() { std::cout << "\n"; fflush(stdout); } template<class T, class... Ts> void err(T arg, Ts &... args) { std::cout << arg << ' '; err(args...); } using namespace std; constexpr int N = 30010; vvi v(N), v2(N); vii res; set<pair<int, int>> edgs; set<pair<int, int>> final; vi autostradki; inline void add(int a, int b, bool st = true) { edgs.insert({min(a, b), max(a, b)}); if (st) res.pb({min(a, b), max(a, b)}); } inline void rem(int a, int b, bool st = true) { edgs.erase({min(a, b), max(a, b)}); if (st) res.pb({-min(a, b), max(a, b)}); } inline bool is(int a, int b) { return edgs.contains({min(a, b), max(a, b)}); } void build_autostradka() { queue<int> q; vb vis(N); vis[1] = true; for (auto it : v[1]) { q.push(it); vis[it] = true; } while (!q.empty()) { auto f = q.front(); q.pop(); if (!is(f, 1)) { add(f, 1); } for (auto it : v[f]) if (!vis[it]) { q.push(it); vis[it] = true; } } } void zwin_autostradke() { queue<int> q; vi pom; vb vis(N); vis[1] = true; for (auto it : v2[1]) { q.push(it); vis[it] = true; } while (!q.empty()) { auto f = q.front(); q.pop(); pom.pb(f); for (auto it : v2[f]) if (!vis[it]) { q.push(it); vis[it] = true; } } for (int i = siz(pom)-1; i>=0; i--) if (!final.contains({1, pom[i]}) and is(1, pom[i])) rem(1, pom[i]); } signed main() { turbo; int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; v[a].pb(b); v[b].pb(a); add(a, b, false); } build_autostradka(); int m2; cin >> m2; for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; final.insert({min(a, b), max(a, b)}); v2[a].pb(b); v2[b].pb(a); if (!is(a, b)) add(a, b); } vii rm; for (auto it : edgs) if (it.fi != 1 and !final.contains(it)) rm.pb(it); for (auto it : rm) rem(it.fi, it.se); zwin_autostradke(); cout << siz(res) << endl; for (auto [a, b] : res) cout << (a>0 ? "+ " : "- ") << abs(a) << " " << b << endl; } |