Code : Shuffling Songs
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
class Graph {
public:
int n;
vector<vector<bool>> adj;
Graph(int n) {
this->n = n;
adj.resize(n, vector<bool>(n, false));
}
int longest_hamiltonian_path() {
int full_mask = (1 << n) - 1;
vector<vector<bool>> dp(n, vector<bool>(full_mask + 1, false));
// dp[i][mask] represents whether we have a hamiltonian path that ends
// at vertex i and contains all the set bits of the mask.
for (int i = 0; i < n; i++) {
dp[i][1 << i] = true;
}
for (int cnt = 0; cnt <= n; cnt++) {
for (int mask = 0; mask <= full_mask; mask++) {
if (__builtin_popcount(mask) != cnt) {
continue;
}
for (int i = 0; i < n; i++) {
if (((1 << i) & mask) == 0 || !dp[i][mask]) {
continue;
}
for (int k = 0; k < n; k++) {
if (adj[i][k] && ((1 << k) & mask) == 0) {
int nxt = mask | (1 << k);
dp[k][nxt] = true;
}
}
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int mask = 0; mask <= full_mask; mask++) {
if (dp[i][mask]) {
ans = max(ans, __builtin_popcount(mask));
}
}
}
return ans;
}
};
void solve() {
int n;
cin >> n;
vector<string> g(n), w(n);
for (int i = 0; i < n; i++) {
cin >> g[i] >> w[i];
}
Graph graph(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
continue;
}
if ((g[i] == g[j]) || (w[i] == w[j])) {
graph.adj[i][j] = true;
}
}
}
cout << n - graph.longest_hamiltonian_path() << endl;
}
int main() {
int t;
cin >> t;
for (int zz = 0; zz < t; zz++) {
solve();
}
return 0;
}