File size: 3,121 Bytes
d3f4f72 |
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 |
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int LIM = 4000005;
struct UnionFind {
int N;
vector<int> root, diff_to_root, rank, sz, diffsum;
UnionFind(int _N):
N(_N), root(_N), diff_to_root(_N), rank(_N), sz(_N, 1), diffsum(_N) {
for (int i = 0; i < N; i++) {
root[i] = i;
}
}
int find(int i) {
int r = root[i];
if (r != i) {
root[i] = find(r);
diff_to_root[i] ^= diff_to_root[r];
}
return root[i];
}
bool merge(int i, int j, int d) {
int ri = find(i), rj = find(j);
d ^= diff_to_root[i] ^ diff_to_root[j];
if (ri == rj) {
return !d;
}
if (rank[ri] > rank[rj]) {
swap(ri, rj);
}
root[ri] = rj;
diff_to_root[ri] = d;
sz[rj] += sz[ri];
diffsum[rj] += d ? sz[ri] - diffsum[ri] : diffsum[ri];
if (rank[ri] == rank[rj]) {
rank[rj]++;
}
return true;
}
};
int N, K;
void solve() {
vector<int> both(LIM), ans(LIM);
vector<vector<vector<int>>> ind(LIM, vector<vector<int>>(2));
int maxL = 0;
// Input.
cin >> N >> K;
for (int i = 0; i < N; i++) {
string s[2];
cin >> s[0] >> s[1];
int len = max(s[0].size(), s[1].size());
maxL = max(maxL, len);
for (int b = 0; b < len; b++) {
int ch[2];
for (int j : {0, 1}) {
int slen = s[j].size();
ch[j] = b < slen ? s[j][slen - b - 1] - '0' : 0;
}
if (ch[0] && ch[1]) {
both[b]++;
} else {
for (int j : {0, 1}) {
if (ch[j]) {
ind[b][j].push_back(i);
}
}
}
}
}
// Consider each bit, starting from the most significant.
UnionFind U(N);
for (int b = maxL - 1; b >= 0; b--) {
ans[b] = 0;
// Bit included twice?
if (both[b] == N) {
ans[b] = 2;
continue;
}
// Bit not included at all?
if (both[b] + ind[b][0].size() + ind[b][1].size() < N) {
continue;
}
// Include bit once if possible.
int k = 0;
bool imp = false;
UnionFind U2 = U;
for (int i = 0; !imp && i < 2; i++) {
for (int j = 0; !imp && j < (int)ind[b][i].size() - 1; j++) {
if (!U2.merge(ind[b][i][j], ind[b][i][j + 1], 0)) {
imp = true;
}
}
}
if (imp) {
continue;
}
if (ind[b][0].size() > 0 && ind[b][1].size() > 0) {
if (!U2.merge(ind[b][0][0], ind[b][1][0], 1)) {
continue;
}
}
for (int i = 0; i < N; i++) {
if (U2.find(i) == i) {
k += min(U2.diffsum[i], U2.sz[i] - U2.diffsum[i]);
}
}
if (k > K) {
continue;
}
U = U2;
ans[b] = 1;
}
// Add up answer in binary.
for (int i = 0; i < maxL; i++) {
if (i == maxL - 1 && ans[i] >= 2) {
ans[maxL++] = 0;
}
ans[i + 1] += ans[i] / 2;
ans[i] %= 2;
}
// Output.
while (maxL > 1 && !ans[maxL - 1]) {
maxL--;
}
for (int i = maxL - 1; i >= 0; i--) {
cout << ans[i];
}
cout << endl;
}
int main() {
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
cout << "Case #" << t << ": ";
solve();
}
return 0;
}
|