//Sylwia Sapkowska #include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") using namespace std; void __print(int x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << "'" << x << "'";} void __print(const char *x) {cerr << '"' << x << '"';} void __print(const string &x) {cerr << '"' << x << '"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifdef LOCAL #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define int long long typedef pair<int, int> T; const int oo = 1e18, oo2 = 1e9+7, K = 30; const int mod = 998244353; void solve(){ int n; cin >> n; int m1; cin >> m1; vector<vector<int>>g(n+1); vector<T>edges; set<T>s; for (int i = 0; i<m1; i++){ int a, b; cin >> a >> b; if (a > b) swap(a, b); g[a].emplace_back(b); g[b].emplace_back(a); s.insert({a, b}); edges.emplace_back(a, b); } string ret; vector<T>ans; auto add = [&](int a, int b){ if (a == b) return; if (a > b) swap(a, b); if (s.find({a, b}) != s.end()) return; s.insert({a, b}); ret += '+'; // debug('+', a, b); ans.emplace_back(a, b); }; auto remove = [&](int a, int b){ if (a == b) return; if (a > b) swap(a, b); if (s.find({a, b}) == s.end()) return; s.erase({a, b}); ret += '-'; // debug('-', a, b); ans.emplace_back(a, b); }; vector<bool>vis(n+1); bool f = 0; vector<int>post(n+1); int tt = 0; function<void(int)>dfs = [&](int v){ if (!f) add(1, v); vis[v] = 1; for (auto x: g[v]){ if (!vis[x]){ dfs(x); } } if (f) post[v] = ++tt; }; dfs(1); int m2; cin >> m2; vector<int>left; set<T>s2; for (int i = 1; i<=n; i++) g[i].clear(); f = 1; vis.assign(n+1, 0); for (int i = 0; i<m2; i++){ int a, b; cin >> a >> b; if (a > b) swap(a, b); if (s.find({a, b}) == s.end()){ add(a, b); } debug(a, b); g[a].emplace_back(b); g[b].emplace_back(a); s2.insert({a, b}); } dfs(1); vector<T>fix; for (auto [a, b]: s){ if (s2.find({a, b}) == s2.end()){ if (a == 1){ left.emplace_back(b); } else { fix.emplace_back(a, b); } } } for (auto [a, b]: fix) remove(a, b); debug(left); sort(left.begin(), left.end(), [&](auto x, auto y){ return post[x] < post[y]; }); debug(left); for (auto v: left) remove(1, v); debug(s); debug(s2); cout << (int)ans.size() << "\n"; for (int i = 0; i<(int)ans.size(); i++){ cout << ret[i] << " " << ans[i].first << " " << ans[i].second << "\n"; } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; //cin >> t; while (t--) 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 | //Sylwia Sapkowska #include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") using namespace std; void __print(int x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << "'" << x << "'";} void __print(const char *x) {cerr << '"' << x << '"';} void __print(const string &x) {cerr << '"' << x << '"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifdef LOCAL #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define int long long typedef pair<int, int> T; const int oo = 1e18, oo2 = 1e9+7, K = 30; const int mod = 998244353; void solve(){ int n; cin >> n; int m1; cin >> m1; vector<vector<int>>g(n+1); vector<T>edges; set<T>s; for (int i = 0; i<m1; i++){ int a, b; cin >> a >> b; if (a > b) swap(a, b); g[a].emplace_back(b); g[b].emplace_back(a); s.insert({a, b}); edges.emplace_back(a, b); } string ret; vector<T>ans; auto add = [&](int a, int b){ if (a == b) return; if (a > b) swap(a, b); if (s.find({a, b}) != s.end()) return; s.insert({a, b}); ret += '+'; // debug('+', a, b); ans.emplace_back(a, b); }; auto remove = [&](int a, int b){ if (a == b) return; if (a > b) swap(a, b); if (s.find({a, b}) == s.end()) return; s.erase({a, b}); ret += '-'; // debug('-', a, b); ans.emplace_back(a, b); }; vector<bool>vis(n+1); bool f = 0; vector<int>post(n+1); int tt = 0; function<void(int)>dfs = [&](int v){ if (!f) add(1, v); vis[v] = 1; for (auto x: g[v]){ if (!vis[x]){ dfs(x); } } if (f) post[v] = ++tt; }; dfs(1); int m2; cin >> m2; vector<int>left; set<T>s2; for (int i = 1; i<=n; i++) g[i].clear(); f = 1; vis.assign(n+1, 0); for (int i = 0; i<m2; i++){ int a, b; cin >> a >> b; if (a > b) swap(a, b); if (s.find({a, b}) == s.end()){ add(a, b); } debug(a, b); g[a].emplace_back(b); g[b].emplace_back(a); s2.insert({a, b}); } dfs(1); vector<T>fix; for (auto [a, b]: s){ if (s2.find({a, b}) == s2.end()){ if (a == 1){ left.emplace_back(b); } else { fix.emplace_back(a, b); } } } for (auto [a, b]: fix) remove(a, b); debug(left); sort(left.begin(), left.end(), [&](auto x, auto y){ return post[x] < post[y]; }); debug(left); for (auto v: left) remove(1, v); debug(s); debug(s2); cout << (int)ans.size() << "\n"; for (int i = 0; i<(int)ans.size(); i++){ cout << ret[i] << " " << ans[i].first << " " << ans[i].second << "\n"; } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; //cin >> t; while (t--) solve(); return 0; } |