#include <bits/stdc++.h> using namespace std; #ifdef DEBUG auto &operator<<(auto &o, pair<auto, auto> p) { return o << "()" << p.first << ", " << p.second <<")"; } auto operator<<(auto &o, auto x)-> decltype(x.end(), o) { o << "{";int i = 0; for(auto e : x) o << ", "+!i++<<e; return o <<"}"; } #define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x) #else #define debug(...) {} #endif #define all(x) x.begin(), x.end() #define pb push_back #define fi first #define se second typedef pair <int, int> pii; constexpr int N = 3e4+7; struct op { char x; int a, b; op() {} op(char k, int aa, int bb) { x = k; a = aa; b = bb; } void display() { cout<<x<<' '<<a<<' '<<b<<'\n'; } }; bool oV[N], nV[N]; set <int> oG[N]; set <int> nG[N]; set <pii> oe; set <pii> ne; vector <op> R; void oDfs(int v) { oV[v] = true; for (int u : oG[v]) { if (!oV[u]) { if (oG[1].find(u) == oG[1].end()) { R.emplace_back('+', 1, u); } oDfs(u); } } } void nDfs(int v) { nV[v] = true; for (int u : nG[v]) { if (!nV[u]) { nDfs(u); if (nG[1].find(u) == nG[1].end()) { R.emplace_back('-', 1, u); } } } } void test() { int n, m; cin>>n>>m; for (int i=0; i<m; ++i) { int a, b; cin>>a>>b; if (a > b) swap(a, b); oG[a].insert(b); oG[b].insert(a); oe.insert({a, b}); } cin>>m; for (int i=0; i<m; ++i) { int a, b; cin>>a>>b; if (a > b) swap(a, b); nG[a].insert(b); nG[b].insert(a); ne.insert({a, b}); } oDfs(1); for (auto &[x, y] : ne) { if (x != 1 && oe.find({x, y}) == oe.end()) { R.emplace_back('+', x, y); } } for (auto &[x, y] : oe) { if (x != 1 && ne.find({x, y}) == ne.end()) { R.emplace_back('-', x, y); } } nDfs(1); cout<<R.size()<<'\n'; for (op x : R) { x.display(); } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) { test(); } }
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 | #include <bits/stdc++.h> using namespace std; #ifdef DEBUG auto &operator<<(auto &o, pair<auto, auto> p) { return o << "()" << p.first << ", " << p.second <<")"; } auto operator<<(auto &o, auto x)-> decltype(x.end(), o) { o << "{";int i = 0; for(auto e : x) o << ", "+!i++<<e; return o <<"}"; } #define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x) #else #define debug(...) {} #endif #define all(x) x.begin(), x.end() #define pb push_back #define fi first #define se second typedef pair <int, int> pii; constexpr int N = 3e4+7; struct op { char x; int a, b; op() {} op(char k, int aa, int bb) { x = k; a = aa; b = bb; } void display() { cout<<x<<' '<<a<<' '<<b<<'\n'; } }; bool oV[N], nV[N]; set <int> oG[N]; set <int> nG[N]; set <pii> oe; set <pii> ne; vector <op> R; void oDfs(int v) { oV[v] = true; for (int u : oG[v]) { if (!oV[u]) { if (oG[1].find(u) == oG[1].end()) { R.emplace_back('+', 1, u); } oDfs(u); } } } void nDfs(int v) { nV[v] = true; for (int u : nG[v]) { if (!nV[u]) { nDfs(u); if (nG[1].find(u) == nG[1].end()) { R.emplace_back('-', 1, u); } } } } void test() { int n, m; cin>>n>>m; for (int i=0; i<m; ++i) { int a, b; cin>>a>>b; if (a > b) swap(a, b); oG[a].insert(b); oG[b].insert(a); oe.insert({a, b}); } cin>>m; for (int i=0; i<m; ++i) { int a, b; cin>>a>>b; if (a > b) swap(a, b); nG[a].insert(b); nG[b].insert(a); ne.insert({a, b}); } oDfs(1); for (auto &[x, y] : ne) { if (x != 1 && oe.find({x, y}) == oe.end()) { R.emplace_back('+', x, y); } } for (auto &[x, y] : oe) { if (x != 1 && ne.find({x, y}) == ne.end()) { R.emplace_back('-', x, y); } } nDfs(1); cout<<R.size()<<'\n'; for (op x : R) { x.display(); } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) { test(); } } |