#include <bits/stdc++.h> using namespace std; const int MAXN = 30002; const int MAXO = 300000; set<int> edges1[MAXN]; set<int> edges2[MAXN]; bool visited1[MAXN]; bool visited2[MAXN]; int dist1[MAXN]; int dist2[MAXN]; int operations[MAXO][3]; void bsf(int start, set<int> edges[], int dist[MAXN]) { bool visited[MAXN] = {false}; queue<int> q; q.push(start); visited[start] = true; while (!q.empty()) { int a = q.front(); q.pop(); for (int b : edges[a]) { if (!visited[b]) { dist[b] = dist[a] + 1; visited[b] = true; q.push(b); } } } } int main() { int n, m1, m2; cin >> n >> m1; for (int i = 0; i < m1; i++) { int a, b; cin >> a >> b; edges1[a].insert(b); edges1[b].insert(a); } cin >> m2; for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; edges2[a].insert(b); edges2[b].insert(a); } // dodawanie krawędzi int vadd_size = 0; for (int i = 1; i < n; i++) { dist2[i] = 0; bsf(i, edges2, dist2); do { int diff[n] = {0}; vector<int> vadd = {}, addfirst = {}; dist1[i] = 0; bsf(i, edges1, dist1); for (int j = i + 1; j <= n; j++) { diff[j] = dist1[j] - dist2[j]; if (diff[j] > 0) { if (dist1[j] == 2) { addfirst.push_back(j); } else { vadd.push_back(j); } } } for (int j : addfirst) { edges1[i].insert(j); edges1[j].insert(i); operations[0][0]++; operations[operations[0][0]][1] = i; operations[operations[0][0]][2] = j; } vadd_size = vadd.size(); if (addfirst.size() == 0 && vadd.size() > 0) { for (int j = i + 1; j <= n; j++) { if (dist1[j] == 2) { edges1[i].insert(j); edges1[j].insert(i); operations[0][0]++; operations[operations[0][0]][1] = i; operations[operations[0][0]][2] = j; } } } } while (vadd_size > 0); } // usuwanie krawędzi int vrem_size = 0; for (int i = 1; i < n; i++) { dist2[i] = 0; bsf(i, edges2, dist2); // do { int diff[n] = {0}; vector<int> vrem = {}, remfirst = {}; dist1[i] = 0; bsf(i, edges1, dist1); for (int j = i + 1; j <= n; j++) { diff[j] = dist1[j] - dist2[j]; if (diff[j] < 0) { if (dist1[j] == 1) { remfirst.push_back(j); } else { vrem.push_back(j); } } } for (int j : remfirst) { edges1[i].erase(j); edges1[j].erase(i); operations[0][0]++; operations[operations[0][0]][0] = -1; operations[operations[0][0]][1] = i; operations[operations[0][0]][2] = j; } vrem_size = vrem.size(); // if (remfirst.size() == 0 && vrem.size() > 0) // { // for (int j = i + 1; j <= n; j++) // { // if (dist1[j] == 2) // { // cout << "+ " << i << " " << j << endl; // edges1[i].push_back(j); // edges1[j].push_back(i); // } // } // } //} while (vrem_size > 0); } cout << operations[0][0] << endl; for (int i = 1; i <= operations[0][0]; i++) { cout << (operations[i][0] < 0 ? "- " : "+ ") << operations[i][1] << " " << operations[i][2] << endl; } 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | #include <bits/stdc++.h> using namespace std; const int MAXN = 30002; const int MAXO = 300000; set<int> edges1[MAXN]; set<int> edges2[MAXN]; bool visited1[MAXN]; bool visited2[MAXN]; int dist1[MAXN]; int dist2[MAXN]; int operations[MAXO][3]; void bsf(int start, set<int> edges[], int dist[MAXN]) { bool visited[MAXN] = {false}; queue<int> q; q.push(start); visited[start] = true; while (!q.empty()) { int a = q.front(); q.pop(); for (int b : edges[a]) { if (!visited[b]) { dist[b] = dist[a] + 1; visited[b] = true; q.push(b); } } } } int main() { int n, m1, m2; cin >> n >> m1; for (int i = 0; i < m1; i++) { int a, b; cin >> a >> b; edges1[a].insert(b); edges1[b].insert(a); } cin >> m2; for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; edges2[a].insert(b); edges2[b].insert(a); } // dodawanie krawędzi int vadd_size = 0; for (int i = 1; i < n; i++) { dist2[i] = 0; bsf(i, edges2, dist2); do { int diff[n] = {0}; vector<int> vadd = {}, addfirst = {}; dist1[i] = 0; bsf(i, edges1, dist1); for (int j = i + 1; j <= n; j++) { diff[j] = dist1[j] - dist2[j]; if (diff[j] > 0) { if (dist1[j] == 2) { addfirst.push_back(j); } else { vadd.push_back(j); } } } for (int j : addfirst) { edges1[i].insert(j); edges1[j].insert(i); operations[0][0]++; operations[operations[0][0]][1] = i; operations[operations[0][0]][2] = j; } vadd_size = vadd.size(); if (addfirst.size() == 0 && vadd.size() > 0) { for (int j = i + 1; j <= n; j++) { if (dist1[j] == 2) { edges1[i].insert(j); edges1[j].insert(i); operations[0][0]++; operations[operations[0][0]][1] = i; operations[operations[0][0]][2] = j; } } } } while (vadd_size > 0); } // usuwanie krawędzi int vrem_size = 0; for (int i = 1; i < n; i++) { dist2[i] = 0; bsf(i, edges2, dist2); // do { int diff[n] = {0}; vector<int> vrem = {}, remfirst = {}; dist1[i] = 0; bsf(i, edges1, dist1); for (int j = i + 1; j <= n; j++) { diff[j] = dist1[j] - dist2[j]; if (diff[j] < 0) { if (dist1[j] == 1) { remfirst.push_back(j); } else { vrem.push_back(j); } } } for (int j : remfirst) { edges1[i].erase(j); edges1[j].erase(i); operations[0][0]++; operations[operations[0][0]][0] = -1; operations[operations[0][0]][1] = i; operations[operations[0][0]][2] = j; } vrem_size = vrem.size(); // if (remfirst.size() == 0 && vrem.size() > 0) // { // for (int j = i + 1; j <= n; j++) // { // if (dist1[j] == 2) // { // cout << "+ " << i << " " << j << endl; // edges1[i].push_back(j); // edges1[j].push_back(i); // } // } // } //} while (vrem_size > 0); } cout << operations[0][0] << endl; for (int i = 1; i <= operations[0][0]; i++) { cout << (operations[i][0] < 0 ? "- " : "+ ") << operations[i][1] << " " << operations[i][2] << endl; } return 0; } |