The Kingdom of New Asia contains villages connected by
roads. Some roads are made of
cobblestones, and others are made of concrete. Keeping roads free-of-charge needs a lot of money, and
it seems impossible for the Kingdom to maintain every road. A new road maintaining plan is needed.
The King has decided that the Kingdom will keep as few free roads as possible, but every two
distinct villages must be connected by one and only one path of free roads. Also, although concrete
roads are more suitable for modern traffic, the King thinks walking on cobblestones is interesting. As
a result, he decides that exactly cobblestone roads will be kept free.
For example, suppose the villages and the roads in New Asia are as in Figure 1a. If the King wants
to keep two cobblestone roads free, then the Kingdom can keep roads ,
,
, and
free
as in Figure 1b. This plan satisfies the King's criteria because (1) every two villages are connected
via one and only one path of free roads, (2) there are as few free roads as possible, and (3) there are
exactly two cobblestone roads:
and
.
Figure 1: (a) An example configuration of villages and roads in the Kingdom of New Asia. Solid lines denote concrete roads, and dashed lines denote cobblestone roads. (b) A road maintaining plan that keeps two cobblestone roads free. Only free roads are shown.
Task
Given a description of roads in New Asia and the number of cobblestone roads that the King wants to keep free, write a program to determine if there is a road maintaining plan that satisfies the King's criteria, and output a valid plan if there is one.
Input
The first line contains three integers separated by one space:
, the number of villages
,
, the number of roads
, and
, the number of cobblestone roads the King wants to keep free
.
The following lines describes the roads in New Asia, which are numbered
to
. The
line describes Road
. It contains three integers separated by one space:
and
, the two villages connected by Road
. Villages are numbered
to
, and
, the type of Road
;
if Road
is made of cobblestone, and
if it is made of concrete.
There will be no more than one road connecting a pair of villages.
Output
If there is no road maintaining plan that satisfies the King's criteria, your program should print no
solution
on the first line of the output.
Otherwise, your program should output a valid road maintaining plan by listing roads that will be kept free, one road per line. To list a road, print the line in the input that describes it. The roads can be listed in any order. If there are more than one valid plan, you can output any such plan.
Scoring
The score for each input scenario will be if the correct answer is outputted and
otherwise.
In test scenarios worth of the points,
will be at most
.
Sample Input
5 7 2
1 3 0
4 5 1
3 2 0
5 3 1
4 3 0
1 2 1
4 2 1
(This input agrees with Figure 1a.)
Sample Output
3 2 0
4 3 0
1 2 1
5 3 1
(This output agrees with Figure 1b.)
Comments