#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n, m; vector<int> s[30001], d[30001]; cin>>n; cin>>m; for (int i=0; i<m; ++i) { int a, b; cin>>a>>b; s[a].push_back(b); s[b].push_back(a); } cin>>m; for (int i=0; i<m; ++i) { int a, b; cin>>a>>b; d[a].push_back(b); d[b].push_back(a); } vector<pair<int, int>> r; vector<int> w; bool ps[30001]={}; for (int a : s[1]) { w.push_back(a); ps[a]=true; } for (int i=0; i<w.size(); ++i) { for (int a : s[w[i]]) if (a!=1 && !ps[a]) { w.push_back(a); ps[a]=true; r.push_back(make_pair(1, a)); } } for (int a=2; a<=n; ++a) for (int b : s[a]) if (a < b) r.push_back(make_pair(-a, b)); for (int a=2; a<=n; ++a) for (int b : d[a]) if (a < b) r.push_back(make_pair(a, b)); w.clear(); bool pd[30001]={}, dd[30001]={}; for (int a : d[1]) { w.push_back(a); pd[a]=dd[a]=true; } for (int i=0; i<w.size(); ++i) { for (int a : d[w[i]]) if (a!=1 && !pd[a]) { w.push_back(a); pd[a]=true; } } for (int i=w.size()-1; 0<=i; --i) if (!dd[w[i]]) r.push_back(make_pair(-1, w[i])); cout<<r.size()<<'\n'; for (auto e : r) cout<<(e.first < 0 ? '-' : '+')<<' '<<abs(e.first)<<' '<<e.second<<'\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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n, m; vector<int> s[30001], d[30001]; cin>>n; cin>>m; for (int i=0; i<m; ++i) { int a, b; cin>>a>>b; s[a].push_back(b); s[b].push_back(a); } cin>>m; for (int i=0; i<m; ++i) { int a, b; cin>>a>>b; d[a].push_back(b); d[b].push_back(a); } vector<pair<int, int>> r; vector<int> w; bool ps[30001]={}; for (int a : s[1]) { w.push_back(a); ps[a]=true; } for (int i=0; i<w.size(); ++i) { for (int a : s[w[i]]) if (a!=1 && !ps[a]) { w.push_back(a); ps[a]=true; r.push_back(make_pair(1, a)); } } for (int a=2; a<=n; ++a) for (int b : s[a]) if (a < b) r.push_back(make_pair(-a, b)); for (int a=2; a<=n; ++a) for (int b : d[a]) if (a < b) r.push_back(make_pair(a, b)); w.clear(); bool pd[30001]={}, dd[30001]={}; for (int a : d[1]) { w.push_back(a); pd[a]=dd[a]=true; } for (int i=0; i<w.size(); ++i) { for (int a : d[w[i]]) if (a!=1 && !pd[a]) { w.push_back(a); pd[a]=true; } } for (int i=w.size()-1; 0<=i; --i) if (!dd[w[i]]) r.push_back(make_pair(-1, w[i])); cout<<r.size()<<'\n'; for (auto e : r) cout<<(e.first < 0 ? '-' : '+')<<' '<<abs(e.first)<<' '<<e.second<<'\n'; return 0; } |