The Heart of the Country

Time limit1sMemory limit128 MB

Summary
Find the largest set of vertices in an undirected graph such that each vertex has total troops from itself and its neighbors inside the set at least K, and report the set size and total troops.
Level

Medium7 of 10

Topics
Graph, Greedy, Implementation, Sorting
Solved
No attempts yet

Problem

The nation of Graphia is at war. Its neighbors have long watched with envy as Graphia built prosperous cities and linked them with a network of highways, and now they want a piece of it.

Graphia consists of several cities connected by highways. The terrain is rough, so the only way to travel between cities is along the highways. Each city has a number of troops quartered in it. The military command needs at least KK troops to defend a city; a city is defended by the troops stationed there together with the troops of every city connected to it by a single highway (with no city in between). Troops any farther away cannot arrive in time. The enemy attacks only one city at a time, so a city's troops may help defend that city and any of its neighbors. If a city cannot be defended, the command must assume its troops are captured and can no longer help defend Graphia.

In the example figure below, with K=10K = 10, city C may look well defended, but it will eventually fall.

Graphia's leadership wants to find the Heart of the country: the largest group of cities that can mutually defend one another, even if every other city falls.

More formally, a city is defensible if it can gather a total of at least KK troops from itself and from the cities directly adjacent to it. A set of cities is defensible if every city in it is defensible using only troops from itself and from its adjacent cities that are also in the set. The Heart of the country is the largest defensible set of cities: no other defensible set contains more cities.

Input

The input contains several data sets. Each data set begins with two integers NN and KK, where NN (3≤N≤10003 \le N \le 1000) is the number of cities and KK is the number of troops required to defend a city. The cities are numbered 00 through N−1N-1.

The next NN lines describe the cities, starting with city 00. Each description begins with an integer TT (0≤T≤100000 \le T \le 10000), the number of troops quartered in that city, followed by an integer MM, the number of highways leaving that city, and then MM integers giving the cities those highways lead to. Within one city's list every city number is distinct, and no highway connects a city to itself. Highways are two-way: if city ii appears in city jj's list, then city jj is guaranteed to appear in city ii's list.

The input ends with a line containing two space-separated zeros.

Output

For each data set, print two integers on one line: the number of cities in the Heart of the country and the total number of troops in the Heart of the country. Separate the two integers with a single space. Print no blank lines between data sets.

Examples1

  1. Example 1

    Input
    4 900
    100 2 1 2
    200 2 0 3
    500 2 0 3
    1000 2 1 2
    4 900
    100 3 1 2 3
    200 3 0 3 2
    500 3 1 3 0
    1000 3 2 1 0
    0 0
    
    Expected output
    3 1700
    4 1800