/* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define st first #define nd second #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #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 struct Op { int a; int b; char c; }; bool operator<(const Op &lhs, const Op &rhs) { return tie(lhs.a, lhs.b, lhs.c) < tie(rhs.a, rhs.b, rhs.c); } vector<Op> res; struct Graph { int n, m; vector<vector<int> > kraw; set<pair<int, int> > list; vector<bool> odw; Graph(int _n, int _m, vector<vector<int> > &_kraw) { n = _n; m = _m; swap(kraw, _kraw); REP(i, n) { for (int v : kraw[i]) { if (i < v) { list.insert({i,v}); } else { list.insert({v, i}); } } } odw.resize(n); } bool exists(int a, int b) { if (a == b) { return true; } if (a > b) { swap(a, b); } return list.find({a, b}) != list.end(); } void dfs(int x, bool insert) { odw[x] = true; if (insert && !exists(0, x)) { res.push_back(Op{0, x, '+'}); } for (int v : kraw[x]) { if (!odw[v]) { dfs(v, insert); } } if (!insert && !exists(0, x)) { res.push_back(Op{0, x, '-'}); } } void handle_rest(bool insert) { char sgn = insert ? '+' : '-'; for (auto p : list) { if (p.st == 0) { continue; } res.push_back(Op(p.st, p.nd, sgn)); } } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, m; vector<vector<int> > kraw; cin >> n; cin >> m; kraw.resize(n); REP(i, m) { int a, b; cin >> a >> b; a--; b--; kraw[a].push_back(b); kraw[b].push_back(a); } Graph start(n, m, kraw); cin >> m; kraw.resize(n); REP(i, m) { int a, b; cin >> a >> b; a--; b--; kraw[a].push_back(b); kraw[b].push_back(a); } Graph finish(n, m, kraw); start.dfs(0, true); start.handle_rest(false); finish.handle_rest(true); finish.dfs(0, false); cout << ssize(res) << "\n"; for (auto op : res) { cout << op.c << " " << op.a + 1 << " " << op.b + 1 << "\n"; } }
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 | /* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define st first #define nd second #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #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 struct Op { int a; int b; char c; }; bool operator<(const Op &lhs, const Op &rhs) { return tie(lhs.a, lhs.b, lhs.c) < tie(rhs.a, rhs.b, rhs.c); } vector<Op> res; struct Graph { int n, m; vector<vector<int> > kraw; set<pair<int, int> > list; vector<bool> odw; Graph(int _n, int _m, vector<vector<int> > &_kraw) { n = _n; m = _m; swap(kraw, _kraw); REP(i, n) { for (int v : kraw[i]) { if (i < v) { list.insert({i,v}); } else { list.insert({v, i}); } } } odw.resize(n); } bool exists(int a, int b) { if (a == b) { return true; } if (a > b) { swap(a, b); } return list.find({a, b}) != list.end(); } void dfs(int x, bool insert) { odw[x] = true; if (insert && !exists(0, x)) { res.push_back(Op{0, x, '+'}); } for (int v : kraw[x]) { if (!odw[v]) { dfs(v, insert); } } if (!insert && !exists(0, x)) { res.push_back(Op{0, x, '-'}); } } void handle_rest(bool insert) { char sgn = insert ? '+' : '-'; for (auto p : list) { if (p.st == 0) { continue; } res.push_back(Op(p.st, p.nd, sgn)); } } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, m; vector<vector<int> > kraw; cin >> n; cin >> m; kraw.resize(n); REP(i, m) { int a, b; cin >> a >> b; a--; b--; kraw[a].push_back(b); kraw[b].push_back(a); } Graph start(n, m, kraw); cin >> m; kraw.resize(n); REP(i, m) { int a, b; cin >> a >> b; a--; b--; kraw[a].push_back(b); kraw[b].push_back(a); } Graph finish(n, m, kraw); start.dfs(0, true); start.handle_rest(false); finish.handle_rest(true); finish.dfs(0, false); cout << ssize(res) << "\n"; for (auto op : res) { cout << op.c << " " << op.a + 1 << " " << op.b + 1 << "\n"; } } |