#include <bits/stdc++.h> #define fi first #define se second using namespace std; typedef long long ll; const int N = 3e4+7; const int M = 5e4+7; const int inf = 1e9+7; int n, ms, md, res = 0; set<int> g1[N], g2[N]; vector<pair<int,pair<int,int>>> to_remove; vector<pair<int,int>> to_add; bool comp(pair<int,pair<int,int>> a, pair<int,pair<int,int>> b){ return a.fi > b.fi; } void bfs(int start){ vector<bool> vis(n+1, false); vector<int> dist(n+1, inf); queue<int> q; vis[start] = true; q.push(start); dist[start] = 0; while (!q.empty()){ int v = q.front(); q.pop(); for (auto u : g2[v]){ if (vis[u]) continue; vis[u] = true; dist[u] = dist[v] + 1; q.push(u); } } for (int i=n; i>=start+1; i--){ if (dist[i] >= 2){ res++; to_remove.push_back({dist[i], {start, i}}); } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); //dane cin >> n >> ms; for (int i=0; i<ms; i++){ int a, b; cin >> a >> b; g1[a].insert(b); g1[b].insert(a); } cin >> md; for (int i=0; i<md; i++){ int a, b; cin >> a >> b; g2[a].insert(b); g2[b].insert(a); } //brakujace krawedzie do kliki set<pair<int,int>> not_in; for (int i=1; i<=n; i++){ for (int j=i+1; j<=n; j++){ if (g1[i].find(j) == g1[i].end()) not_in.insert({i, j}); } } //krawedzie do usuniecia for (int i=1; i<=n; i++){ bfs(i); } sort(to_remove.begin(), to_remove.end(), comp); //dodanie krawedzi w okreslonej kolejnosci for (int i=1; i<=n; i++){ vector<pair<int,int>> used; for (auto [a, b] : not_in){ for (auto c : g1[b]){ if (g1[a].find(c) != g1[a].end()){ g1[a].insert(b); g1[b].insert(a); to_add.push_back({a, b}); used.push_back({a, b}); res++; break; } } } for (auto ele : used) not_in.erase(ele); } //zebranie wyniku cout << res << '\n'; for (auto [a, b] : to_add) cout << "+ " << min(a, b) << ' ' << max(a, b) << '\n'; for (auto ele : to_remove) cout << "- " << min(ele.se.fi, ele.se.se) << ' ' << max(ele.se.fi, ele.se.se) << '\n'; //sprawdzarka //cout << res << '\n'; //set<pair<int,int>> delet; //for (auto ele : to_remove){ //delet.insert(ele.se); //delet.insert({ele.se.se, ele.se.fi}); //} //vector<pair<int,int>> edges1, edges2; //for (int i=1; i<=n; i++){ //for (auto v : g1[i]){ //if (delet.find({i, v}) == delet.end()){ //edges1.push_back({i, v}); //} //} //for (auto v : g2[i]) edges2.push_back({i, v}); //} //sort(edges1.begin(), edges1.end()); //sort(edges2.begin(), edges2.end()); //if (edges1 != edges2) cout << "CRITICAL ERROR! 1\n"; return 0; } /* 4 3 1 2 3 4 3 2 4 1 4 1 2 2 3 3 4 5 4 1 2 2 3 1 4 1 5 4 1 2 1 3 3 4 3 5 */
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #include <bits/stdc++.h> #define fi first #define se second using namespace std; typedef long long ll; const int N = 3e4+7; const int M = 5e4+7; const int inf = 1e9+7; int n, ms, md, res = 0; set<int> g1[N], g2[N]; vector<pair<int,pair<int,int>>> to_remove; vector<pair<int,int>> to_add; bool comp(pair<int,pair<int,int>> a, pair<int,pair<int,int>> b){ return a.fi > b.fi; } void bfs(int start){ vector<bool> vis(n+1, false); vector<int> dist(n+1, inf); queue<int> q; vis[start] = true; q.push(start); dist[start] = 0; while (!q.empty()){ int v = q.front(); q.pop(); for (auto u : g2[v]){ if (vis[u]) continue; vis[u] = true; dist[u] = dist[v] + 1; q.push(u); } } for (int i=n; i>=start+1; i--){ if (dist[i] >= 2){ res++; to_remove.push_back({dist[i], {start, i}}); } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); //dane cin >> n >> ms; for (int i=0; i<ms; i++){ int a, b; cin >> a >> b; g1[a].insert(b); g1[b].insert(a); } cin >> md; for (int i=0; i<md; i++){ int a, b; cin >> a >> b; g2[a].insert(b); g2[b].insert(a); } //brakujace krawedzie do kliki set<pair<int,int>> not_in; for (int i=1; i<=n; i++){ for (int j=i+1; j<=n; j++){ if (g1[i].find(j) == g1[i].end()) not_in.insert({i, j}); } } //krawedzie do usuniecia for (int i=1; i<=n; i++){ bfs(i); } sort(to_remove.begin(), to_remove.end(), comp); //dodanie krawedzi w okreslonej kolejnosci for (int i=1; i<=n; i++){ vector<pair<int,int>> used; for (auto [a, b] : not_in){ for (auto c : g1[b]){ if (g1[a].find(c) != g1[a].end()){ g1[a].insert(b); g1[b].insert(a); to_add.push_back({a, b}); used.push_back({a, b}); res++; break; } } } for (auto ele : used) not_in.erase(ele); } //zebranie wyniku cout << res << '\n'; for (auto [a, b] : to_add) cout << "+ " << min(a, b) << ' ' << max(a, b) << '\n'; for (auto ele : to_remove) cout << "- " << min(ele.se.fi, ele.se.se) << ' ' << max(ele.se.fi, ele.se.se) << '\n'; //sprawdzarka //cout << res << '\n'; //set<pair<int,int>> delet; //for (auto ele : to_remove){ //delet.insert(ele.se); //delet.insert({ele.se.se, ele.se.fi}); //} //vector<pair<int,int>> edges1, edges2; //for (int i=1; i<=n; i++){ //for (auto v : g1[i]){ //if (delet.find({i, v}) == delet.end()){ //edges1.push_back({i, v}); //} //} //for (auto v : g2[i]) edges2.push_back({i, v}); //} //sort(edges1.begin(), edges1.end()); //sort(edges2.begin(), edges2.end()); //if (edges1 != edges2) cout << "CRITICAL ERROR! 1\n"; return 0; } /* 4 3 1 2 3 4 3 2 4 1 4 1 2 2 3 3 4 5 4 1 2 2 3 1 4 1 5 4 1 2 1 3 3 4 3 5 */ |