#include <bits/stdc++.h> #include <ranges> #define ft first #define sc second #define pb push_back #define FOR(i,n) for(int i=0; i<n; i++) #define FORX(i,a,b) for(int i=(a); i<=(b); i++) #define watch(x) cerr << (#x) << " == " << (x) << endl typedef long long ll; typedef long double ld; using namespace std; const int N = 3e4, M = 5e4; vector<int> gs[N+5], gd[N+5]; bool vis[N+5]; vector<pair<int,int>> add_base, add, del, del_base; void dfs(int a) { vis[a] = 1; for (const int &b : gs[a]) { if (!vis[b]) { if (a != 1) { add_base.pb({1,b}); } dfs(b); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,ms,md; cin>>n; cin>>ms; FOR(i,ms){ int a,b; cin>>a>>b; gs[a].pb(b); gs[b].pb(a); } cin>>md; FOR(i,md){ int a,b; cin>>a>>b; gd[a].pb(b); gd[b].pb(a); } FORX(i,1,n){ sort(gs[i].begin(),gs[i].end()); sort(gd[i].begin(),gd[i].end()); } dfs(1); FORX(i,2,n){ for (const int &j : gd[i]) { if (j<i || binary_search(gs[i].begin(), gs[i].end(), j)) {continue;} add.pb({i,j}); } } FORX(i,2,n){ for (const int &j : gs[i]) { if (j<i || binary_search(gd[i].begin(), gd[i].end(), j)) {continue;} del.pb({i,j}); } } for (const auto &p : views::reverse(add_base)) { if (!binary_search(gd[1].begin(), gd[1].end(), p.sc)) { del_base.pb(p); } } cout << add_base.size() + add.size() + del.size() + del_base.size() << "\n"; for (const auto &p : add_base) { cout << "+ " << p.ft << " " << p.sc << "\n"; } for (const auto &p : add) { cout << "+ " << p.ft << " " << p.sc << "\n"; } for (const auto &p : del) { cout << "- " << p.ft << " " << p.sc << "\n"; } for (const auto &p : del_base) { cout << "- " << p.ft << " " << p.sc << "\n"; } 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 | #include <bits/stdc++.h> #include <ranges> #define ft first #define sc second #define pb push_back #define FOR(i,n) for(int i=0; i<n; i++) #define FORX(i,a,b) for(int i=(a); i<=(b); i++) #define watch(x) cerr << (#x) << " == " << (x) << endl typedef long long ll; typedef long double ld; using namespace std; const int N = 3e4, M = 5e4; vector<int> gs[N+5], gd[N+5]; bool vis[N+5]; vector<pair<int,int>> add_base, add, del, del_base; void dfs(int a) { vis[a] = 1; for (const int &b : gs[a]) { if (!vis[b]) { if (a != 1) { add_base.pb({1,b}); } dfs(b); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,ms,md; cin>>n; cin>>ms; FOR(i,ms){ int a,b; cin>>a>>b; gs[a].pb(b); gs[b].pb(a); } cin>>md; FOR(i,md){ int a,b; cin>>a>>b; gd[a].pb(b); gd[b].pb(a); } FORX(i,1,n){ sort(gs[i].begin(),gs[i].end()); sort(gd[i].begin(),gd[i].end()); } dfs(1); FORX(i,2,n){ for (const int &j : gd[i]) { if (j<i || binary_search(gs[i].begin(), gs[i].end(), j)) {continue;} add.pb({i,j}); } } FORX(i,2,n){ for (const int &j : gs[i]) { if (j<i || binary_search(gd[i].begin(), gd[i].end(), j)) {continue;} del.pb({i,j}); } } for (const auto &p : views::reverse(add_base)) { if (!binary_search(gd[1].begin(), gd[1].end(), p.sc)) { del_base.pb(p); } } cout << add_base.size() + add.size() + del.size() + del_base.size() << "\n"; for (const auto &p : add_base) { cout << "+ " << p.ft << " " << p.sc << "\n"; } for (const auto &p : add) { cout << "+ " << p.ft << " " << p.sc << "\n"; } for (const auto &p : del) { cout << "- " << p.ft << " " << p.sc << "\n"; } for (const auto &p : del_base) { cout << "- " << p.ft << " " << p.sc << "\n"; } return 0; } |