#include <bits/stdc++.h> using namespace std; //#define int long long const int MAXN = 1e6 + 7; const int INF = 1e9 + 7; set<pair<int, int>> st1, st2; vector<pair<int, int>> dodaj; vector<pair<int, pair<int, int>>> usun; set<pair<int, int>> unavailable; set<int> G1[MAXN], G2[MAXN]; void solve(){ int n, siz1, siz2, a, b, i; cin >> n >> siz1; for(i = 1; i <= siz1; i++){ cin >> a >> b; G1[a].insert(b); G1[b].insert(a); } cin >> siz2; for(i = 1; i <= siz2; i++){ cin >> a >> b; G2[a].insert(b); G2[b].insert(a); } for(a = 1; a <= n; a++){ for(b = a + 1; b <= n; b++){ if(!G1[a].count(b)) unavailable.insert({a, b}); } } for(i = 1; i <= n + 1; i++){ vector<pair<int, int>> deletion; for(auto [a, b] : unavailable){ for(int u : G1[b]){ if(G1[a].count(u)){ deletion.push_back({a, b}); G1[a].insert(b); G1[b].insert(a); dodaj.push_back({a, b}); break; } } } for(auto [a, b] : deletion) unavailable.erase({a, b}); } for(a = 1; a <= n; a++){ vector<int> dist(n + 1, INF); queue<int> q; q.push(a); dist[a] = 0; while(!q.empty()){ int b = q.front(); q.pop(); for(auto u : G2[b]){ if(dist[u] >= INF){ dist[u] = dist[b] + 1; q.push(u); } } } for(b = a + 1; b <= n; b++){ if(dist[b] > 1) usun.push_back({dist[b], {a, b}}); } } sort(usun.begin(), usun.end()); reverse(usun.begin(), usun.end()); int ile = dodaj.size() + usun.size(); cout << ile << '\n'; for(auto [a, b] : dodaj) cout << "+ " << a << ' ' << b << '\n'; for(auto [d, u] : usun) cout << "- " << u.first << ' ' << u.second << '\n'; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; //cin >> t; while(t--) solve(); 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 | #include <bits/stdc++.h> using namespace std; //#define int long long const int MAXN = 1e6 + 7; const int INF = 1e9 + 7; set<pair<int, int>> st1, st2; vector<pair<int, int>> dodaj; vector<pair<int, pair<int, int>>> usun; set<pair<int, int>> unavailable; set<int> G1[MAXN], G2[MAXN]; void solve(){ int n, siz1, siz2, a, b, i; cin >> n >> siz1; for(i = 1; i <= siz1; i++){ cin >> a >> b; G1[a].insert(b); G1[b].insert(a); } cin >> siz2; for(i = 1; i <= siz2; i++){ cin >> a >> b; G2[a].insert(b); G2[b].insert(a); } for(a = 1; a <= n; a++){ for(b = a + 1; b <= n; b++){ if(!G1[a].count(b)) unavailable.insert({a, b}); } } for(i = 1; i <= n + 1; i++){ vector<pair<int, int>> deletion; for(auto [a, b] : unavailable){ for(int u : G1[b]){ if(G1[a].count(u)){ deletion.push_back({a, b}); G1[a].insert(b); G1[b].insert(a); dodaj.push_back({a, b}); break; } } } for(auto [a, b] : deletion) unavailable.erase({a, b}); } for(a = 1; a <= n; a++){ vector<int> dist(n + 1, INF); queue<int> q; q.push(a); dist[a] = 0; while(!q.empty()){ int b = q.front(); q.pop(); for(auto u : G2[b]){ if(dist[u] >= INF){ dist[u] = dist[b] + 1; q.push(u); } } } for(b = a + 1; b <= n; b++){ if(dist[b] > 1) usun.push_back({dist[b], {a, b}}); } } sort(usun.begin(), usun.end()); reverse(usun.begin(), usun.end()); int ile = dodaj.size() + usun.size(); cout << ile << '\n'; for(auto [a, b] : dodaj) cout << "+ " << a << ' ' << b << '\n'; for(auto [d, u] : usun) cout << "- " << u.first << ' ' << u.second << '\n'; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; //cin >> t; while(t--) solve(); return 0; } |