#include <bits/stdc++.h> using namespace std; int n,ms,i,j,a,b,md; array<vector<int>,30001> v,w; vector<int> f,g,ff,gg; vector<array<int,2>> h,q; array<int,2> ww; array<bool,30001> x,y; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; cin >> ms; for (i=0;i<ms;i++){ cin >> a >> b; v[a].push_back(b); v[b].push_back(a); if (a>1 and b>1) h.push_back({a,b}); } cin >> md; for (i=0;i<md;i++){ cin >> a >> b; w[a].push_back(b); w[b].push_back(a); if (a>1 and b>1) q.push_back({a,b}); } f={1}; x[1]=1; ff={}; while (j<f.size()){ a=f[j]; for (auto b:v[a]){ if (!x[b]){ x[b]=1; f.push_back(b); if (a>1) ff.push_back(b); } } j++; } g={1}; y[1]=1; j=0; gg={}; while (j<g.size()){ a=g[j]; for (auto b:w[a]){ if (!y[b]){ y[b]=1; g.push_back(b); //cout << b << "#"; if (a>1){ //cout << b << "$"; gg.push_back(b); } } } j++; } cout << ff.size()+gg.size()+h.size()+q.size() << '\n'; for (auto a:ff) cout << "+ " << 1 << ' ' << a << '\n'; for (auto ww:h) cout << "- " << ww[0] << ' ' << ww[1] << '\n'; if (q.size()>0){ for (i=q.size()-1;i>=0;i--) cout << "+ " << q[i][0] << ' ' << q[i][1] << '\n'; } if (gg.size()>0){ for (i=gg.size()-1;i>=0;i--) cout << "- " << 1 << ' ' << gg[i] << '\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 | #include <bits/stdc++.h> using namespace std; int n,ms,i,j,a,b,md; array<vector<int>,30001> v,w; vector<int> f,g,ff,gg; vector<array<int,2>> h,q; array<int,2> ww; array<bool,30001> x,y; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; cin >> ms; for (i=0;i<ms;i++){ cin >> a >> b; v[a].push_back(b); v[b].push_back(a); if (a>1 and b>1) h.push_back({a,b}); } cin >> md; for (i=0;i<md;i++){ cin >> a >> b; w[a].push_back(b); w[b].push_back(a); if (a>1 and b>1) q.push_back({a,b}); } f={1}; x[1]=1; ff={}; while (j<f.size()){ a=f[j]; for (auto b:v[a]){ if (!x[b]){ x[b]=1; f.push_back(b); if (a>1) ff.push_back(b); } } j++; } g={1}; y[1]=1; j=0; gg={}; while (j<g.size()){ a=g[j]; for (auto b:w[a]){ if (!y[b]){ y[b]=1; g.push_back(b); //cout << b << "#"; if (a>1){ //cout << b << "$"; gg.push_back(b); } } } j++; } cout << ff.size()+gg.size()+h.size()+q.size() << '\n'; for (auto a:ff) cout << "+ " << 1 << ' ' << a << '\n'; for (auto ww:h) cout << "- " << ww[0] << ' ' << ww[1] << '\n'; if (q.size()>0){ for (i=q.size()-1;i>=0;i--) cout << "+ " << q[i][0] << ' ' << q[i][1] << '\n'; } if (gg.size()>0){ for (i=gg.size()-1;i>=0;i--) cout << "- " << 1 << ' ' << gg[i] << '\n'; } } |