#5446. Problem 2. Picking Flowers

0

Problem 2. Picking Flowers

Problem 2. Picking Flowers

USACO 2026 Third Contest, Gold

Note: The time limit for this problem is 3s, 1.5x the default.

Farmer John's farm structure can be represented as a connected undirected graph with NN vertices and MM unweighted edges ($2 \leq N \leq 2 \cdot 10^5, N - 1 \leq M \leq 2 \cdot 10^5$). Initially, Farmer John is at his barn, represented by farm 11.

Initially, farms s1,s2,,sKs_1, s_2, \ldots, s_K contain flower fields and farms d1,d2,,dLd_1, d_2, \ldots, d_L are destination farms. FJ calls a path pretty if:

  • It starts at farm 11.
  • It ends at some destination farm xx.
  • There is no shorter path starting at farm 11 and ending at farm xx.
  • FJ visits all flower fields along the way.

FJ can wave his magic wand and make up to one more farm contain a flower field (if it doesn't already). However, FJ isn't very decisive. For farms ff numbered 22 through NN, after FJ temporarily makes farm ff contain a flower field, determine if there exists a pretty path.

Note that there are multiple test cases, and each case must be treated independently.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains TT (1T1001 \leq T \leq 100), the number of independent test cases.

The first line of each test case contains NN, MM, KK, and LL (0KN1,1LN10 \leq K \leq N - 1, 1 \leq L \leq N - 1).

The following line contains s1,s2,,sKs_1, s_2, \ldots, s_K (2siN2 \leq s_i \leq N, sis_i are all distinct).

The following line contains d1,d2,,dLd_1, d_2, \ldots, d_L (2diN2 \leq d_i \leq N, did_i are all distinct).

The following MM lines contain uu and vv, denoting there is an undirected edge between farms uu and vv. All edges are considered to have equal length. It is guaranteed that there aren't any multi-edges or self loops.

It is guaranteed that both the sum of NN and the sum of MM do not exceed 10610^6 over all test cases.

OUTPUT FORMAT (print output to the terminal / stdout):

For each test case, output a binary string of length N1N - 1. The ii'th character in the string should be 11 if the answer holds true for the (i+1)(i+1)'th farm.

SAMPLE INPUT:


1
7 7 0 1

5
1 2
2 3
3 4
4 5
5 6
6 7
3 6

SAMPLE OUTPUT:


111110

Since 55 is the only destination farm, the answer holds true if the ii'th farm lies on any shortest path from 11 to 55.

There are two shortest paths from 11 to 55, which are $1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ and $1 \rightarrow 2 \rightarrow 3 \rightarrow 6 \rightarrow 5$.

Since there are no farms that already contain flower fields, the answer for farm ii holds true if farm ii lies on at least one of the two aforementioned paths.

SAMPLE INPUT:


1
6 6 0 2

5 3
1 2
2 3
3 4
4 5
5 6
2 5

SAMPLE OUTPUT:


11010

There are two destination farms: 55 and 33. Since there are no farms that already contain flower fields, the ii'th farm must lie on a shortest path to either 55 or 33. Since farm 22 lies on a shortest path to farm 55, so the answer holds for farm 22. Trivially, farm 33 lies on the shortest path to farm 33 and farm 55 lies on the shortest path to farm 55.

SAMPLE INPUT:


3
4 3 2 1
2 3
4
1 2
2 3
3 4
4 4 2 1
2 3
4
1 2
1 3
2 4
3 4
5 5 2 1
2 4
5
1 2
1 3
2 4
3 4
4 5

SAMPLE OUTPUT:


111
000
1011

For the first test case, the answer holds true for the ii'th farm if FJ can pass through farm ii, farm 22, and farm 33 (in no particular order) on some shortest path to farm 44. It can be shown that the answer holds true for all farms.

SCORING: Inputs 4-6: K=0K = 0 and L=1L = 1 Inputs 7-9: K=0K = 0Inputs 10-23: No additional constraints

Problem credits: Chongtian Ma