G. Shuffling Songs

G. Shuffling Songs

Vladislav has a playlist consisting of n songs, numbered from 1 to n. Song i has genre gi and writer wi. He wants to make a playlist in such a way that every pair of adjacent songs either have the same writer or are from the same genre (or both). He calls such a playlist exciting. Both gi and wi are strings of length no more than 104.

It might not always be possible to make an exciting playlist using all the songs, so the shuffling process occurs in two steps. First, some amount (possibly zero) of the songs are removed, and then the remaining songs in the playlist are rearranged to make it exciting.

Since Vladislav doesn't like when songs get removed from his playlist, he wants the making playlist to perform as few removals as possible. Help him find the minimum number of removals that need to be performed in order to be able to rearrange the rest of the songs to make the playlist exciting.

Input

The first line of the input contains a single integer t (1t1000) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n (1n16) — the number of songs in the original playlist.

Then n lines follow, the i-th of which contains two strings of lowercase letters gi and wi (1|gi|,|wi|104) — the genre and the writer of the i-th song. Where |gi| and |wi| are lengths of the strings.

The sum of 2n over all test cases does not exceed 216.

The sum of |gi|+|wi| over all test cases does not exceed 4105.

Output

For each test case, output a single integer — the minimum number of removals necessary so that the resulting playlist can be made exciting.

Example

input

4
1
pop taylorswift
4
electronic themotans
electronic carlasdreams
pop themotans
pop irinarimes
7
rap eminem
rap drdre
rap kanyewest
pop taylorswift
indierock arcticmonkeys
indierock arcticmonkeys
punkrock theoffspring
4
a b
c d
e f
g h

output

0
0
4
3

Note

In the first test case, the playlist is already exciting.

In the second test case, if you have the songs in the order 4,3,1,2, it is exciting, so you don't need to remove any songs.

In the third test case, you can remove songs 4,5,6,7. Then the playlist with songs in the order 1,2,3 is exciting.

 

解题思路

  因为前段时间有事好久没有写博客了,也没有怎么写题。挑个上场的 Div. 4 最后一题水水()

  数据范围的提示 "The sum of 2n over all test cases does not exceed 216." 就直接把做法贴脸上了,挺乐的()

  先定义符号 aibi 分别表示第 i 首音乐的作者和流派。g(i,j)=0/1 表示第 i 首音乐能否与第 j 首音乐相邻,直接暴力预处理即可。定义状态 f(i,j) 表示由二进制集合 i 中的音乐(即所有是 1 位)构成的所有合法序列中(且最后一首音乐为 j),代价的最小值。其中代价指集合中要删除的元素数量。当然还有一个前提是 i 的二进制下的第 j 位要为 1

  考虑状态转移,如果第 j 首音乐不接在任何一首音乐的后面,那么代价就是 popcount(i)1,其中 popcount(i) 表示 i 在二进制下为 1 的位数。如果要接在第 k 首音乐后面,有两个条件要满足:i 的第 k 位是 1g(j,k)=1。代价就是 f(i2j,k)。因此状态转移方程就是 f(i,j)=min{popcount(i)1,min0k<nimod2k=0g(j,k)=1{f(i2j,k)}}

  AC代码如下,时间复杂度为 O(n|ai|+|bi|+n22n)

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 16, M = 1 << 16, INF = 0x3f3f3f3f;

string a[N], b[N];
int g[N][N];
int f[M][N];

void solve() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (a[i] == a[j] || b[i] == b[j]) g[i][j] = g[j][i] = 1;
            else g[i][j] = g[j][i] = 0;
        }
    }
    for (int i = 1; i < 1 << n; i++) {
        for (int j = 0; j < n; j++) {
            if (i >> j & 1) {
                f[i][j] = __builtin_popcount(i) - 1;
                for (int k = 0; k < n; k++) {
                    if (i >> k & 1 && g[j][k]) f[i][j] = min(f[i][j], f[i ^ 1 << j][k]);
                }
            }
            else {
                f[i][j] = INF;
            }
        }
    }
    int ret = n;
    for (int i = 0; i < n; i++) {
        ret = min(ret, f[(1 << n) - 1][i]);
    }
    cout << ret << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round 937 (Div. 4) Editorial:https://codeforces.com/blog/entry/127664

posted @   onlyblues  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2022-04-01 机器人移动
2022-04-01 序列重排
2021-04-01 在堆区申请二维数组的方法
Web Analytics
点击右上角即可分享
微信分享提示