Public submissions
Source code of every submission to this problem in this contest
will be visible for everyone since 2013-05-16 07:00:00.
Submit | All submissions | Best solutions | Back to list |
GREED - Greedy island |
Gon is on Greedy island. He wants to go home. But to get the ticket to leave the game, he has to get N cards labeled in a sequence from 1 to N (the order of the cards in his hand is irrelevant). He already has N cards, but not forming a sequence from 1 to N. So he wants you to help him. For some cards, he can change one card for another for one piece of gold. Help him to get the ticket at the minimum cost (using the minimum number of exchanges).
Input
The first line contains t, the number of tests (1<=t<=10). For each test case:
- the number of cards N is given is given in the first line (2<=N<=500).
- the next N lines contain the N cards owned by Gon.
- the following line contains e, the number of different allowed types of exchanges.
- the next e lines contain two integers xi,yi each which mean that we can exchange and replace the card marked x by the card marked y and vice versa.
There is a blank line after each test case.
Output
For each test case, output a line denoting the minimum required cost.
Example
Input: 1 4 1 2 2 2 2 2 3 3 4 Output: 3
Added by: | Le Trong Dao |
Date: | 2005-06-08 |
Time limit: | 50s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | C C++ 4.3.2 CPP JAVA |
Resource: | Mr.Tran Minh Quan |
Public source code since: | 2013-05-16 07:00:00 |