#include <iostream> #include <vector> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <algorithm> #include <tuple> using namespace std; #define pb push_back #define mp make_pair #define st first #define nd second #define x first #define y second typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; int n; unordered_set<int>gs[30001]; unordered_set<int>gd[30001]; bool most_zbudowany[30001]; bool most_zniszczony[30001]; vector<tuple<int, int, bool>> o; void buduj_most(int q=1){ if(!most_zbudowany[q]){ most_zbudowany[q] = true; if(q > 1 && gs[1].find(q) == gs[1].end()){ o.pb(make_tuple(1, q, true)); } for(auto gi: gs[q]){ buduj_most(gi); } } } void dodaj(){ for(int i=1; i<=n; i++){ for(auto gi: gd[i]){ if(i<gi && (i!=1 && gi!=1)){ if(gs[i].find(gi) == gs[i].end()){ o.pb(make_tuple(i, gi, true)); } } } } } void usun(){ for(int i=1; i<=n; i++){ for(auto gi: gs[i]){ if(i<gi && (i!=1 && gi!=1)){ if(gd[i].find(gi) == gd[i].end()){ o.pb(make_tuple(i, gi, false)); } } } } } void niszcz_most(int q=1){ if(!most_zniszczony[q]){ most_zniszczony[q] = true; for(auto gi: gd[q]){ niszcz_most(gi); } if(q>1 && gd[1].find(q) == gd[1].end()){ o.pb(make_tuple(1, q, false)); } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int ms, md; cin >> n; for(int i=1; i<=n; i++){ unordered_set<int> gi; gs[i]=gi; gd[i]=gi; } cin >> ms; for(int i=0; i<ms; i++){ int a, b; cin >> a >> b; gs[a].insert(b); gs[b].insert(a); } cin >> md; for(int i=0; i<md; i++){ int a, b; cin >> a >> b; gd[a].insert(b); gd[b].insert(a); } buduj_most(); dodaj(); usun(); niszcz_most(); cout << o.size() << "\n"; for(auto oi: o){ if(get<2>(oi)){ cout << "+ " << get<0>(oi) << " " << get<1>(oi) << "\n"; }else{ cout << "- " << get<0>(oi) << " " << get<1>(oi) << "\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 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 130 | #include <iostream> #include <vector> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <algorithm> #include <tuple> using namespace std; #define pb push_back #define mp make_pair #define st first #define nd second #define x first #define y second typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; int n; unordered_set<int>gs[30001]; unordered_set<int>gd[30001]; bool most_zbudowany[30001]; bool most_zniszczony[30001]; vector<tuple<int, int, bool>> o; void buduj_most(int q=1){ if(!most_zbudowany[q]){ most_zbudowany[q] = true; if(q > 1 && gs[1].find(q) == gs[1].end()){ o.pb(make_tuple(1, q, true)); } for(auto gi: gs[q]){ buduj_most(gi); } } } void dodaj(){ for(int i=1; i<=n; i++){ for(auto gi: gd[i]){ if(i<gi && (i!=1 && gi!=1)){ if(gs[i].find(gi) == gs[i].end()){ o.pb(make_tuple(i, gi, true)); } } } } } void usun(){ for(int i=1; i<=n; i++){ for(auto gi: gs[i]){ if(i<gi && (i!=1 && gi!=1)){ if(gd[i].find(gi) == gd[i].end()){ o.pb(make_tuple(i, gi, false)); } } } } } void niszcz_most(int q=1){ if(!most_zniszczony[q]){ most_zniszczony[q] = true; for(auto gi: gd[q]){ niszcz_most(gi); } if(q>1 && gd[1].find(q) == gd[1].end()){ o.pb(make_tuple(1, q, false)); } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int ms, md; cin >> n; for(int i=1; i<=n; i++){ unordered_set<int> gi; gs[i]=gi; gd[i]=gi; } cin >> ms; for(int i=0; i<ms; i++){ int a, b; cin >> a >> b; gs[a].insert(b); gs[b].insert(a); } cin >> md; for(int i=0; i<md; i++){ int a, b; cin >> a >> b; gd[a].insert(b); gd[b].insert(a); } buduj_most(); dodaj(); usun(); niszcz_most(); cout << o.size() << "\n"; for(auto oi: o){ if(get<2>(oi)){ cout << "+ " << get<0>(oi) << " " << get<1>(oi) << "\n"; }else{ cout << "- " << get<0>(oi) << " " << get<1>(oi) << "\n"; } } return 0; } |