This page is still under construction.

Parts of this page are still being built. What you see may change.

Firefighter

Time limit1sMemory limit128 MB

Summary
On a graph with max degree 3, fire spreads one step per hour while one house can be protected each hour; maximize houses kept safe.
Level

Hard8 of 10

Topics
Graph, Tree, Greedy, Dynamic programming
Solved
No attempts yet

Problem

Bajtoland is a city that is extremely prone to fire. Its houses are connected by neighbor relations that form an undirected graph. Each house has at most 33 neighboring houses.

One day a fire breaks out in exactly one house kk. Every hour the fire spreads from every currently burning house to all of its neighbors that are not yet burning and not protected.

Every house that has exactly 33 neighbors is equipped with an automatic early fire-warning system, so a fire never originates in such a house. In other words, the house kk where the fire starts always has at most 22 neighbors.

There is only a single firefighting aircraft, and it can carry unlimited firefighters. The aircraft moves between houses practically instantly, but dropping one firefighter takes one hour — exactly the time the fire needs to spread one step. Therefore, during each hour you may permanently protect one house that is not yet burning. A protected house never catches fire.

The timeline is as follows. At hour 00 the fire starts in house kk. Then, during each hour, you may first protect one not-yet-burning house, and afterwards the fire spreads one step. This repeats until the fire can no longer spread.

Determine the maximum number of houses that can be kept safe from the fire.

Input

The first line contains the number of test cases tt (1≤t≤201 \le t \le 20).

Each test case is given as follows.

  • The first line contains the number of houses nn and the number of neighbor relations mm (1≤n≤1051 \le n \le 10^5, 0≤m≤32n0 \le m \le \frac{3}{2}n). Houses are numbered from 11 to nn.
  • Each of the next mm lines contains two integers aa and bb, meaning that house aa and house bb are neighbors (the relation is symmetric).
  • The last line contains the number kk (1≤k≤n1 \le k \le n) of the house where the fire starts.

Output

For each test case, print on its own line the maximum number of houses that can be kept safe from the fire.

Examples1

  1. Example 1

    Input
    2
    4 4
    1 2
    2 3
    3 4
    4 1
    1
    11 10
    1 2
    2 3
    2 4
    4 6
    3 5
    1 11
    11 7
    8 11
    9 7
    10 8
    1
    
    Expected output
    2
    8