#include <cstdio> #include <vector> #include <queue> #include <sstream> #include <algorithm> using namespace std; vector<int> ingraph[30001]; vector<int> outgraph[30001]; queue<int> bfs; stringstream commands; int totalcommands; int supervertex_out_status[30001]; int visited[30001]; void clear_visited(); void connect_all(int); void disconnect_all(int); void disconnect_aux(int, int); void remove_edge_hard(int, int); void remove_edge(int, int, int); void add_edge(int, int, int); void clear_visited() { for (int i = 0; i < 30001; ++i) { visited[i] = 0; } } // connect all vertices to n according to task rules void connect_all(int n) { clear_visited(); visited[n] = 1; bfs.push(n); for (int i: ingraph[n]) { // printf("Pushing %d\n", i); // can't add edges to neighbors bfs.push(i); visited[i] = 1; } // printf("Now pushing with commands\n"); while (!bfs.empty()) { int current = bfs.front(); bfs.pop(); for (int i: ingraph[current]) { if (visited[i] == 0) { visited[i] = 1; totalcommands++; commands << "+ " << n << " " << i << "\n"; // printf("Pushing %d\n", i); bfs.push(i); } } } } void disconnect_all(int n) { clear_visited(); visited[n] = 1; for (int i: outgraph[n]) { if (visited[i] == 0) { visited[i] = 1; disconnect_aux(n, i); } } } void disconnect_aux(int n, int cur) { for (int i: outgraph[cur]) { if (visited[i] == 0) { visited[i] = 1; disconnect_aux(n, i); } } if (supervertex_out_status[cur] == 0) { // postfix disconnect supervertex with current remove_edge_hard(n, cur); } } void remove_edge_hard(int a, int b) { commands << "- " << a << " " << b << "\n"; totalcommands++; } void remove_edge(int a, int b, int supervertex) { if (a == supervertex || b == supervertex) return; if (a < b) return; // prevent duplicate removals commands << "- " << a << " " << b << "\n"; totalcommands++; } void add_edge(int a, int b, int supervertex) { if (a == supervertex || b == supervertex) return; if (a < b) return; // prevent duplicate adds commands << "+ " << a << " " << b << "\n"; totalcommands++; } int main() { int n, m1, m2; scanf("%d", &n); scanf("%d", &m1); for (int i = 0; i < m1; ++i) { int a, b; scanf("%d %d", &a, &b); ingraph[a].push_back(b); ingraph[b].push_back(a); } scanf("%d", &m2); for (int i = 0; i < m2; ++i) { int a, b; scanf("%d %d", &a, &b); outgraph[a].push_back(b); outgraph[b].push_back(a); } int supervertex = min(4, n); // printf("PROCESSING. SUPERVERTEX: %d\n", supervertex); for (int i = 1; i <= n; ++i) { sort(ingraph[i].begin(), ingraph[i].end()); sort(outgraph[i].begin(), outgraph[i].end()); } connect_all(supervertex); // printf("CREATED THE TOWER \n"); for (int i = 0; i < outgraph[supervertex].size(); ++i) { supervertex_out_status[outgraph[supervertex][i]] = 1; } // printf("Starting non-tower mods\n"); for (int i = 1; i <= n; ++i) { if (i == supervertex) continue; int in_idx = 0; int out_idx = 0; // printf("Non-tower modding vertex %d\n", i); while (true) { // printf("in: %d/%d | out: %d/%d\n", in_idx, ingraph[i].size(), out_idx, outgraph[i].size()); if (in_idx >= ingraph[i].size()) { // processed all input vertices - all remaining outvs should be added for (;out_idx < outgraph[i].size(); ++out_idx) { add_edge(i, outgraph[i][out_idx], supervertex); } break; } if (out_idx >= outgraph[i].size()) { // processed all output vertices - all remaining invs should be removed for (;in_idx < ingraph[i].size(); ++in_idx) { remove_edge(i, ingraph[i][in_idx], supervertex); } break; } int vxin = ingraph[i][in_idx]; int vxou = outgraph[i][out_idx]; if (vxin < vxou) { remove_edge(i, vxin, supervertex); in_idx++; } else if (vxin > vxou) { add_edge(i, vxou, supervertex); out_idx++; } else { in_idx++; out_idx++; } // printf("looped\n"); } } // printf("MODIFIED NON-TOWER LINES \n"); disconnect_all(supervertex); // printf("DISMANTLED TOWER \n"); printf("%d\n", totalcommands); printf("%s", commands.str().c_str()); 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | #include <cstdio> #include <vector> #include <queue> #include <sstream> #include <algorithm> using namespace std; vector<int> ingraph[30001]; vector<int> outgraph[30001]; queue<int> bfs; stringstream commands; int totalcommands; int supervertex_out_status[30001]; int visited[30001]; void clear_visited(); void connect_all(int); void disconnect_all(int); void disconnect_aux(int, int); void remove_edge_hard(int, int); void remove_edge(int, int, int); void add_edge(int, int, int); void clear_visited() { for (int i = 0; i < 30001; ++i) { visited[i] = 0; } } // connect all vertices to n according to task rules void connect_all(int n) { clear_visited(); visited[n] = 1; bfs.push(n); for (int i: ingraph[n]) { // printf("Pushing %d\n", i); // can't add edges to neighbors bfs.push(i); visited[i] = 1; } // printf("Now pushing with commands\n"); while (!bfs.empty()) { int current = bfs.front(); bfs.pop(); for (int i: ingraph[current]) { if (visited[i] == 0) { visited[i] = 1; totalcommands++; commands << "+ " << n << " " << i << "\n"; // printf("Pushing %d\n", i); bfs.push(i); } } } } void disconnect_all(int n) { clear_visited(); visited[n] = 1; for (int i: outgraph[n]) { if (visited[i] == 0) { visited[i] = 1; disconnect_aux(n, i); } } } void disconnect_aux(int n, int cur) { for (int i: outgraph[cur]) { if (visited[i] == 0) { visited[i] = 1; disconnect_aux(n, i); } } if (supervertex_out_status[cur] == 0) { // postfix disconnect supervertex with current remove_edge_hard(n, cur); } } void remove_edge_hard(int a, int b) { commands << "- " << a << " " << b << "\n"; totalcommands++; } void remove_edge(int a, int b, int supervertex) { if (a == supervertex || b == supervertex) return; if (a < b) return; // prevent duplicate removals commands << "- " << a << " " << b << "\n"; totalcommands++; } void add_edge(int a, int b, int supervertex) { if (a == supervertex || b == supervertex) return; if (a < b) return; // prevent duplicate adds commands << "+ " << a << " " << b << "\n"; totalcommands++; } int main() { int n, m1, m2; scanf("%d", &n); scanf("%d", &m1); for (int i = 0; i < m1; ++i) { int a, b; scanf("%d %d", &a, &b); ingraph[a].push_back(b); ingraph[b].push_back(a); } scanf("%d", &m2); for (int i = 0; i < m2; ++i) { int a, b; scanf("%d %d", &a, &b); outgraph[a].push_back(b); outgraph[b].push_back(a); } int supervertex = min(4, n); // printf("PROCESSING. SUPERVERTEX: %d\n", supervertex); for (int i = 1; i <= n; ++i) { sort(ingraph[i].begin(), ingraph[i].end()); sort(outgraph[i].begin(), outgraph[i].end()); } connect_all(supervertex); // printf("CREATED THE TOWER \n"); for (int i = 0; i < outgraph[supervertex].size(); ++i) { supervertex_out_status[outgraph[supervertex][i]] = 1; } // printf("Starting non-tower mods\n"); for (int i = 1; i <= n; ++i) { if (i == supervertex) continue; int in_idx = 0; int out_idx = 0; // printf("Non-tower modding vertex %d\n", i); while (true) { // printf("in: %d/%d | out: %d/%d\n", in_idx, ingraph[i].size(), out_idx, outgraph[i].size()); if (in_idx >= ingraph[i].size()) { // processed all input vertices - all remaining outvs should be added for (;out_idx < outgraph[i].size(); ++out_idx) { add_edge(i, outgraph[i][out_idx], supervertex); } break; } if (out_idx >= outgraph[i].size()) { // processed all output vertices - all remaining invs should be removed for (;in_idx < ingraph[i].size(); ++in_idx) { remove_edge(i, ingraph[i][in_idx], supervertex); } break; } int vxin = ingraph[i][in_idx]; int vxou = outgraph[i][out_idx]; if (vxin < vxou) { remove_edge(i, vxin, supervertex); in_idx++; } else if (vxin > vxou) { add_edge(i, vxou, supervertex); out_idx++; } else { in_idx++; out_idx++; } // printf("looped\n"); } } // printf("MODIFIED NON-TOWER LINES \n"); disconnect_all(supervertex); // printf("DISMANTLED TOWER \n"); printf("%d\n", totalcommands); printf("%s", commands.str().c_str()); return 0; } |