European Girls' Olympiad in Informatics: 2023 Day 1 Problem 4
In Lund, biking is a very common method of transportation. But it is sometimes difficult to fit both cars and cyclists on the narrow streets. To improve the situation, the local governor wants to completely redesign the local street network.
There are important locations (numbered from
to
) in Lund that people frequently
travel between. People travel between two locations by
following a path, which is a sequence of streets going from the
first location to the other. A vehicle (car or bike) can travel
on a path if all relevant lanes are at least as wide as the
vehicle. Each newly built street connects two of these
important locations and it has a total width of
. This width can be arbitrarily
partitioned between bike lane and car lane. In Lund, some
engineers have recently invented cars and bikes of width
(these can travel on
lanes with width
).
The engineers have measured the widths of the cars and bikes in the city. For each pair of important locations, they know the widest car and the widest bike that should be able to travel between them, but the governor also requires that no wider cars or bikes can travel between these two locations.
Formally, you are given for each pair (
) two integer
values
and
. Your task is to
construct a network of streets connecting the
locations. The streets all have a
width of
, but for each
street
you can decide
the width of its bike lane
and this determines the width of its car lane
. The network must
satisfy the following:
It is possible to travel between each pair of locations. Note that this might require a bike or car of width
.
For each pair of locations
,
(where
), it is possible to travel between
and
by only using streets whose car lanes have a width of at least
. Also,
is the maximum number with this property. That is, for all paths between locations
and
it holds that at least one of the streets has a car lane of width at most
.
For each pair of locations
,
(where
), it is possible to travel between
and
by only using streets whose bike lanes have a width of at least
. Also,
is the maximum number with this property.
Can you help the governor of Lund to design such a street
network? Because the funding is limited, you can build at most
streets. You can
build multiple streets between the same pair of important
locations but you cannot connect a location with itself. All
streets can be used in both directions.
Input Specification
The first line of input contains two integers and
, the number of important locations
in Lund and the width of the streets that you can build.
The following
lines contain the integers
. The
-th of these lines will contain
every
where
. So the first
line will contain only
, the second will contain
and
, the third
,
,
, and so on.
The following
lines contain the integers
, in the same format as
.
Output Specification
If it is impossible to construct such a street network,
print one line with the string NO
.
Otherwise, print one line with the integer , the number of streets of your
network.
For each of the following lines, print three integers
, indicating that
a street with a bike lane of width
(and a car lane with width
) goes between
and
.
You may use at most streets. The streets you output
must satisfy
,
and
.
You may use multiple streets (possibly of different bike lane
width) between the same pair of important locations.
In case there are multiple solutions, you may output any of them.
Constraints and Scoring
.
.
for all
.
Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.
Group | Score | Constraints |
---|---|---|
1 | 10 | All |
2 | 5 | All |
3 | 17 | |
4 | 18 | |
5 | 19 | All |
6 | 31 | No further constraints. |
Example
In the first sample, the width of a street is and we need a car lane and a bike lane of width at least
between locations
and
. The solution is to have two separate streets connecting the locations, one with a
bike lane of width
and one with a car lane or width
.
In the second sample, the width of a street is again and there should be a
path with a bike lane of width
between every pair of important
locations and there is a path between the locations
and
and
and
where the width of the car lane is
for every street. This contradicts the fact that, as
,
there should not be a path with car lane width
from
to
as we can just join the two
aforementioned paths to form such a path. Thus it is not possible to construct such a street network.
In the third sample, the street network below fulfills all the conditions. For example, there should be a path with
minimum width of the car lane between location
and location
(e.g. by following the route
, a path where the bike lane has minimum width
(e.g. by following
the route
. At the same time, it can be checked that there are
no paths with a wider minimum width for any of the connections.
Note that there are many other solutions to the third sample.
Sample Input 1
2 1
1
1
Sample Output 1
2
0 1 0
0 1 1
Sample Input 2
4 1
0
0 1
0 0 1
1
1 1
1 1 1
Sample Output 2
NO
Sample Input 3
6 6
5
4 4
1 1 1
1 1 1 3
1 1 1 5 3
2
3 2
6 2 3
3 2 5 3
3 2 4 3 4
Sample Output 3
8
0 1 1
0 2 3
1 2 2
0 3 6
2 4 5
3 4 3
3 5 1
4 5 4
Comments