欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1950G.Shuffling Songs
Shuffling Songs
Vladislav has a playlist consisting of songs, numbered from to . Song has genre and writer . 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 and are strings of length no more than .
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 () — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer () — the number of songs in the original playlist.
Then lines follow, the -th of which contains two strings of lowercase letters and () — the genre and the writer of the -th song. Where and are lengths of the strings.
The sum of over all test cases does not exceed .
The sum of over all test cases does not exceed .
Output
For each test case, output a single integer — the minimum number of removals necessary so that the resulting playlist can be made exciting.
Note
In the first test case, the playlist is already exciting.
In the second test case, if you have the songs in the order , it is exciting, so you don't need to remove any songs.
In the third test case, you can remove songs . Then the playlist with songs in the order is exciting.
Samples
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
0
0
4
3
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |