closing
English (ISC)
Closing Time
Hungary is a country with N cities, numbered from 0 to N −1.
The cities are connected by N −1 bidirectional roads, numbered from 0 to N −2. For each j such
that 0 ≤j ≤N −2, road j connects city U[j] and city V [j] and has length W[j], that is, it allows
one to travel between the cities in W[j] units of time. Each road connects two different cities, and
each pair of cities is connected by at most one road.
A path between two distinct cities a and b is a sequence p ,p ,... ,p of distinct cities, such that:
p = a,
p = b,
for each i (0 ≤i < t), there is a road connecting cities p and p
.
It is possible to travel from any city to any other city by using the roads, that is, there exists a path
between every two distinct cities. It can be shown that this path is unique for each pair of distinct
cities.
The length of a path p ,p ,... ,p is the sum of the lengths of the t roads connecting consecutive
cities along the path.
In Hungary, many people travel to attend the Foundation Day festivities in two major cities. Once
the celebrations are over, they return to their homes. The government wants to prevent the crowd
from disturbing the locals, so they plan to lock down all cities at certain times. Each city will be
assigned a non-negative closing time by the government. The government has decided that the
sum of all closing times must not be more than K. More precisely, for every i between 0 and N −1
, inclusive, the closing time assigned to city i is a nonnegative integer c[i]. The sum of all c[i] must
not be greater than K.
Consider a city a and some assignment of closing times. We say that a city b is reachable from city
a if and only if either b = a, or the path p ,... ,p between these two cities (so in particular p = a
and p = b) satisfies the following conditions:
the length of the path p ,p is at most c[p ], and
the length of the path p ,p ,p is at most c[p ], and
...
the length of the path p ,p ,p ,... ,p is at most c[p ].
0
1
t
0
t
i
i+1
0
1
t
0
t
0
t
0
1
1
0
1
2
2
0
1
2
t
t
closing (1 of 4)
This year, the two main festival sites are located in city X and city Y . For each assignment of
closing times, the convenience score is defined as the sum of the following two numbers:
The number of cities reachable from city X.
The number of cities reachable from city Y .
Note that if a city is reachable from city X and reachable from city Y , it counts twice towards the
convenience score.
Your task is to compute the maximum convenience score that can be achieved by some
assignment of closing times.
Implementation Details
You should implement the following procedure.
int max_score(int N, int X, int Y, int64 K, int[] U, int[] V, int[] W)
N: the number of cities.
X, Y : the cities with main festival sites.
K: the upper bound on the sum of closing times.
U, V : arrays of length N −1 describing road connections.
W: array of length N −1 describing road lengths.
This procedure should return the maximum convenience score that can be achieved by
some assignment of closing times.
This procedure may be called multiple times in each test case.
Example
Consider the following call:
max_score(7, 0, 2, 10,
[0, 0, 1, 2, 2, 5], [1, 3, 2, 4, 5, 6], [2, 3, 4, 2, 5, 3])
This corresponds to the following road network:
closing (2 of 4)
Suppose the closing times are assigned as follows:
City
0
1
2
3
4
5
6
Closing time
0
4
0
3
2
0
0
Note that the sum of all closing times is 9, which is not more than K = 10. Cities 0, 1, and 3 are
reachable from city X (X = 0), while cities 1, 2, and 4 are reachable from city Y (Y = 2).
Therefore, the convenience score is 3 + 3 = 6. There is no assignment of closing times with
convenience score more than 6, so the procedure should return 6.
Also consider the following call:
max_score(4, 0, 3, 20, [0, 1, 2], [1, 2, 3], [18, 1, 19])
This corresponds to the following road network:
Suppose the closing times are assigned as follows:
City
0
1
2
3
Closing time
0
1
19
0
City 0 is reachable from city X (X = 0), while cities 2 and 3 are reachable from city Y (Y = 3).
Therefore, the convenience score is 1 + 2 = 3. There is no assignment of closing times with
closing (3 of 4)
convenience score more than 3, so the procedure should return 3.
Constraints
2 ≤N ≤200 000
0 ≤X < Y < N
0 ≤K ≤10
0 ≤U[j] < V [j] < N (for each j such that 0 ≤j ≤N −2)
1 ≤W[j] ≤10 (for each j such that 0 ≤j ≤N −2)
It is possible to travel from any city to any other city by using the roads.
S
≤200 000, where S is the sum of N over all calls to max_score in each test case.
Subtasks
We say that a road network is linear if road i connects cities i and i + 1 (for each i such that
0 ≤i ≤N −2).
1. (8 points) The length of the path from city X to city Y is greater than 2K.
2. (9 points) S
≤50, the road network is linear.
3. (12 points) S
≤500, the road network is linear.
4. (14 points) S
≤3 000, the road network is linear.
5. (9 points) S
≤20
6. (11 points) S
≤100
7. (10 points) S
≤500
8. (10 points) S
≤3 000
9. (17 points) No additional constraints.
Sample Grader
Let C denote the number of scenarios, that is, the number of calls to max_score . The sample
grader reads the input in the following format:
line 1: C
The descriptions of C scenarios follow.
The sample grader reads the description of each scenario in the following format:
line 1: N X Y K
line 2 + j (0 ≤j ≤N −2): U[j] V [j] W[j]
The sample grader prints a single line for each scenario, in the following format:
line 1: the return value of max_score
18
6
N
N
N
N
N
N
N
N
N
closing (4 of 4)
Input / Output on this judge
This is the IOI function-implementation task closing adapted to standard input / output. Instead of implementing the function, read its arguments from standard input and print the returned value(s) to standard output, exactly as the official grader below does (its internal anti-cheat checks have been removed). You may also simply submit the grader together with your own implementation of the function.
Function signature (closing.h):
#include <vector>
int max_score(int N, int X, int Y, long long K,
std::vector<int> U, std::vector<int> V, std::vector<int> W);
Reference I/O driver (official grader, sanitized):
#include "closing.h"
#include <cassert>
#include <cstdio>
#include <vector>
int main()
{
int Q;
assert(1 == scanf("%d", &Q));
std::vector<int> N(Q), X(Q), Y(Q);
std::vector<long long> K(Q);
std::vector<std::vector<int>> U(Q), V(Q), W(Q);
for (int q = 0; q < Q; q++)
{
assert(4 == scanf("%d %d %d %lld", &N[q], &X[q], &Y[q], &K[q]));
U[q].resize(N[q] - 1);
V[q].resize(N[q] - 1);
W[q].resize(N[q] - 1);
for (int i = 0; i < N[q] - 1; ++i)
{
assert(3 == scanf("%d %d %d", &U[q][i], &V[q][i], &W[q][i]));
}
}
fclose(stdin);
std::vector<int> result(Q);
for (int q = 0; q < Q; q++)
{
result[q] = max_score(N[q], X[q], Y[q], K[q], U[q], V[q], W[q]);
}
for (int q = 0; q < Q; q++)
{
printf("%d\n", result[q]);
}
fclose(stdout);
return 0;
}
| 서브태스크 | 점수 | 설명 |
|---|---|---|
Subtask 1 (01-separated) | 8점 | None |
Subtask 2 (02-small-line) | 9점 | None |
Subtask 3 (03-cube-line) | 12점 | None |
Subtask 4 (04-square-line) | 14점 | None |
Subtask 5 (05-exp) | 9점 | None |
Subtask 6 (06-quad) | 11점 | None |
Subtask 7 (07-cube) | 10점 | None |
Subtask 8 (08-square) | 10점 | None |
Subtask 9 (09-full) | 17점 | None |
2
7 0 2 10
0 1 2
0 3 3
1 2 4
2 4 2
2 5 5
5 6 3
4 0 3 20
0 1 18
1 2 1
2 3 19
6
3