P22192 [CodeChef] CERSOL
提高+
题目描述

Cersei wants to be the queen of seven kingdoms.

For this to happen, she needs to address the soldiers in her army. There are n$n$ soldiers in her army (numbered 1$1$ through n$n$). Cersei passes on the message to the first soldier (soldier 1).
This message needs to reach every soldier in the army. For this, the soldiers communicate among themselves by one soldier passing the message to another soldier through some communication links. It is known that the message could reach every soldier using the given links.
Now, each soldier will receive the message from exactly one soldier or Cersei and could pass on the message to atmost two soldiers. That is each soldier (except soldier 1) has only one incoming link and every soldier (including soldier 1) has atmost two outgoing links.
Now, the High Sparrow feels that Cersei is planning to kill his people first. Hence, for the sake of his people, he decided to appoint some sparrows to overhear every conversation between the soldiers (The conversation between Cersei and the first soldier needn't be overheard due to the fear of Ser Gregor Clegane).
To overhear a conversation between soldiers A$A$ and B$B$, there needs to be a sparrow either at soldier A$A$ or soldier B$B$ or both.
Also, by his research, the High Sparrow has found that the soldiers are partitioned into some classes (1$1$ to k$k$). That is, every soldier belongs to exactly one class. He then demands the presence of atleast one sparrow with each class he knows (1$1$ to k$k$).
Find the minimum number of sparrows the High Sparrow needs to recruit for the job or tell that he couldn't.

-----Input:-----
- The first line of the input contains the number of test cases t$t$.
- The first line of each test case gives the number of soldiers n$n$ in the army, the number of communication links m$m$ between the soldiers and the number of classes k$k$ in soldiers.
- The next line of the test case consists of n$n$ integers A1,A2....An$A_1,A_2....A_n$ each denoting the class of the ith$i^{th}$ soldier.
- The next m$m$ lines of the test case contain two integers u$u$ and v$v$, which denotes that soldier u$u$ can pass a message to soldier v$v$ (u≠v$u \neq v$).

-----Output:-----
For each test case, print in a single line the minimum number of sparrows required for the above task or print −1$-1$ if no such way is possible.

-----Constraints-----
- 1≤t≤500$1 \leq t \leq 500$
- 1≤n≤2500$1 \leq n \leq 2500$
- m=n−1$m = n - 1$
- 1≤k≤10$1 \leq k \leq 10$
- 1≤ai≤k$1 \leq a_i \leq k$
- The sum of n$n$ over all test cases is ≤2500$\leq 2500$.

-----Sample Input:-----
1
5 4 3
1 1 2 2 3
1 2
1 3
2 4
2 5

-----Sample Output:-----
3

-----EXPLANATION:-----
Selecting soldiers 1,4,5 would satisfy all the conditions.

-----Sample Input:-----
1
5 4 5
1 1 2 2 3
1 2
1 3
2 4
2 5

-----Sample Output:-----
-1

-----EXPLANATION:-----
Classes 4 and 5 are not present. So, there is no way possible.

输入格式

见题面

输出格式

见题面

样例输入
['1', '5 4 3', '1 1 2 2 3', '1 2', '1 3', '2 4', '2 5']
样例输出
['3']