Given a directed graph with iron and coal cells, find the fewest settlers needed to claim at least one iron cell and one coal cell from cell 1.
Medium7GraphBFSShortest pathNo attempts yetTime limit2sMemory limit512 MBIn the strategy board game Steel Age your favourite plan is simple: build as many soldiers as possible and club the opposition into submission. Building soldiers needs two resources, iron ore and coal.
The board consists of cells numbered from 1 to n, and a cell may contain a resource. A connection between two cells runs in one direction only. Being able to move from cell A to cell B does not mean you can move from B to A. If two cells are connected by a river, you can go downstream but not upstream until someone invents the steam engine, and a detour over roads and other cells may still bring you to the upstream cell.
At the start of the game you own a single cell and all of your settlers stand there. In one move you may send any number of settlers from a cell to one of its accessible neighbours. The first time settlers enter a cell, you claim it. Every claimed cell binds one settler, who stays there until the game ends. Your starting cell holds your palace, so it stays claimed without a settler.
Your goal is to claim at least one cell with iron ore and at least one cell with coal. Find the smallest number of settlers that reaches this goal.
The first line contains three integers n, the number of cells, m, the number of cells with iron ore, and k, the number of cells with coal. (2≤n≤105, 1≤m<n, 1≤k<n)
The second line contains m distinct integers o1,…,om, the IDs of the cells with iron ore. (1≤oi≤n)
The third line contains k distinct integers c1,…,ck, the IDs of the cells with coal. (1≤ci≤n)
The next n lines describe the board. The j-th of these lines starts with an integer a, the number of cells accessible from cell j (0≤a≤10), followed by a distinct integers b1,…,ba, the IDs of those cells, with bi=j. (1≤bi≤n)
No cell contains both iron ore and coal. At the start of the game you own cell 1.
Print the minimum number of settlers needed to claim at least one cell with iron ore and at least one cell with coal. Print impossible if you cannot claim both.