#include<bits/stdc++.h> using namespace std; const int maxn = 3e4+7; set<int> ms[maxn]; map<pair<int, int >, bool >ei, eo; vector<pair<int, int> >plusy; pair<int, int> norm(int a, int b){ if(a <= b) return {a, b}; else return {b, a}; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, mi, mo; cin >> n; cin >> mi; for(int i=0, a, b; i<mi; ++i){ cin >> a >> b; ei[norm(a, b)] = 1; } cin >> mo; for(int i=0, a, b; i<mo; ++i){ cin >> a >> b; eo[norm(a, b)] = 1; } for(int i=2; i<=n; ++i){ if(ei.find(norm(1, i)) == ei.end()){ ei[norm(1, i)] = 1; plusy.push_back({1, i}); } } for(auto it = eo.begin(); it != eo.end(); it++){ if(ei.find(it->first) == ei.end()){ ei[(it->first)] = 1; plusy.push_back({(it->first).first, (it->first).second}); } } int a, b; bool val=0; stack<pair<int, int> > minusy; while(!ei.empty()){ // for(auto it=ei.begin(); it != ei.end(); ++it) cout << "{" << (it->first).first << ", " << (it->first).second << "}"; cout << "\n"; // for(auto it=eo.begin(); it != eo.end(); ++it) cout << "{" << (it->first).first << ", " << (it->first).second << "}"; cout << "\n"; // cout << minusy.size() << "lol\n"; for(auto it= ei.begin(); it != ei.end();){ if(eo.find(it->first) != eo.end()){ ms[(it->first).first].insert((it->first).second); ms[(it->first).second].insert((it->first).first); ei.erase(it++); }else{ a = (it->first).first; b = (it->first).second; if(ms[a].size() > ms[b].size()) swap(a, b); val = 1; for(auto u: ms[a]){ if(ms[b].find(u) != ms[b].end()){ minusy.push({a, b}); ms[(it->first).first].insert((it->first).second); ms[(it->first).second].insert((it->first).first); ei.erase(it++); val=0; break; } } if( val == 1) it++; } } } // cout << minusy.size() << "lol\n"; cout << plusy.size() + minusy.size() << "\n"; for(auto u: plusy){ cout << "+ " << u.first << " " << u.second << "\n"; } while(!minusy.empty()){ // cerr << "lol\n"; cout << "- " << minusy.top().first << " " << minusy.top().second << "\n"; minusy.pop(); } }
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 | #include<bits/stdc++.h> using namespace std; const int maxn = 3e4+7; set<int> ms[maxn]; map<pair<int, int >, bool >ei, eo; vector<pair<int, int> >plusy; pair<int, int> norm(int a, int b){ if(a <= b) return {a, b}; else return {b, a}; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, mi, mo; cin >> n; cin >> mi; for(int i=0, a, b; i<mi; ++i){ cin >> a >> b; ei[norm(a, b)] = 1; } cin >> mo; for(int i=0, a, b; i<mo; ++i){ cin >> a >> b; eo[norm(a, b)] = 1; } for(int i=2; i<=n; ++i){ if(ei.find(norm(1, i)) == ei.end()){ ei[norm(1, i)] = 1; plusy.push_back({1, i}); } } for(auto it = eo.begin(); it != eo.end(); it++){ if(ei.find(it->first) == ei.end()){ ei[(it->first)] = 1; plusy.push_back({(it->first).first, (it->first).second}); } } int a, b; bool val=0; stack<pair<int, int> > minusy; while(!ei.empty()){ // for(auto it=ei.begin(); it != ei.end(); ++it) cout << "{" << (it->first).first << ", " << (it->first).second << "}"; cout << "\n"; // for(auto it=eo.begin(); it != eo.end(); ++it) cout << "{" << (it->first).first << ", " << (it->first).second << "}"; cout << "\n"; // cout << minusy.size() << "lol\n"; for(auto it= ei.begin(); it != ei.end();){ if(eo.find(it->first) != eo.end()){ ms[(it->first).first].insert((it->first).second); ms[(it->first).second].insert((it->first).first); ei.erase(it++); }else{ a = (it->first).first; b = (it->first).second; if(ms[a].size() > ms[b].size()) swap(a, b); val = 1; for(auto u: ms[a]){ if(ms[b].find(u) != ms[b].end()){ minusy.push({a, b}); ms[(it->first).first].insert((it->first).second); ms[(it->first).second].insert((it->first).first); ei.erase(it++); val=0; break; } } if( val == 1) it++; } } } // cout << minusy.size() << "lol\n"; cout << plusy.size() + minusy.size() << "\n"; for(auto u: plusy){ cout << "+ " << u.first << " " << u.second << "\n"; } while(!minusy.empty()){ // cerr << "lol\n"; cout << "- " << minusy.top().first << " " << minusy.top().second << "\n"; minusy.pop(); } } |